chang jc
chang jc

Reputation: 529

link error when using shared memory with POCO

When I make a application with POCO, I found a strange thing about compile error.

I have add -lrt according to check others' similar issues. However, it still does NOT work.

/usr/local/lib//libPocoFoundation.a(SharedMemory.o): In function 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 toshm_open' SharedMemory.cpp:(.text+0x31b): undefined reference to shm_unlink' /usr/local/lib//libPocoFoundation.a(SharedMemory.o): In function Poco::SharedMemoryImpl::close()': SharedMemory.cpp:(.text+0x666): undefined reference to `shm_unlink' collect2: error: ld returned 1 exit status Makefile:17: recipe for target 'test' failed make: *** [test] Error 1

Magically, if I add a dummy function as below.

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;
}

It successfully is built.

I cannot understand why & would like to search the formal to avoid such strange workaround. Is there anyone can help on it please?

Also, I have checked libPocoFoundation.a and find both

  1. shm_open

  2. shm_unlink

belong to 'The symbol is undefined'

Thanks in advance.

Upvotes: 0

Views: 503

Answers (1)

Alex
Alex

Reputation: 5330

Answered here - the problem is the order of linked libraries (-lrt before -lPocoFoundation).

Upvotes: 1

Related Questions