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 }
编译命令:
[[email protected] temp]$ xlC_r -c test.cpp -o test.o [[email protected] temp]$ makeC++SharedLib test.o -p0 -o libTest.so [[email protected] temp]$ ls -l libTest.so -rwxrw---- 1 tortoise user 55250 2014-07-10 15:48 libTest.so [[email protected] temp]$ file libTest.so libTest.so: executable (RISC System/6000) or object module not stripped [[email protected] temp]$
2.在main函数里调用:
main.cpp:
1 #include "test.h" 2 3 int main() 4 { 5 test A; 6 A.printHello(); 7 return 0; 8 }
编译命令:
[[email protected] 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. [[email protected] temp]$
libTest.so 文件命名在当前目录下,为何提示找不到呢?
经过搜索后,加上 -brtl 参数就可以了:
[[email protected] temp]$ xlC_r main.cpp -brtl -L. -lTest -o main [[email protected] temp]$ ls -l main -rwxrw---- 1 tortoise user 10177 2014-07-10 15:52 main [[email protected] 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) [[email protected] temp]$ ./main Hello, C++ [[email protected] temp]$
AIX下创建C++共享库
时间: 2024-11-02 09:31:34