【问题标题】:Compile Error Boost library undefined reference to `shm_open'编译错误 Boost 库未定义对“shm_open”的引用
【发布时间】:2015-04-11 02:06:14
【问题描述】:

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

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

操作系统:linux mint 生成文件:

    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

【问题讨论】:

    标签: c++11 boost compilation linker


    【解决方案1】:

    不要这样做:

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

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

    也不要这样做:

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

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

    这个:

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

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

    【讨论】:

    • 感谢您的回答我将 CFLAGS=-Wall -g -std=c++11 -lpthread -lrt 更改为 CFLAGS=-std=c++11 -Wall -g 和 LDFLAGS= - std=c++11 -lpthread -lrt 到 LDFLAGS=-lpthread -lrt 和 $(LDFLAGS) $(CC) $(LDFLAGS) $(OBJECTS) -o $@ 到 $(CC) $(OBJECTS) -o $ @ $(LDFLAGS) 它有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多