繁体   English   中英

C++ boost 库shared_memory_object 对'shm_open' 的未定义引用

[英]C++ boost libraries shared_memory_object undefined reference to 'shm_open'

我尝试在 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

由于我更喜欢​​与远程目标机器同步,因此我已将库的“sysroot 路径”添加到 eclipse 项目属性“库搜索路径 (-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。

g++ -L /lib shared.o -o shared -lrt -lpthread

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM