AIX下创建C++共享库
1.创建一个简单的so库文件,头文件和cpp文件如下:
test.h:
1 #ifndef __TEST_H__ 2 #define __TEST_H__ 3 4 #include <iostream> 5 6 class test 7 { 8 public: 9 int printHello(); 10 }; 11 12 #endif
test.cpp:
1 #include "test.h" 2 3 int test::printHello() 4 { 5 std::cout << "Hello, C++" << std::endl; 6 return 0; 7 }
编译命令:
[tortoise@sea temp]$ xlC_r -c test.cpp -o test.o [tortoise@sea temp]$ makeC++SharedLib test.o -p0 -o libTest.so [tortoise@sea temp]$ ls -l libTest.so -rwxrw---- 1 tortoise user 55250 2014-07-10 15:48 libTest.so [tortoise@sea temp]$ file libTest.so libTest.so: executable (RISC System/6000) or object module not stripped [tortoise@sea temp]$
2.在main函数里调用:
main.cpp:
1 #include "test.h" 2 3 int main() 4 { 5 test A; 6 A.printHello(); 7 return 0; 8 }
编译命令:
[tortoise@sea temp]$ xlC_r main.cpp -L. -lTest -o main ld: 0706-006 Cannot find or open library file: -l Test ld:open(): A file or directory in the path name does not exist. [tortoise@sea temp]$
libTest.so 文件命名在当前目录下,为何提示找不到呢?
经过搜索后,加上 -brtl 参数就可以了:
[tortoise@sea temp]$ xlC_r main.cpp -brtl -L. -lTest -o main [tortoise@sea temp]$ ls -l main -rwxrw---- 1 tortoise user 10177 2014-07-10 15:52 main [tortoise@sea temp]$ ldd main main needs: /data/app1/lich1/test/personal/libTest.so /usr/lib/libC.a(shr.o) /usr/ccs/lib/libpthreads.a(shr_xpg5.o) /usr/ccs/lib/libc.a(shr.o) /usr/ccs/lib/librtl.a(shr.o) /usr/lib/libC.a(ansi_32.o) /usr/lib/libC.a(ansicore_32.o) /usr/lib/libC.a(shrcore.o) /usr/ccs/lib/libpthreads.a(shr_comm.o) /unix /usr/lib/libcrypt.a(shr.o) /usr/lib/libC.a(shr2.o) /usr/lib/libC.a(shr3.o) [tortoise@sea temp]$ ./main Hello, C++ [tortoise@sea temp]$