2016-09-21 107 views
0

比方說,我有一個名爲libfoo.so的共享庫,它也依賴於另一個名爲libbar.so的共享庫。在libfoo.so中,它提供的唯一功能是存儲兩個整數的類,並且可以返回這兩個整數相加的值。C++鏈接問題

libfoo.so:

// Foo.hpp 

class Foo 
{ 
    int x, y; 
public: 
    Foo(int x, int y); 
    int add() const; 
}; 

現在,在libbar.so,有兩類:即只存儲一個字符串和存儲由創建一個Foo計算整數的BAR2類BAR1類對象並使用add()函數生成新的整數。

// Bar1.hpp 

class Bar1 
{ 
    std::string str; 
public: 
    Bar1(const std::string& str); 
    const std::string& getString() const; 
}; 


// Bar2.hpp 

#include "foo.hpp" 

class Bar2 
{ 
    int z; 
public: 
    Bar2(int x, int y); 
    int getInt() const; 
}; 

現在,我想寫一個使用Bar1的程序。我不在乎Bar2。我非常簡單的程序是這樣的:

// Test.cpp 

#include <iostream> 

#include "Bar1.hpp" 

using namespace std; 

int main() 
{ 
    Bar1 bar1("Hello"); 
    cout << bar1.getString() << endl; 
} 

我編譯這個程序,像這樣:

g++ -c test.cpp -o test.o 
g++ -o test test.o -lbar 

時產生的錯誤是:

undefined reference to 'Foo::Foo(int, int)' 
undefined reference to 'Foo::add() const' 

這可以通過指定'修正-lfoo'鏈接器。但是,我現在鏈接到庫中,我的二進制文件永遠不會使用。

在編譯器明白我的二進制文件不關心解析這些符號的情況下,是否有辦法清除它,因爲我從來沒有在程序的任何地方使用Bar2?

編輯:

添加類的實現。我不認爲這很重要。在這裏,他們是:

// Foo.cpp 

#include "Foo.hpp" 

Foo::Foo(int new_x, int new_y) 
{ 
    x = new_x; 
    y = new_y; 
} 

int Foo::add() const 
{ 
    return x + y; 
} 

這裏是Bar1.cpp:

// Bar1.cpp 

#include "Bar1.hpp" 

Bar1::Bar1(const std::string& the_str) 
{ 
    str = the_str; 
} 

const std::string& Bar1::getString() const 
{ 
    return str; 
} 

這裏是Bar2.cpp:

// Bar2.cpp 

#include "Bar2.hpp" 

Bar2::Bar2(int x, int y) 
{ 
    Foo foo(x, y); 
    z = foo.add(); 
} 

int Bar2::getInt() const 
{ 
    return z; 
} 

注意,它應該是顯而易見的,我寫這些類似這樣的純粹是爲了實驗目的。我正在玩弄鏈接器,以及開發人員如何鏈接到庫並使用它們。

+0

簡而言之,沒有。但是,當你鏈接'libbar.so'時,它可能已經在連接線上提供了'-lfoo'。然後,當你只放入'-lbar'時,鏈接器會爲你提供'libfoo.so'。 – jxh

+0

不是直接連接,就像那樣。使用帶'RTLD_LAZY'標誌和'dlsym()'的'dlopen()'。這不會直接與'C++'代碼一起工作,所以有必要通過'C'-linkage函數蹦跳你的方式,這就是爲什麼我沒有把它作爲一個全面的答案。但它在理論上是可行的。 –

+0

Something's calling those'Foo' functions somewhere –

回答

0

where是foo.cpp和bar.cpp?你沒有實現類foo和bar:

// foo.cpp 
#include "Foo.hpp" 

Foo::Foo(int X, int Y) : x(X), y(Y){} 
int Foo::add()const{return x + y;} 


// Bar1.cpp 

#include "bar1.hpp" 

Bar1::Bar1(const std::string& STR) : str(STR){} 
const std::string& Bar1::getString() const{return str;} 


// Bar2.cpp 

#include "foo.hpp" 

Bar2::Bar2(int X, int Y) : x(X), y(Y) {z = x + y;} 
int Bar2::getInt() const{ return z;}