我试图在ubuntu 11.04上编译以下代码。
#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>
int main()
{
boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, "Highscore", boost::interprocess::read_write);
shdmem.truncate(1024);
std::cout << shdmem.get_name() << std::endl;
boost::interprocess::offset_t size;
if (shdmem.get_size(size))
std::cout << size << std::endl;
}
结果得到以下错误
/tmp/cc786obC.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&)':
shared_memory.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'
shared_memory.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'
shared_memory.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'
shared_memory.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'
collect2: ld returned 1 exit status
我用来编译文件的命令:g++ -o shared shared.cpp。
我用来安装boost库的命令:sudo apt-get install libboost-dev libboost-doc。
shm_open是通过链接librt来实现的。尝试将-lrt标志传递给链接器。
尝试:g++ -c -Wall shared.cpp。
g++ -L lib -lrt shared.o -o shared
只是对@anio的回答进行补充。
在链接时,可能需要在命令的最后加上-lrt标志。
g++ -L /lib shared.o -o shared -lrt
我的同样问题在@anio的回答中得到了解决,但我需要做额外的工作。由于我的名气不大,所以不能发表评论。所以我提出了我的一分钱,也许有人会发现它的帮助。我是在婴儿时期就开始学习的,所以如果我看起来很幼稚的话,"对不起"。
我在Debian上使用Eclipse进行arm-linux-gnueabihf-g++的交叉编译。所以我首先找到了 "librt "的位置。
/$ find -iname "librt*"
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.a
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.so
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librtmp.so.0
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt-2.13.so
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt-2.19.so
./lib/i386-linux-gnu/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt-2.19.so
./lib/i386-linux-gnu/librt-2.19.so
由于我喜欢和远程目标机同步,我在eclipse项目属性 "库搜索路径(-L) "中为我的库添加了 "sysroot路径"
/home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf
还在Libraries(-l)中加入了 "rt",最终解决了我的问题。
如果你在编译时使用
g++ -L $YOUR_PATH_TO_LIB$ shared.o -o shared -lrt
将$YOUR_PATH_TO_LIB替换为你的。
g++ -L lib shared.o -o shared -lrt -lpthread