2

我编写了一个小程序,它将boost :: interprocess :: container字符串写入共享内存,另一个程序将其读取。使用boost :: interprocess编译链接器错误

我收到如下链接错误:

g++ SharedMemTest.cpp -L /usr/lib/x86_64-linux-gnu/librt.a 
/tmp/cc5v7WKj.o: In function `boost::interprocess::detail::mutexattr_wrapper::mutexattr_wrapper(bool)': 
SharedMemTest.cpp:(.text._ZN5boost12interprocess6detail17mutexattr_wrapperC2Eb[_ZN5boost12interprocess6detail17mutexattr_wrapperC5Eb]+0x1c): undefined reference to `pthread_mutexattr_init' 
SharedMemTest.cpp:(.text._ZN5boost12interprocess6detail17mutexattr_wrapperC2Eb[_ZN5boost12interprocess6detail17mutexattr_wrapperC5Eb]+0x31): undefined reference to `pthread_mutexattr_setpshared' 
SharedMemTest.cpp:(.text._ZN5boost12interprocess6detail17mutexattr_wrapperC2Eb[_ZN5boost12interprocess6detail17mutexattr_wrapperC5Eb]+0x4c): undefined reference to `pthread_mutexattr_settype' 
/tmp/cc5v7WKj.o: In function `boost::interprocess::detail::mutexattr_wrapper::~mutexattr_wrapper()': 
SharedMemTest.cpp:(.text._ZN5boost12interprocess6detail17mutexattr_wrapperD2Ev[_ZN5boost12interprocess6detail17mutexattr_wrapperD5Ev]+0x14): undefined reference to `pthread_mutexattr_destroy' 
/tmp/cc5v7WKj.o: In function `boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)': 
SharedMemTest.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0xe0): undefined reference to `shm_open' 
SharedMemTest.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x116): undefined reference to `shm_open' 
SharedMemTest.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x16c): undefined reference to `shm_open' 
SharedMemTest.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x1c0): undefined reference to `shm_open' 
/tmp/cc5v7WKj.o: In function `boost::interprocess::shared_memory_object::remove(char const*)': 
SharedMemTest.cpp:(.text._ZN5boost12interprocess20shared_memory_object6removeEPKc[boost::interprocess::shared_memory_object::remove(char const*)]+0x40): undefined reference to `shm_unlink' 

我也曾尝试用下面的链接:

g++ -c SharedMemTest.cpp 
g++ -L /usr/lib/x86_64-linux-gnu/librt.a SharedMemTest.o -o SharedMemTest 

但是,这些命令都在工作。有人可以告诉我怎样才能得到这个简单的例子来编译?

+0

'g ++ -lpthread -lrt SharedMemTest.cpp'(包括pthread和rt库)。你至少试过Google搜索吗? – nothrow

+2

@Yossarian,显然,因为你的建议也不起作用。如果谷歌有一切的答案,我们不需要这样做,我们会吗? – czchlong

+3

@czchlong:他的建议_应该可以工作,但可以尝试'g ++ SharedMemTest.cpp -lpthread -lrt'(命令很重要)。 – ildjarn

回答

0

-L的参数应该是一个目录,但是你要在它之后放置一个库文件名。你可能意思是-l rt。其他人已经建议-l pthread,您也可以使用-pthread链接到pthreads库。