当前位置:网站首页>C语言学习-18-makefile文件编写例子以及如何生成、调用动态库
C语言学习-18-makefile文件编写例子以及如何生成、调用动态库
2022-06-22 14:28:00 【阳光九叶草LXGZXJ】
一、环境
| 名称 | 版本 |
|---|---|
| 操作系统 | Red Hat Enterprise Linux Server release 7.9 (Maipo) |
| CPU | 12th Gen Intel Core i7-12700H |
| 内存 | 5G |
| gcc | 4.8.5 |
二、文件及目录结构
[[email protected] c_test]# ll
总用量 12
drwxr-xr-x 2 root root 4096 6月 21 15:34 exec
drwxr-xr-x 2 root root 23 6月 21 14:16 include
drwxr-xr-x 2 root root 4096 4月 7 15:17 leecode_src
drwxr-xr-x 2 root root 27 6月 21 15:34 lib
drwxr-xr-x 2 root root 22 6月 21 15:34 makefile
drwxr-xr-x 2 root root 23 6月 21 15:34 temp
drwxr-xr-x 2 root root 4096 6月 21 14:32 test_src
[[email protected] c_test]# ll include/FindMax.h
-rw-r--r-- 1 root root 90 6月 21 14:16 include/FindMax.h
[[email protected] c_test]# ll test_src/main.c
-rw-r--r-- 1 root root 144 6月 21 14:32 test_src/main.c
[[email protected] c_test]# ll test_src/FindMax.c
-rw-r--r-- 1 root root 416 6月 21 14:14 test_src/FindMax.c
三、makefile实现内容
(1)我们需要根据FindMax.h、FindMax.c生成二进制点O文件。
(2)再根据二进制点O文件生成libFindMax.so。
(3)再根据libFindMax.so、main.c文件生成可执行文件main。
为什么我们要先生成点O文件?
为了加快编译效率,如果是一个大工程,需要很多点C文件组成,每个都重新编译时间漫长。如果先生成点O文件,在点C文件没有改动的情况下,是不需要重新编译生成点O文件的,这样大大加快了编译时间。
四、源代码文件
(1)FindMax.h
#ifndef FindMax_H
#define FindMax_H
extern int FindMax(int* Array, int ArraySize);
#endif
建议加上条件编译,避免声明重复的问题。
(2)FindMax.c
#include <stdio.h>
#include <FindMax.h>
extern int FindMax(int* Array, int ArraySize);
int main()
{
int Array[] = {
1,2,3,4,5};
printf("max : %d\n",FindMax(Array,5));
return 0;
}
extern int FindMax(int* Array, int ArraySize)
{
int max = Array[0];
int i;
for(i=1; i<ArraySize; i++)
{
if(max < Array[i])
{
max = Array[i];
}
}
return max;
}
我看网上其他人的例子中只有函数定义和头文件引用,没有加main函数和函数声明这些,我暂时不知道有什么隐患,编译时是没有任何报错与警告的,我这边只是为了方便自己测试,嘻嘻。
(3)main.c
#include <stdio.h>
#include <FindMax.h>
int main()
{
int Array[] = {
1,2,3,4,5};
printf("max : %d\n",FindMax(Array,5));
return 0;
}
(4) makefile
CC = gcc
CFLAG_O = -c -Wall -fpic -O3
CFLAG_SO = -shared
CFLAG_EXEC = -Wall -O3
CFLAG_ALIAS = -o
RM_COMM = rm -rf
SRC_PATH = ../test_src/
EXEC_PATH = ../exec/
TEMP_PATH = ../temp/
INCLUDE_COMMAND = -I
LIB_COMMAND = -L
INCLUDE_PATH = ../include/
LIB_PATH = ../lib/
LIB_NAME = -l
all : main
main : libFindMax.so
$(CC) $(CFLAG_EXEC) $(CFLAG_ALIAS) $(EXEC_PATH)main $(SRC_PATH)main.c $(INCLUDE_COMMAND) $(INCLUDE_PATH) $(LIB_COMMAND) $(LIB_PATH) $(LIB_NAME)FindMax
libFindMax.so : FindMax.o
$(CC) $(CFLAG_SO) $(TEMP_PATH)FindMax.o $(CFLAG_ALIAS) $(LIB_PATH)libFindMax.so
FindMax.o :
$(CC) $(CFLAG_O) $(SRC_PATH)FindMax.c $(INCLUDE_COMMAND) $(INCLUDE_PATH) $(CFLAG_ALIAS) $(TEMP_PATH)FindMax.o
clean :
$(RM_COMM) $(EXEC_PATH)main $(TEMP_PATH)FindMax.o $(LIB_PATH)libFindMax.so
五、环境变量
我这边libFindMax.so动态库的存放路径在/opt/c_test/lib,每个人根据实际情况而定哦,把这个路径添加到/etc/ld.so.conf文件的最后一行,在执行/sbin/ldconfig -v就ok了
[[email protected] makefile]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/openssl/lib
/opt/c_test/lib
[[email protected] makefile]#
[[email protected] makefile]#
[[email protected] makefile]# /sbin/ldconfig -v
六、测试结果
[[email protected] makefile]# make clean
rm -rf ../exec/main ../temp/FindMax.o ../lib/libFindMax.so
[[email protected] makefile]#
[[email protected] makefile]#
[[email protected] makefile]# make
gcc -c -Wall -fpic -O3 ../test_src/FindMax.c -I ../include/ -o ../temp/FindMax.o
gcc -shared ../temp/FindMax.o -o ../lib/libFindMax.so
gcc -Wall -O3 -o ../exec/main ../test_src/main.c -I ../include/ -L ../lib/ -lFindMax
[[email protected] makefile]#
[[email protected] makefile]#
[[email protected] makefile]# ../exec/main
max : 5

边栏推荐
- I took a private job and earned 15250. Is it still necessary to do my main business?
- 那些没考上大学的人,后来过的怎样
- How to use the concat() function of MySQL
- Keil simulation and VSPD
- "Forget to learn again" shell process control - 38. Introduction to while loop and until loop
- ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
- 向量3(静态成员)
- The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
- Phpstudy 2016 build Pikachu range
- 华为机器学习服务银行卡识别功能,一键实现银行卡识别与绑定
猜你喜欢

Method of using inout signal in Verilog

New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
![[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide

Please, don't be brainwashed. This is the living reality of 90% of Chinese people

那些令人懵逼的用户态&内核态

UE4 obtains local files through blueprints
米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)
![[Zhejiang University] information sharing of the first and second postgraduate entrance examinations](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Zhejiang University] information sharing of the first and second postgraduate entrance examinations

Live broadcast goes to sea | domestic live broadcast room produces explosive products again. How can "roll out" win the world

好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践
随机推荐
[Zhejiang University] information sharing of the first and second postgraduate entrance examinations
向量6(继承)
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
Is SQL analysis query unavailable in the basic version?
What happened to those who didn't go to college
晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦
封装api时候token的处理
历时100天、小鱼搭建了个机器人交流社区!!现公开邀请版主中!
大佬们 2.2.1cdc 监控sqlsever 只能拿到全量的数据 后期增量的数据拿不到 咋回事啊
Keil simulation and VSPD
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
mysql如何修改存储引擎为innodb
Can Google bidding account detect the global market?
快速排序quick_sort
U++ operator learning notes
那些令人懵逼的用户态&内核态
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
FPGA采集DHT11温湿度
Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan
新版负载均衡WebClient CRUD