程式原始碼經過編譯器和組譯器後產生目的檔,
接著透過連結器(Linker) 產生可執行檔。
Object files:Linux(.o), Windows(.obj)
Executables:/bin/bash檔
Shared libraries:Linux(.so), Windows(.dll)
Core dumps
以上皆屬於ELF檔案格式。
ELF檔案格式將上述symbol table, debugging information等等資訊
以”Section”形式儲存,如”.text”, “.data”, “.bss”等等。
範例:
a.h
#ifndef _a_h #define _a_h #include class a_class1{ public: int member; a_class1(){ member = 100; } }; extern class a_class1 object1; void function1(); extern int a_var1; #endif
a.cpp
#include "a.h" class a_class1 object1; int a_var1 = 1; void function1(){ printf("a.cpp a_var1 = %d\n",a_var1); }
main.cpp
#include "a.h" void function2(){ printf("main.cpp a_var1 = %d\n",a_var1); } int main(int argc, char *argv[]) { function1(); function2(); printf("main.cpp object1.member = %d\n",object1.member); }
執行結果
a.cpp a_var1 = 1
main.cpp a_var1 = 1
main.cpp object1.member = 100
1.直接編
hwchen18546@ubuntu:~/obj$ g++ main.cpp a.cpp b.cpp -o output1
2.obj file
o,是目標檔(尚未連結的中間檔案),相當於windows中的.obj文件
hwchen18546@ubuntu:~/obj$ g++ -c b.cpp (-o b.o)
hwchen18546@ubuntu:~/obj$ g++ -c a.cpp (-o a.o)
hwchen18546@ubuntu:~/obj$ g++ main.cpp -o output2 a.o b.o
3.static lib
A static library(.a) is a library that can be linked directly into
the final executable produced by the linker,
it is contained in it and there is no need to have the library into
the system where the executable will be deployed.
(.a為靜態庫,是好多個.o合在一起,用於靜態連接)
-r: 新增 fun.o 到 libfun.a 中
-c: 建立新的程式庫
-s: 將一個 object 檔的 index 寫入程式庫
hwchen18546@ubuntu:~/obj$ ar rcs libcool.a a.o b.o //archiver,程式庫名稱一定要加lib
hwchen18546@ubuntu:~/obj$ g++ main.cpp -o output3 -L. -lcool // -L程式庫位置 -l程式庫名稱
例如你想把自己提供的函數給別人使用,不過又想對函數的原始碼進行保密,你就
能給別人提供一個靜態函數庫檔。
4.shared lib
A shared library(.so) is a library that is linked
but not embedded in the final executable,
so will be loaded when the executable is launched and
need to be present in the system where the executable is deployed.
(.so 為共用庫,是shared object,用於動態連接的,和dll差不多)
hwchen18546@ubuntu:~/obj$ g++ a.cpp b.cpp -fPIC -shared -o libcool2.so
-fPIC 表示編譯為位置獨立的代碼(position-independent code)
shared 該選項指定生成動態連接庫
hwchen18546@ubuntu:~/obj$ g++ main.cpp -L. -lcool2 -o output4
執行output4會遇到error因為沒有指定so路徑
error while loading shared libraries: libcool2.so:
cannot open shared object file: No such file or directory
hwchen18546@ubuntu:~/obj$ ldd output4
linux-gate.so.1 => (0xb7761000)
libcool2.so => not found
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75a6000)
/lib/ld-linux.so.2 (0xb7762000)
解法
hwchen18546@ubuntu:~/obj$ sudo cp libcool2.so /usr/lib/
或者設定 /etc/ld.so.conf ,加入一個新的library搜尋目錄,
並執行ldconfig更新快取
A dynamic link library on windows(.dll) is like a shared library(.so) on linux
but there are some differences between the two implementations that are related to the OS (Windows vs Linux) :
A DLL can define two kinds of functions: exported and internal.
The exported functions are intended to be called by other modules,
as well as from within the DLL where they are defined.
Internal functions are typically intended to be called
only from within the DLL where they are defined.
An SO library on Linux doesn’t need special export statement to indicate exportable symbols,
since all symbols are available to an interrogating process.
reference
http://seansylo.blogspot.tw/2012/09/linuxo-so-la.html
http://tapobuzz.blogspot.tw/2010/06/object-file-format.html
http://blog.xuite.net/csiewap/cc/23626229-Using+GCC+to+create+static+and+shared+library+.so
http://mqjing.blogspot.tw/2009/04/c-gcc-library.html