当我尝试使用librbd时,遇到了以下链接问题。
以下是我的代码片段。
- main.cc
#include <iostream>
#include <rados/librados.hpp>
#include <rbd/librbd.hpp>
int main(){
// Initialize and open an rbd image
std::string pool = "xxx";
std::string image_name = "xxxx";
int r;
librados::Rados cluster;
librados::IoCtx io_ctx;
librbd::Image image;
librbd::RBD rbd;
r = cluster.init("cinder-ctest");
r = cluster.connect();
r = cluster.ioctx_create(pool.c_str(), io_ctx);
r = rbd.open_read_only(io_ctx, image, image_name.c_str(), NULL);
std::string id;
image.get_id(&id); // <- Where the problem occurs
std::cerr << id << std::endl;
return 0;
}
使用以下命令编译时出现错误。
$ g++ main.cc -o info -lrbd -lrados
/tmp/ccOpSFrv.o: In function `main':
main.cc:(.text+0x12b): undefined reference to `librbd::Image::get_id(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)'
collect2: error: ld returned 1 exit status
但是我使用 nm 命令查看 get_id 是否存在:
$ nm -D /usr/lib64/librbd.so | grep get_id
0000000000083d00 T rbd_get_id
000000000008de10 T _ZN6librbd5Image6get_idEPSs
U _ZN8librados7v14_2_05IoCtx6get_idEv
并且它在全球范围内可见:
$ readelf -s /usr/lib64/librbd.so | grep get_id
498: 0000000000083d00 70 FUNC GLOBAL DEFAULT 11 rbd_get_id
559: 000000000008de10 54 FUNC GLOBAL DEFAULT 11 _ZN6librbd5Image6get_idEP
为什么编译时会出现错误:librbd::Image::get_id
未定义的引用?这个函数显然是存在的,让我感到疑惑。
g++ -c main.cc
命令,并检查nm main.o
命令输出的未定义符号get_id
。如果它有一个不同的名称,那么你遇到了ABI不兼容的问题。 - aschepler$ nm main.o | grep get_id U _ZN6librbd5Image6get_idEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
- toliu