C++ boost库shared_memory_object undefined reference 'shm_open'

标签 c++ boost compiler-errors shared-memory ubuntu-11.04

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

关于C++ boost库shared_memory_object undefined reference 'shm_open',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7985236/

相关文章:

c++ - 基于模板的条件类成员

c++ - 在 C++ 中使用 boost::lexical_cast 将 double 转换为字符串?

c - MpLab 链接错误 : Could Not Allocate Section (C30 Compiler)

c++ - LNK 1120文字翻译

r - R开始消息包含在编译过程中

c++ - 在父类中实现部分 C++ 接口(interface)

c++ - 在 C++ 中传递和返回二维数组

android - 如何从 AAssetManager 获取 std::basic_istream?

可执行文件的 Boost.Build 和 <link> 功能

c++ - map 中的不同键类型