Linux動態庫的導出控制


在實際工作中,許多軟件模塊是以動態庫的方式提供的。做為模塊開發人員,我們不僅要掌握如何編寫和構建動態庫,還要了解如何控制動態庫的導出接口,這樣,我們可以向模塊的用戶僅導出必要的接口,而另一些內部接口,為了安全或其他考慮,可以不必導出。當需要導出C++類時,問題顯得更復雜一些,不過我認為不應導出C++類成員,而只應導出純C接口。

和Visual C++不同,GCC編譯器默認會導出所有符號。假設我們需要導出兩個全局函數test和test2,以及一個C++類foo,此類有兩個public成員函數a和b,聲明文件so.h如下:

 1 #ifndef __SO_H__
 2 #define __SO_H__
 3 
 4 #ifdef __cplusplus
 5 extern "C" {
 6 #endif
 7 
 8 void  test();
 9 int   test2(int _v);
10 
11 
12 class foo
13 {
14 public:
15     void a();
16     int  b(int _v);
17 };
18 
19 
20 #ifdef __cplusplus
21 }
22 #endif
23 
24 
25 #endif

 

實現文件so.cpp如下:

 1 #include <stdio.h>
 2 #include "so.h"
 3 
 4 
 5 void  test()
 6 {
 7     printf("test\n");
 8 }
 9 
10 
11 int  test2(int _v)
12 {
13     return _v*_v;
14 }
15 
16 
17 void  foo::a()
18 {
19     printf("foo::a()\n");
20 }
21 
22 
23 int  foo::b(int _v)
24 {
25     return _v*_v;
26 }

 

我們把這些代碼編譯成一個動態庫test.so:

$ g++ -shared -o test.so -fPIC so.cpp

然后使用nm命令查看動態符號表:

$ nm -D test.so
                 w _Jv_RegisterClasses
000000000000063e T _ZN3foo1aEv
0000000000000658 T _ZN3foo1bEi
0000000000201018 A __bss_start
                 w __cxa_finalize
                 w __gmon_start__
0000000000201018 A _edata
0000000000201028 A _end
00000000000006a8 T _fini
0000000000000508 T _init
                 U puts
000000000000061c T test
000000000000062e T test2

可見,test、test2、foo::a、foo::b都被導出了(注意帶有大寫T的項)。

接着我們再寫一個客戶程序main.cpp,來實現此動態庫,代碼如下:

 1 #include <stdio.h>
 2 #include "so.h"
 3 
 4 int main(int argc, char** argv)
 5 {
 6     test();
 7     printf("test2: %d\n", test2(3));
 8 
 9     foo f;
10     f.a();
11     printf("foo::b: %d\n", f.b(2));
12 
13     return 0;
14 }

 

編譯命令和輸出如下:

$ g++ -o app main.cpp test.so
$ ./app
test
test2: 9
foo::a()
foo::b: 4

 

上面的操作,顯示了默認情況下,Linux動態庫是導出了所有符號的,另外,也展示了如何導出和使用動態庫中的C++類成員。

 

現在,假設我們要只導出全局函數test和foo類的成員函數a,怎么辦呢?有好幾種方法,最方便的是使用GCC編譯器特性。首先,將so.h修改如下:

 1 #ifndef __SO_H__
 2 #define __SO_H__
 3 
 4 #define DLL_PUBLIC __attribute__ ((visibility("default")))
 5 
 6 #ifdef __cplusplus
 7 extern "C" {
 8 #endif
 9 
10 DLL_PUBLIC  void  test();
11 int   test2(int _v);
12 
13 
14 class foo
15 {
16 public:
17     DLL_PUBLIC  void a();
18     int  b(int _v);
19 };
20 
21 
22 #ifdef __cplusplus
23 }
24 #endif
25 
26 
27 #endif

so.cpp不變。接着,使用以下命令編譯test.so:

$ g++ -shared -o test.so -fPIC -fvisibility=hidden so.cpp

其中,__attribute__ ((visibility("default")))是默認可見標簽,還有一個是__attribute__ ((visibility("hidden")))。-fvisibility=hidden,意思是將動態庫中的符號設置為默認不導出。這樣一來,只有添加了DLL_PUBLIC,也就是__attribute__ ((visibility("default")))標簽的符號才會被導出。我們可以用nm命令來檢驗:

$ nm -D test.so
                 w _Jv_RegisterClasses
00000000000005ee T _ZN3foo1aEv
0000000000201018 A __bss_start
                 w __cxa_finalize
                 w __gmon_start__
0000000000201018 A _edata
0000000000201028 A _end
0000000000000658 T _fini
00000000000004b8 T _init
                 U puts
00000000000005cc T test

可見,只留下了test和foo::a,其他兩個符號已經看不到了。

如果此時,我們按一開始的步驟編譯main.cpp,會報錯:

$ g++ -o app main.cpp test.so
/tmp/ccA12RQf.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `test2'
main.cpp:(.text+0x48): undefined reference to `foo::b(int)'
collect2: ld returned 1 exit status

 

OK, 至此我們已經實現了Linux動態庫(.so)中導出符號的控制。

 

 

【參考資料】

1. 這個還講了其他導出符號控制方法 http://blog.csdn.net/zdragon2002/article/details/6061962

2. 這是一個PDF,《how to write shared libraries》,詳細講解了如何在Linux下創建共享庫 

 http://www.akkadia.org/drepper/dsohowto.pdf

3. GCC Visibility http://gcc.gnu.org/wiki/Visibility


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM