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

前端 未结 4 1161
囚心锁ツ
囚心锁ツ 2021-01-04 00:21

I tried to compile the following code on ubuntu 11.04:

#include  
#include  

int main() 
         


        
相关标签:
4条回答
  • 2021-01-04 00:42

    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.

    0 讨论(0)
  • 2021-01-04 00:48

    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

    0 讨论(0)
  • 2021-01-04 00:56

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

    0 讨论(0)
  • 2021-01-04 00:58

    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
    
    0 讨论(0)
提交回复
热议问题