簡體   English   中英

C++ boost 庫shared_memory_object 對'shm_open' 的未定義引用

[英]C++ boost libraries shared_memory_object undefined reference to 'shm_open'

我嘗試在 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; 
} 

只會得到以下錯誤:

/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

我用來編譯文件的命令:g++ -o shared shared.cpp

我用來安裝 boost 庫的命令: sudo apt-get install libboost-dev libboost-doc

shm_open 通過鏈接 librt 可用。 嘗試將 -lrt 標志傳遞給鏈接器。

嘗試: g++ -c -Wall shared.cpp

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

只是添加到@anio 的答案:

鏈接時,可能需要在命令末尾添加 -lrt 標志。 嘗試:

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

我同樣的問題從@anio 的回答中得到解決,但我需要做額外的工作。 由於聲譽低,我無法發表評論。 所以我正在展示我的便士,可能有人會覺得它有幫助。 我是嬰兒式的一切,如果我顯得幼稚,那么“對不起”。

我在 Debian 上使用 Eclipse 對 arm-linux-gnueabihf-g++ 進行交叉編譯。 所以我首先找到了“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

由於我更喜歡​​與遠程目標機器同步,因此我已將庫的“sysroot 路徑”添加到 eclipse 項目屬性“庫搜索路徑 (-L)”中

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

還向庫(-l)添加了“rt”,最終解決了我的問題。

如果您正在編譯使用

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

用你的替換 $YOUR_PATH_TO_LIB。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM