当前位置:网站首页>C language learning -18-makefile file writing examples and how to generate and call dynamic libraries
C language learning -18-makefile file writing examples and how to generate and call dynamic libraries
2022-06-22 15:45:00 【Sunshine clover lxgzxj】
One 、 Environmental Science
| name | edition |
|---|---|
| operating system | Red Hat Enterprise Linux Server release 7.9 (Maipo) |
| CPU | 12th Gen Intel Core i7-12700H |
| Memory | 5G |
| gcc | 4.8.5 |
Two 、 File and directory structure
[[email protected] c_test]# ll
Total usage 12
drwxr-xr-x 2 root root 4096 6 month 21 15:34 exec
drwxr-xr-x 2 root root 23 6 month 21 14:16 include
drwxr-xr-x 2 root root 4096 4 month 7 15:17 leecode_src
drwxr-xr-x 2 root root 27 6 month 21 15:34 lib
drwxr-xr-x 2 root root 22 6 month 21 15:34 makefile
drwxr-xr-x 2 root root 23 6 month 21 15:34 temp
drwxr-xr-x 2 root root 4096 6 month 21 14:32 test_src
[[email protected] c_test]# ll include/FindMax.h
-rw-r--r-- 1 root root 90 6 month 21 14:16 include/FindMax.h
[[email protected] c_test]# ll test_src/main.c
-rw-r--r-- 1 root root 144 6 month 21 14:32 test_src/main.c
[[email protected] c_test]# ll test_src/FindMax.c
-rw-r--r-- 1 root root 416 6 month 21 14:14 test_src/FindMax.c
3、 ... and 、makefile Implementation content
(1) We need to FindMax.h、FindMax.c Generate binary points O file .
(2) Then according to the binary point O File generation libFindMax.so.
(3) According to libFindMax.so、main.c File generation executable file main.
Why should we have a little O file ?
To speed up compilation efficiency , If it is a big project , Need a lot of points C The composition of the document , It takes a long time to recompile each . If Mr. a little O file , At point C The document has not been changed , No need to recompile the generation point O Of documents , This greatly speeds up compilation time .
Four 、 Source code file
(1)FindMax.h
#ifndef FindMax_H
#define FindMax_H
extern int FindMax(int* Array, int ArraySize);
#endif
It is recommended to add conditional compilation , Avoid repeating statements .
(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;
}
In the examples of other people on the Internet, only function definitions and header file references , No addition main Functions and function declarations , I don't know what the hidden trouble is , There are no errors or warnings when compiling , I'm here just for the convenience of my own test , Hee hee .
(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
5、 ... and 、 environment variable
My side libFindMax.so The storage path of the dynamic library is /opt/c_test/lib, Everyone depends on the actual situation , Add this path to /etc/ld.so.conf The last line of the document , In execution /sbin/ldconfig -v Just 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
6、 ... and 、 test result
[[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

边栏推荐
- 快速排序quick_sort
- What does Alibaba cloud's cipu release mean for enterprise customers?
- ML笔记-matrix fundamental, Gradient Descent
- mysql的concat()函数如何用
- 打新债安全性有多高
- Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
- Common operations in Visual Studio development
- Scala语言学习-06-传名参数、传值参数、传函数参数的区别
- Devsecops: best practices for ci/cd pipeline security
- Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
猜你喜欢

What happened to those who didn't go to college

Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization

Common operations in Visual Studio development

On the routing tree of gin

得物App数据模拟平台的探索和实践

CVE-2022-0847(提权内核漏洞)

多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中

HMS Core新闻行业解决方案:让技术加上人文的温度

二分查找(整数二分)

DevSecOps: CI/CD 流水线安全的最佳实践
随机推荐
What are the five characteristics of network security? What are the five attributes?
大佬们好,初次使用MySQL cdc 报错
那些没考上大学的人,后来过的怎样
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
Exploration and practice of dewu app data simulation platform
Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express
mysql如何修改存储引擎为innodb
壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代
Reconstruction practice of complex C-end project of acquisition technology
Ask if you want to get the start of sqlserver_ Is there a good way for LSN?
New load balancing webclient CRUD
Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
Driving the efficient growth of the manufacturing industry, UFIDA u9 cloud is constantly improving the password behind it
Is it difficult for flush to open an account? Is it safe to open an account online?
Recommendation of individual visa free payment scheme
晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦
打新债安全性有多高
迷宫问题(BFS记录路径)
洛谷P2466 [SDOI2008] Sue 的小球 题解
Method of using inout signal in Verilog