C++ 使用boost::interprocess编译链接器时出错

C++ 使用boost::interprocess编译链接器时出错,c++,boost,linker-errors,undefined-reference,boost-interprocess,C++,Boost,Linker Errors,Undefined Reference,Boost Interprocess,我已经编写了一个小程序,将boost::interprocess::container字符串写入共享内存,并从中读取另一个 我收到以下链接器错误: 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

我已经编写了一个小程序,将boost::interprocess::container字符串写入共享内存,并从中读取另一个

我收到以下链接器错误:

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

但这些命令都不起作用。有人能告诉我如何编译这个简单的例子吗?

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

g++-lpthread-lrt-SharedMemTest.cpp
(包括pthread和rt库)。你至少试过谷歌搜索吗?@Yossarian,很明显,因为你的推荐也不起作用。如果谷歌对一切都有答案,我们就不需要了,是吗?@czchlong:他的建议应该有效,但是试试
g++SharedMemTest.cpp-lpthread-lrt
(订单问题)。@ildjarn,非常感谢。秩序确实重要。谢谢可能重复的