gcc编译-同时包含静态库和共享库

本文介绍了在GCC编译过程中,当静态库和共享库同名时,如何选择使用以及如何在编译程序中同时包含静态库和共享库。通过实例展示了删除静态库、保留共享库,以及编译calculate为静态库并用其与共享库libhello.so一起编译main的过程。
摘要由CSDN通过智能技术生成

1. 当在同一个目录下静态库和共享库同名时,共享库优先

hello.h头文件

#ifndef HELLO_H
#define HELLO_H
void print_hello();
#endif

hello.c源文件

#include "hello.h"
#include <stdio.h>
int main(int argc,char *argv[])
{
	printf("hello world!");
}

测试使用源文件main.c

#include "hello.h"
int main(int argc,char *argv[])
{
	printf_hello();
}

1.1 编译静态库和共享库

[test@hadoop hello]$ ls
hello.c  hello.h  main.c
[test@hadoop hello]$ gcc -c hello.c
[test@hadoop hello]$ ar crs libhello.a hello.o
[test@hadoop hello]$ gcc -shared -fPIC -o libhello.so hello.o
[test@hadoop hello]$ ls
hello.c  hello.h  hello.o  libhello.a  libhello.so  main.c

1.2 使用hello库编译 main.c


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值