Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法链接到共享库_C++_Linux_Hyperlink_G++_Shared Libraries - Fatal编程技术网

C++ 无法链接到共享库

C++ 无法链接到共享库,c++,linux,hyperlink,g++,shared-libraries,C++,Linux,Hyperlink,G++,Shared Libraries,我正在尝试编译一个最小的共享库并链接到它,但已经失败了两个小时。以下是所有代码: // rect.h class Rect{ private: int width_, height_; public: Rect(int width, int height); int width(); int height(); }; // rect.cpp #include "rect.h" Rect::Rect(int width, int height) :widt

我正在尝试编译一个最小的共享库并链接到它,但已经失败了两个小时。以下是所有代码:

// rect.h
class Rect{
  private:
    int width_, height_;
  public:
    Rect(int width, int height);
    int width();
    int height();
};

// rect.cpp
#include "rect.h"
Rect::Rect(int width, int height)
:width_(width), height_(height){}

int Rect::width() { return width_; }
int Rect::height() { return height_; }

// client.cpp
#include "rect.h"
#include <iostream>
int main() { 
  std::cout << Rect(1,2).width();
  return 0;
}
该库编译得很好,并且根据我所知正确导出了Rect类:

$ nm -D librect.so 
0000000000201028 B __bss_start
                 w __cxa_finalize
0000000000201028 D _edata
0000000000201030 B _end
0000000000000738 T _fini
                 w __gmon_start__
00000000000005b8 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w _Jv_RegisterClasses
0000000000000714 T _ZN4Rect5widthEv
0000000000000724 T _ZN4Rect6heightEv
00000000000006f0 T _ZN4RectC1Eii
00000000000006f0 T _ZN4RectC2Eii
最奇怪的是,它编译得很好,可以在我的工作计算机(Kubuntu 12.10 64位)上运行,但无法在我尝试过的任何其他机器上正确链接(总共4台,全部64位Ubuntu/Kubuntu 12.04和12.10)

我想尽了一切办法。将verbose选项传递给链接器表明librect.so确实被成功找到

有人知道问题出在哪里吗?

图书馆必须寻找当地的翻译单位:

g++ -L. -Wl,-rpath,'.' client.cpp -o client -lrect
#                                           ^^^^^^
它与链接器如何查找未解析的符号有关;如果你对此感到好奇,可以在互联网上搜索大量信息。

图书馆必须寻找当地的翻译单位:

g++ -L. -Wl,-rpath,'.' client.cpp -o client -lrect
#                                           ^^^^^^

它与链接器如何查找未解析的符号有关;如果您对此感到好奇,请在互联网上搜索大量信息。

所有代码?真正地遗忘包括可能?在GCC 4.7.2的Fedora 17 32位上运行良好;也就是说,如果我在rect的顶部添加#include“rect.h”。cpp@LuchianGrigore我确实忘记了包含,但它在我的代码中。我编辑了这个问题。谢谢你的密码?真正地遗忘包括可能?在GCC 4.7.2的Fedora 17 32位上运行良好;也就是说,如果我在rect的顶部添加#include“rect.h”。cpp@LuchianGrigore我确实忘记了包含,但它在我的代码中。我编辑了这个问题。谢谢这太荒谬了!至少在我熟悉这背后的原因之前我这么认为。链接器不能至少给出一个警告,说明它忽略了它的一个参数或其他东西。@Mitko:这确实很有意义。在你抱怨之前写下你自己的链接器:-)这太荒谬了!至少在我熟悉这背后的原因之前我这么认为。链接器不能至少给出一个警告,说明它忽略了它的一个参数或其他东西。@Mitko:这确实很有意义。在你抱怨之前写下你自己的链接器:-)