C++ boost libraries shared_memory_object undefined reference to 'shm_open'
Asked Answered
T

4

17

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

Tricostate answered 2/11, 2011 at 18:11 Comment(1)
I have the exact same error but the solution doesn't work for me.Sickly
H
24

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
Halfcocked answered 2/11, 2011 at 18:17 Comment(2)
@TerryLiYifeng You might need to specify the path to librt, it should be in /lib.Halfcocked
Here on my arch linux linking -lrt coped with shm error. However I needed to add -lpthread to remove some additional errors.Spinescent
D
3

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
Dextrality answered 18/4, 2015 at 23:44 Comment(1)
but why though?Arthrospore
S
1

My same problem got solved from @anio's answer but I needed to do additional work. So I am presenting my solution.

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.

Scotsman answered 4/2, 2016 at 8:48 Comment(0)
A
-2
g++ -L /lib shared.o -o shared -lrt -lpthread
Airman answered 1/5, 2020 at 8:44 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Illusionary

© 2022 - 2024 — McMap. All rights reserved.