Linux環境下使用eclipse開發C++動態鏈接庫程序
Linux中也有類似windows中DLL的變成方法,只不過名稱不同而已。在Linux中,動態鏈接叫做Standard Object,生成的動態鏈接文件為*.so。詳細請參考相關文檔。
開發環境:Eclipse 3.4.2
G++:4.3.2
1. 創建動態鏈接庫
(1)在Eclipse中創建新的C++工程
File->New->Project->C++->C++ Project,選擇Shared Library。
(2)創建源代碼文件
File->New->Source File,指定名稱為shared.cc
(3)編寫源碼
[cpp] view plaincopy
- /*
- * Test.cc
- *
- * Created on: Oct 10, 2009
- * Author: fify
- */
- #include <iostream>
- using namespace std;
- void show()
- {
- cout << "Shared Library." << endl;
- }
編譯之后將在工程目錄下生成一個libshared.so文件,這就是我們要用的動態鏈接庫文件
2. 調用動態鏈接庫文件
(1)創建C++工程,選擇Executable類型工程
(2)添加Source File,并編寫源碼
[cpp] view plaincopy
- //============================================================================
- // Name : Import.cpp
- // Author : Fify
- // Version :
- // Copyright : Fify copyleft
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- using namespace std;
- void show();
- int main()
- {
- show();
- return 0;
- }
(3)設置導入動態庫
Project->Properties->C/C++ Build->Settings->Tool Settings->GCC C++ Linker->Libraries
添加Libraries (-l): shared
添加Library search path (-L): {剛才編譯生成的*.so文件路徑}
(4)編譯該項目
3. 運行含動態鏈接庫的程序
點擊運行,會出現以下錯誤:error while loading shared libraries: libShared.so: cannot open shared object file: No such file or directory ,因為系統無法從默認位置找到動態鏈接庫文件。修改系統默認搜索鏈接庫的路徑,程序即可順利執行,輸出:
Shared Library.
修改動態庫搜索路徑的方法有五種,參考:http://www.yuanma.org/data/2007/1212/article_2923.htm ,這里不在詳細介紹
RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成