首先需要安裝好eclipse 的cdt編譯的環境。配置好gcc 編譯環境。lunix 一般情況下都會有gcc ,windows就需要裝個cygwin。
一、建立一個動態連庫(.so檔案)
1、先在eclipse中建立一個共用庫工程
File->New->Project->C Project,選擇Shared Library,選擇empty project
2、建立工程的源檔案share.cpp
File->New->Source Folder,指定名稱為src
File->New->Source file,指定名稱為share
最後的目錄結構:
3、編寫c代碼以及c標頭檔
#include <stdio.h>#include <stdlib.h>void sayhello(){printf("hello !!1");}
標頭檔:
void sayhello();
3、build project
會出現錯誤資訊如下:
**** Build of configuration Debug for project share ****
make all
Building file: ../src/share.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/share.d" -MT"src/share.d" -o "src/share.o" "../src/share.c"
Finished building: ../src/share.c
Building target: libshare.so
Invoking: GCC C Linker
gcc -shared -o "libshare.so" ./src/share.o
/usr/bin/ld: ./src/share.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
./src/share.o: could not read symbols: Bad value
collect2: ld 返回 1
make: *** [libshare.so] 錯誤 1
**** Build Finished ****
錯誤資訊表示在編譯時間候需要 -fPIC 參數。
具體是這句缺少-fPIC參數
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/share.d" -MT"src/share.d" -o "src/share.o" "../src/share.c"
需要加上-fPIC:
project->properties->c/c++ build -> setting->GCC C Compiler的commend中從原來的gcc 修改為gcc -fPIC
再次編譯通過。資訊如下:
**** Build of configuration Debug for project share ****
make all
Building file: ../src/share.c
Invoking: GCC C Compiler
gcc -fPIC -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/share.d" -MT"src/share.d" -o "src/share.o" "../src/share.c"
Finished building: ../src/share.c
Building target: libshare.so
Invoking: GCC C Linker
gcc -shared -o "libshare.so" ./src/share.o
Finished building target: libshare.so
**** Build Finished ****
二、調用編譯出來的.so檔案。
1、先在eclipse中建立一個可執行工程
File->New->Project->C Project,選擇Execuable Library,選擇empty project
2、建立工程的源檔案share.cpp
File->New->Source Folder,指定名稱為src
File->New->Source file,指定名稱為hello.c
3、複製share的標頭檔,因為需要引用shar.so的函數。編寫hello.c檔案
這裡在編譯的時候要引用到剛才建立的share.so共用庫:
project->properties->c/c++ build -> setting->GCC C Linker 中的libraries引入庫路徑和庫名稱
不然會出現找不到函數問題如下:
**** Build of configuration Debug for project hello ****
make all
Building target: hello
Invoking: GCC C Linker
gcc -o "hello" ./src/hello.o
./src/hello.o: In function `main':
/ws/jack/Android/projects/testPorject/hello/Debug/../src/hello.c:11: undefined reference to `sayhello'
collect2: ld 返回 1
make: *** [hello] 錯誤 1
**** Build Finished ****
三、運行工程
出錯。。。
/ws/jack/Android/projects/testPorject/hello/Debug/hello: error while loading shared libraries: libshare.so: cannot open shared object file: No such file or directory
找不到庫檔案。
原來lunix運行c/c++程式跑到預設路徑去找這個share庫檔案了,但是肯定找不到。因為沒把庫放到預設路徑裡面。
這有兩個辦法指定運行時候庫檔案的路徑:
一種,是設定系統的LD_LIBRARY_PATH 環境變數(在eclipse裡設定對外部正式啟動並執行程式無效)。
另外一種,是將動態連結程式庫檔案的路徑編譯進二進位可執行檔。-R+路徑。
我的就利用LD_LIBRARY_PATH運行環境變數去設定路徑了。設定成libshare.so的路徑。
project->run as ->run configurations->Environment
參考:
http://blog.sina.com.cn/s/blog_7769660f01011pf1.html
http://hi.baidu.com/sfzhaoif/item/32ecf6bfe055b542bb0e1209
http://stackoverflow.com/questions/14448343/cannot-open-shared-object-file-no-such-file-or-directory-running-or-debugging
http://www.linuxidc.com/Linux/2012-08/68828.htm
再次運行 ,就可以出來調用的結果了