C++ Boost错误消息
在尝试使用boost 1.55库编译某些代码时,我收到了一条错误消息,但我不理解。起初,我有一个简单的程序,试图分配一个共享内存对象。遇到一些错误,最后我决定复制并粘贴boost示例并编译它。我也犯了同样的错误。示例代码:C++ Boost错误消息,c++,boost,C++,Boost,在尝试使用boost 1.55库编译某些代码时,我收到了一条错误消息,但我不理解。起初,我有一个简单的程序,试图分配一个共享内存对象。遇到一些错误,最后我决定复制并粘贴boost示例并编译它。我也犯了同样的错误。示例代码: #include <boost/interprocess/shared_memory_object.hpp> #include <boost/interprocess/mapped_region.hpp> #include <cstring>
#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;
}
据我所知,这些库已正确安装。我能够编译并运行一个匿名共享内存对象程序,没有任何问题。我还不足以用C++程序员快速确定这里的错误。我的想法是,我失踪了,而且还包括了一些东西。我非常感谢任何人提供的任何见解/帮助。错误消息中有线索表明这是一个链接问题:
的未定义引用表示链接时所需的符号不可用对
显示ld(链接器)未成功完成ld返回1退出状态
未定义的对
的引用时,您需要找到提供符号的库,并将-l
添加到编译器选项中。如果库不在正常位置,您可能还需要添加-L
如注释所示以及添加
-lrt
中所建议的,解决了这种情况下的问题 错误消息中有线索表明这是一个链接问题:
的未定义引用表示链接时所需的符号不可用对
显示ld(链接器)未成功完成ld返回1退出状态
未定义的对
的引用时,您需要找到提供符号的库,并将-l
添加到编译器选项中。如果库不在正常位置,您可能还需要添加-L
如注释所示以及添加
-lrt
中所建议的,解决了这种情况下的问题 @Praetorian我看了那个问题,但没有一个回答适用(我后来在链接行中添加了-Lrt)。-L
和-L
之间有区别,前者用于链接器将搜索latter@Praetorian谢谢小写字母“L”起到了作用。请改进你的标题,使其与标题列表中的其他问题区分开来。@Praetorian我看了这个问题,但没有任何回答适用(我后来在链接行中添加了-Lrt)。-L
和-L
之间有区别,前者用于链接器将在其中搜索latter@Praetorian谢谢小写字母“L”起到了作用。请改进你的标题,使其与标题列表中的其他问题区分开来。
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