简体   繁体   中英

C++ boost libraries shared_memory_object undefined reference to 'shm_open'

I tried to compile the following code on ubuntu 11.04:

#include <boost/interprocess/shared_memory_object.hpp> 
#include <iostream> 

int main() 
{ 
  boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, "Highscore", boost::interprocess::read_write); 
  shdmem.truncate(1024); 
  std::cout << shdmem.get_name() << std::endl; 
  boost::interprocess::offset_t size; 
  if (shdmem.get_size(size)) 
    std::cout << size << std::endl; 
} 

only to get the following errors:

/tmp/cc786obC.o: In function `boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)':
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0xe0): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x116): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x16c): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x1c0): undefined reference to `shm_open'
collect2: ld returned 1 exit status

Command I used to compile the file: g++ -o shared shared.cpp

Command I used to install the boost libraries: sudo apt-get install libboost-dev libboost-doc

shm_open is made available by linking librt. Try passing -lrt flag to the linker.

Try: g++ -c -Wall shared.cpp

g++ -L /lib -lrt shared.o -o shared

Just adding to @anio's answer:

While linking, the -lrt flag may need to be added at the end of the command. Try:

g++ -L /lib shared.o -o shared -lrt

My same problem got solved from @anio's answer but I needed to do additional work. As I cannot comment due to low reputation. So I am presenting my pennies, may be someone find it helpful. I am baby stepping everything, so "sorry" if I seem childish.

I am using Eclipse on Debian for cross compiling for arm-linux-gnueabihf-g++. So I first found the location for "librt"

/$ find -iname "librt*"
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.a
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.so
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librtmp.so.0
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt-2.13.so
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt-2.19.so
./lib/i386-linux-gnu/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt-2.19.so
./lib/i386-linux-gnu/librt-2.19.so

As I prefer to sync with the remote target machine I have added "sysroot path" for my library into eclipse project properties "Library Search Path (-L)"

/home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf

Also added "rt" to Libraries (-l), which ultimately solved my problem.

In case you are compiling with the use

g++ -L $YOUR_PATH_TO_LIB$ shared.o -o shared -lrt

replace $YOUR_PATH_TO_LIB with yours.

g++ -L /lib shared.o -o shared -lrt -lpthread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM