C++11 编译错误Boost library未定义对'shm#u open';的引用;

C++11 编译错误Boost library未定义对'shm#u open';的引用;,c++11,boost,compilation,linker,C++11,Boost,Compilation,Linker,我无法编译我的项目。 当我运行make命令时,我得到以下错误 如果我尝试单独使用命令编译shareMemoryWriter.cpp g++-std=c++11 shareMemoryWriter.cpp main2.cpp-o main-lpthread-lrt 编译成功了 操作系统:LinuxMint 生成文件: CC = g++ CFLAGS=-Wall -g -std=c++11 -lpthread -lrt LDFLAGS= -std=c++11 -lpthread -lr

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

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

操作系统:LinuxMint 生成文件:

    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  $^
不要这样做:

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

链接器标志不属于编译行,只属于链接行

也不要这样做:

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

-std=c++11
是一个编译标志,不属于链接行

这:

$(CC)$(LDFLAGS)$(对象)-o$@


将库放在链接行的开头。他们应该在最后。解释为什么这很重要。

谢谢你的回答,我将CFLAGS=-Wall-g-std=c++11-lpthread-lrt改为CFLAGS=-std=c++11-Wall-g和LDFLAGS=-std=c++11-lpthread-lrt改为LDFLAGS=-lpthread-lrt和$(LDFLAGS)$(LDFLAGS)$(OBJECTS)-o$@改为$(CC)$(OBJECTS)-o$@$(LDFLAGS)它可以工作
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