AIX 下通常用xlC来编译c++代码,例如我写了一个string的类,包含俩文件 string.h 和 string.cpp
现在我想将其编译成动态链接库(.so文件) 步骤为:
1.
$(CC) -q64 -o libstring.o -c libstring.cpp $(CFLAGS) -I$(HOME)/include -L$(LD_LIBRARY_PATH)
2.
ld -v -G -o libstring.so libstring.o common.o -b64 -bexpall -bnoentry
AIX下.so文件最好用 libxxx.so的方式来命名
3.使用动态库:
$(CC) -q64 -brtl -o libstring.o -c libstring.cpp $(CFLAGS) -I$(HOME)/include -L$(LD_LIBRARY_PATH) -lstring
-brtl表示: Enables run-time linking for the output file. DCE thread libraries and heap debug libraries are not compatible with run-time linking. Do not specify the -brtl compiler option if you are invoking the compiler with xlc_r4 or xlC_r4, or if the -qheapdebug compiler option is specified.
就是对于输出的可执行文件,启动运行时动态链接,在加入这项时,连接器会在可执行文件和.so文件之间建立一个签名(signature),用来说明他们俩的接口,而.so文件的搜寻和载入则是由OS来负责的。
-L说明.so文件或者.a文件(静态链接库)所在的path,-l说明链接库的名称,比如我们写 -lxxx ,连接器会去搜寻 libxxx.so 或者 libxxx.a 文件来进行处理。