提升错误消息

时间:2013-12-02 18:32:55

标签: c++ boost

我在尝试使用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。我很感激任何人都可以给予的见解/帮助。

1 个答案:

答案 0 :(得分:3)

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

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

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

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

正如评论中所示以及C++ boost libraries shared_memory_object undefined reference to 'shm_open'中建议添加-lrt解决了此案例中的问题。