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]$

 

转载于:https://www.cnblogs.com/StupidTortoise/p/3835884.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值