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

本文详细介绍了使用gcc编译C语言程序,并生成可执行文件的过程,包括源文件的编译、链接和执行步骤,以及动态库的生成与调用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具:

  • 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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值