与POCO一起使用共享内存时出现链接错误

时间:2017-11-22 10:09:52

标签: gcc linker-errors shared-memory poco poco-libraries

当我用POCO创建应用程序时,我发现编译错误很奇怪。

我根据检查其他人添加-lrt'类似的问题。但是,它仍然不起作用。

  

/usr/local/lib//libPocoFoundation.a(SharedMemory.o):在功能中   Poco::SharedMemoryImpl::SharedMemoryImpl(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, Poco::SharedMemory::AccessMode, void const*, bool)': SharedMemory.cpp:(.text+0x2ab): undefined reference to的shm_open&#39;   SharedMemory.cpp :(。text + 0x31b):未定义引用shm_unlink' /usr/local/lib//libPocoFoundation.a(SharedMemory.o): In function Poco :: SharedMemoryImpl :: close()&#39 ;: SharedMemory.cpp :(。text + 0x666):   未定义的引用`shm_unlink&#39; collect2:错误:ld返回1   退出状态Makefile:17:目标配方&#39; test&#39;失败了:***   [测试]错误1

奇怪的是,如果我添加一个虚拟函数,如下所示。

int dummy()
{
    const char *memname = "sample";
    const size_t region_size = sysconf(_SC_PAGE_SIZE);
    int fd = shm_open(memname, O_CREAT | O_TRUNC | O_RDWR, 0666);
    if (fd == -1)
        return -1;
    int r = shm_unlink(memname);
    if (r != 0)
        return -1;
}

它已成功构建。

我无法理解为什么&amp;想搜索正式以避免这种奇怪的解决方法。 请问有人可以提供帮助吗?

另外,我检查了libPocoFoundation.a并找到了两个

  1. 的shm_open

  2. shm_unlink

  3. 属于&#39;符号未定义&#39;

    提前致谢。

1 个答案:

答案 0 :(得分:1)

已回答here - 问题是链接库的顺序(-lrt之前-lPocoFoundation)。