2013年9月3日 星期二

How ot make .a and .so and use it

//refer to http://blog.yam.com/ytha/article/16636287

Generally, gcc is used to compile C language programs under Linux. By default, the compiler will only search for the standard C99 library.
Such as stdio.h, math.h, etc., while standard Linux system functions are usually stored in /lib and /usr/lib, So it is necessary to tell the compiler (linker) which function library to search, and the name of the function library under Linux always starts with lib,
Subsequent names are determined according to the library (m stands for math library), and the extension .a refers to the static library, and .so refers to the shared library.

//test.c
#define PI 3.14159265
int main ()
{
double param, result;
param = 88.0;
result = cos (param*PI/180);
printf ("The cosine of %lf degrees is %lf.\n", param, result );
return 0;
}

使用gcc編譯:
gcc -o test test.c -lm

math.h is the C99 standard library, here is just an example, if the program adds #include,
This -lm can be omitted, but if our program today is about processing system execution threads, It is necessary to add -lpthread to compile, because thread is not in the standard library of Xijiujiu.

The static library can exist independently, but it cannot be shared. It is often used in embedded systems, while the dynamic library cannot exist independently.
But it can be shared, and it must be placed under /lib to achieve sharing, so it is often used in PC software systems.
Generally, static functions save memory space, but dynamic functions save more space than static libraries when a large number of shared files are required.

Static library:
For example, we now have four files a.c, b.c, lib.h, main.c in a directory named mytest
Its contents are

//a.c
#include
void a(char * arg)
{
printf("a: you passed %s\n", arg);
}

//b.c
#include
void b(int arg)
{
printf("b: you passed %d\n", arg);
}

//lib.h
void a (char*);
void b (int);

//main.c
#include "lib.h"
int main()
{
a("Hello World");
exit(0);
}
好了, 開始編譯:
# cd mytest
# gcc -c a.c b.c
# ls *.o
您就會見到 a.o 及 b.o 兩個目的檔
再來
# gcc -c main.c 產生main.o的目的檔

現在我們來產生靜態函式庫:
# ar crv libtest.a a.o b.o 產生libtest.a的靜態函式庫

最後來使用這個函式庫來編譯出可執行檔:
# gcc -o main main.o libtest.a 這樣就產生出一個名為 main 的執行檔

我們也可以這樣寫來編出可執行檔
# gcc -o main main.o -L. -ltest
-L. 指目前所在目錄, 如果libtest.a是放在其他目錄下, 請在-L後面加上所在位置
-ltest 就是指 libtest.a

一樣用ldd指令可以查詢執行檔所參考的lib檔喔
# ldd main

如果對windows下面做比較的話:
Linux Windows
目的檔 func.o FUNC.OBJ
靜態函式庫 lib.a LIB.LIB
執行檔 main main.exe


動態函式庫: (我們採用上面的程式資料)
開始來做動態函式庫
# gcc a.c b.c -shared -o libtest.so //編譯a.c及b.c成一個libtest.so動態函式
# gcc -c main.c //產生 main.o目的檔
# gcc -o main main.o libtest.so //利用libtest.so動態函式來產生main可執行檔
# ./main 當你執行main執行檔的時後就出現了錯誤~

而錯誤訊息應該是這樣的:
./main: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
因為要共用喔(前面有說過)所以:
# cp libtest.so /lib //將libtest.so拷備到/lib目錄下

# export LD_LIBRARY_PATH=./
# ./main //再執行一次, 成功了吧~

#gedit /etc/ld.so.conf.d/newapp.conf
add /usr/local/newbuild/lib
#ldconfig -v

一樣用ldd指令可以查詢它的lib檔喔
# ldd main

沒有留言:

張貼留言