【发布时间】:2013-12-18 13:47:03
【问题描述】:
我在尝试使用 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。任何人都可以提供任何见解/帮助,我将不胜感激。
【问题讨论】:
-
@Praetorian 我查看了该问题,但没有应用任何回复(我已将 -Lrt 添加到链接行)。
-
-L和-l是有区别的,前者用于链接器搜索后者指定的库的目录 -
@Praetorian 谢谢。小写的“L”起到了作用。
-
请改进您的标题,以便将此问题与标题列表中的其他问题区分开来。