编译Error Boost库未定义引用`shm_open'

时间:2015-04-11 02:06:15

标签: c++11 boost compilation linker

我无法编译我的项目。 当我运行make命令时,我收到以下错误。

如果我尝试使用命令单独编译shareMemoryWriter.cpp     g ++ -std = c ++ 11 shareMemoryWriter.cpp main2.cpp -o main -lpthread -lrt 编译成功

操作系统:linux薄荷 生成文件:

    CC = g++

CFLAGS=-Wall -g -std=c++11 -lpthread -lrt 

LDFLAGS= -std=c++11 -lpthread -lrt

SOURCES=main.cpp \
daq.cpp \
srs.cpp \
fec.cpp \
chip.cpp \
detector.cpp \
chamber.cpp \
chamberSpecs.cpp \
multilayer.cpp \
layer.cpp \
readout.cpp \
connector.cpp \
connectorSpecs.cpp \
createEvents.cpp \
event.cpp \
shareMemoryWriter.cpp \
Coordinates.cpp

OBJECTS=$(SOURCES:.cpp=.o)

EXECUTABLE=main

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS)  -c $<  -o $@

clean:
    $(RM) *.o *~ $(MAIN)

depend: $(SRCS)
    makedepend  $^

编译输出:

g++ -Wall -g -std=c++11 -lpthread -lrt   -c main.cpp  -o main.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c daq.cpp  -o daq.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c srs.cpp  -o srs.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c fec.cpp  -o fec.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c chip.cpp  -o chip.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c detector.cpp  -o detector.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c chamber.cpp  -o chamber.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c chamberSpecs.cpp  -o chamberSpecs.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c multilayer.cpp  -o multilayer.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c layer.cpp  -o layer.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c readout.cpp  -o readout.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c connector.cpp  -o connector.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c connectorSpecs.cpp  -o connectorSpecs.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c createEvents.cpp  -o createEvents.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c event.cpp  -o event.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c shareMemoryWriter.cpp  -o shareMemoryWriter.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c Coordinates.cpp  -o Coordinates.o
g++ -std=c++11 -lpthread -lrt main.o daq.o srs.o fec.o chip.o detector.o chamber.o chamberSpecs.o multilayer.o layer.o readout.o connector.o connectorSpecs.o createEvents.o event.o shareMemoryWriter.o Coordinates.o -o main
shareMemoryWriter.o: In function `boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::ipcdetail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)':
/usr/include/boost/interprocess/shared_memory_object.hpp:309: undefined reference to `shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:315: undefined reference to `shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:327: undefined reference to `shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:334: undefined reference to `shm_open'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::mutexattr_wrapper::mutexattr_wrapper(bool)':
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:37: undefined reference to `pthread_mutexattr_init'
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:38: undefined reference to `pthread_mutexattr_setpshared'
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:40: undefined reference to `pthread_mutexattr_settype'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::mutexattr_wrapper::~mutexattr_wrapper()':
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:45: undefined reference to `pthread_mutexattr_destroy'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::posix_condition::posix_condition()':
/usr/include/boost/interprocess/sync/posix/condition.hpp:132: undefined reference to `pthread_condattr_setpshared'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_open(sem_t*&, boost::interprocess::ipcdetail::create_enum_t, char const*, unsigned int, boost::interprocess::permissions const&)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:61: undefined reference to `sem_open'
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:69: undefined reference to `sem_open'
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:77: undefined reference to `sem_open'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_close(sem_t*)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:105: undefined reference to `sem_close'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_post(sem_t*)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:148: undefined reference to `sem_post'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_try_wait(sem_t*)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:164: undefined reference to `sem_trywait'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

1 个答案:

答案 0 :(得分:0)

不要这样做:

  

CFLAGS=-Wall -g -std=c++11 -lpthread -lrt

链接器标志不属于编译行,仅在链接行上。

不要这样做:

  

LDFLAGS = -std = c ++ 11 -lpthread -lrt

-std=c++11是一个编译标记,不属于您的链接行。

此:

  

$(CC) $(LDFLAGS) $(OBJECTS) -o $@

将库放在链接行的开头。它们应位于 end This answer解释了为什么这很重要。