当前位置:网站首页>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

边栏推荐
- Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
- visual studio开发过程中常见操作
- 大佬们 2.2.1cdc 监控sqlsever 只能拿到全量的数据 后期增量的数据拿不到 咋回事啊
- Those confusing user state & kernel state
- Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
- 【newman】postman生成漂亮的测试报告
- Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
- [graduation project] Research on emotion analysis based on semi supervised learning and integrated learning
- 【题目精刷】2023禾赛-FPGA
- 个人免签支付方案推荐
猜你喜欢

好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践

数据资产管理:数据发现,发现什么,怎么发现?

I rely on the sideline to buy a house in full one year: the industry you despise will make a lot of money in the next ten years!

ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点

得物技术复杂 C 端项目的重构实践

口令安全是什么意思?等保2.0政策中口令安全标准条款有哪些?

华为机器学习服务银行卡识别功能,一键实现银行卡识别与绑定

架构师之路,从「存储选型」起步

KEIL仿真和vspd

NF-ResNet:去掉BN归一化,值得细读的网络信号分析 | ICLR 2021
随机推荐
FreeRTOS task priority and interrupt priority
PHP built-in protocols (supported and encapsulated protocols)
Is SQL analysis query unavailable in the basic version?
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
向量6(继承)
网络安全的五大特点有哪些?五大属性是什么?
Database connection pool: stress testing
Bochs software usage record
个人免签支付方案推荐
数据库连接池:压力测试
PowerPoint tutorial, how to add watermarks in PowerPoint?
口令安全是什么意思?等保2.0政策中口令安全标准条款有哪些?
The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
Using virtual serial port to debug serial port in keil MDK
U++ iterative Sorting Query learning notes
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
数据库连接池:连接池功能点的实现
What happened to those who didn't go to college
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
Mobile learning notes of u++ programming