Boost错误消息

Boost error message

本文关键字:消息 错误 Boost      更新时间:2023-10-16

在尝试使用boost 1.55库编译某些代码时,我收到了一条错误消息,但我无法理解。起初,我有一个简单的程序,试图分配一个共享内存对象。遇到一些错误,最后我决定复制并粘贴boost示例并编译它。得到了同样的错误。示例代码:

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
int main(int argc, char *argv[])
{
   using namespace boost::interprocess;
   if(argc == 1){  //Parent process
      //Remove shared memory on construction and destruction
      struct shm_remove
      {
         shm_remove() { shared_memory_object::remove("MySharedMemory"); }
         ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
      } remover;
      //Create a shared memory object.
      shared_memory_object shm (create_only, "MySharedMemory", read_write);
      //Set size
      shm.truncate(1000);
      //Map the whole shared memory in this process
      mapped_region region(shm, read_write);
      //Write all the memory to 1
      std::memset(region.get_address(), 1, region.get_size());
      //Launch child process
      std::string s(argv[0]); s += " child ";
      if(0 != std::system(s.c_str()))
         return 1;
   }
   else{
      //Open already created shared memory object.
      shared_memory_object shm (open_only, "MySharedMemory", read_only);
      //Map the whole shared memory in this process
      mapped_region region(shm, read_only);
      //Check that memory was initialized to 1
      char *mem = static_cast<char*>(region.get_address());
      for(std::size_t i = 0; i < region.get_size(); ++i)
         if(*mem++ != 1)
            return 1;   //Error checking memory
   }
   return 0;
}

当我编译它时,我得到的错误是:

g++  -D_REENTRANT -o loadfrags loadfrags.o -Lstdc++ -Wl,--rpath -Wl,/usr/local/lib
loadfrags.o: In function `boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::ipcdetail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)':
loadfrags.cc:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_9ipcdetail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::ipcdetail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x105): undefined reference to `shm_open'
loadfrags.cc:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_9ipcdetail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::ipcdetail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x139): undefined reference to `shm_open'
loadfrags.cc:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_9ipcdetail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::ipcdetail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x18e): undefined reference to `shm_open'
loadfrags.cc:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_9ipcdetail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::ipcdetail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x1e7): undefined reference to `shm_open'
loadfrags.o: In function `boost::interprocess::shared_memory_object::remove(char const*)':
loadfrags.cc:(.text._ZN5boost12interprocess20shared_memory_object6removeEPKc[boost::interprocess::shared_memory_object::remove(char const*)]+0x42): undefined reference to `shm_unlink'
collect2: ld returned 1 exit status
make: *** [loadfrags] Error 1

据我所知,库安装正确。我能够毫无问题地编译和运行一个匿名共享内存对象程序。我不是一个足够的C++程序员,无法快速确定这里出了什么问题。我的想法是我错过了#include。我很感激任何人能提供的任何见解/帮助。

错误消息中有线索表明这是一个链接问题:

  • undefined reference to指示链接时所需的符号不可用

  • ld returned 1 exit status显示ld(链接器)未成功完成

诀窍是,当您看到undefined reference to <some symbol>时,您需要找到提供符号的库,并将-l<libraryName>添加到编译器选项中。如果库不在正常位置,则可能还需要添加-L<PathWhereLibraryCanBeFound>

如注释中所示以及C++boost库shared_memory_object中所建议的,对';shm_open';添加CCD_ 6解决了这种情况下的问题。