我尝试在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
答案 0 :(得分:19)
通过链接librt使shm_open可用。尝试将-lrt标志传递给链接器。
尝试: g ++ -c -Wall shared.cpp
g ++ -L / lib -lrt shared.o -o shared
答案 1 :(得分:1)
同样的问题从@ anio的答案中解决了,但我需要做更多的工作。由于声誉低,我无法发表评论。所以我提出我的便士,可能有人觉得它很有帮助。我是宝贝踩着一切,所以&#34;抱歉&#34;如果我看起来很幼稚。
我在Debian上使用Eclipse来交叉编译arm-linux-gnueabihf-g ++。所以我首先找到&#34; librt&#34;
的位置/$ 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
由于我更喜欢与远程目标机器同步,因此我添加了&#34; sysroot路径&#34;我的库进入eclipse项目属性&#34;图书馆搜索路径(-L)&#34;
/home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf
还添加了&#34; rt&#34;到图书馆(-l),最终解决了我的问题。
如果您正在使用
进行编译g++ -L $YOUR_PATH_TO_LIB$ shared.o -o shared -lrt
用你的替换$ YOUR_PATH_TO_LIB。
答案 2 :(得分:0)
只需添加@ anio的回答:
链接时,可能需要在命令末尾添加-lrt标志。 尝试:
g++ -L /lib shared.o -o shared -lrt
答案 3 :(得分:-1)
g ++ -L / lib shared.o -o shared -lrt -lpthread