ITPub博客

首页 > 数据库 > 数据库开发技术 > 【新炬网络名师大讲堂】在AIX机器上使用xlc编译c的动态库

【新炬网络名师大讲堂】在AIX机器上使用xlc编译c的动态库

原创 数据库开发技术 作者:shsnchyw 时间:2014-12-09 17:43:04 0 删除 编辑

一、编译路径:
[lzj@239: ~/cpp/lib_test]$ pwd
/home/lzj/cpp/lib_test

二、初始文件:
[lzj@239: ~/cpp/lib_test]$ ls
hello.c hello.h main.c

[lzj@239: ~/cpp/lib_test]$ cat hello.c
#include
void hello(const char *name)
{
printf(“Hello %s!\n”, name);
}

[lzj@239: ~/cpp/lib_test]$ cat hello.h
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif //HELLO_H

[lzj@239: ~/cpp/lib_test]$ cat main.c
#include “hello.h”
int main()
{
hello(“everyone”);
return 0;
}

三、编译步骤如下:
1.编译hello.c的文件生成hell.o文件:
[lzj@239: ~/cpp/lib_test]$ xlc -c hello.c
结果:
[lzj@239: ~/cpp/lib_test]$ ls
hello.c hello.h hello.o main.c

2.编译libmyhello.so动态库:
[lzj@239: ~/cpp/lib_test]$xlc -qmkshrobj -o libmyhello.so hello.o
或 xlc -G -o libmyhello.so hello.o
结果:
[lzj@239: ~/cpp/lib_test]$ ls
hello.c hello.h hello.o libmyhello.so main.c

3.编译main.c成可执行程序hello:
[lzj@239: ~/cpp/lib_test]$xlc -brtl -o hello main.c -lmyhello -L.
结果:
[lzj@239: ~/cpp/lib_test]$ ls
hello hello.c hello.h hello.o libmyhello.so main.c

4.hello程序执行结果:
[lzj@239: ~/cpp/lib_test]$ hello
Hello everyone!

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29960155/viewspace-1362346/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-10-16

  • 博文量
    147
  • 访问量
    268247