基于redhat64位操作系统编写C程序和动态库

来源:互联网 发布:hope软件下载 编辑:程序博客网 时间:2024/05/21 22:23

工具:

  • gedit Text Editor

下载吧

操作系统:red hat 6.4X64

示例代码

hello1.c

#include <stdio.h>int hello1(const char *str){    printf("hello word%d",1);    return 1;}

hello2.c

#include <stdio.h>int hello2(char *str){    printf("hello word2");}

main.c

#include "hello1.h"#include "hello2.h"int main(){    hello1("hello1");    hello2("hello2");    return 1;}

现在对以上三个文件进行编译
生成应用程序
同时编译
gcc -c hello1.c hello2.c main.c
生成可执行文件
gcc main.o hello1.o hello2.o -o main
对于cpp文件命令:
g++ main.o hello1.o hello2.o -o main
执行可执行文件 ./main
生成动态库
动态库变译和普通的可执行文件的变译不一致,需要添加-fPIC参数
编译
gcc -fPIC -c hello1.c hello2.c
“PIC”命令行标记告诉GCC产生的代码不要包含对函数和变量具体内存位置的引用
生成动态库
gcc -shared -o libmyhello.so hello1.o hello2.o
通过mian.c调用动态库进行测试
gcc -o mainsoft main.c -L. -lmyhello2
mainsoft:生成的应用名称
-L :用于连接动态库 libmyhello.so ,标记告诉GCC函数库可能位于当前目录
执行应用:./miansoft

第一次写博客,请大家多多关照,如果有什么错误,希望大家能指出来

0 0
原创粉丝点击