我尝试在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
发布于 2011-11-03 02:17:13
shm_open是通过链接库提供的。尝试将-lrt标志传递给链接器。
尝试: g++ -c -Wall shared.cpp
g++ -L /lib -lrt共享。o -o共享
发布于 2015-04-19 07:44:37
只是补充@anio的答案:
链接时,可能需要在命令末尾添加-lrt标志。尝试:
g++ -L /lib shared.o -o shared -lrt
发布于 2016-02-04 16:48:49
我同样的问题在@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
因为我更喜欢与远程目标机器同步,所以我已经将我的库的"sysroot path“添加到eclipse项目属性"Library Search Path (-L)”中。
/home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf
我还在库(-l)中添加了"rt“,这最终解决了我的问题。
如果您正在使用
g++ -L $YOUR_PATH_TO_LIB$ shared.o -o shared -lrt
将$YOUR_PATH_TO_LIB替换为您的。
https://stackoverflow.com/questions/7985236
复制相似问题