栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言学习-18-makefile文件编写例子以及如何生成、调用动态库

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言学习-18-makefile文件编写例子以及如何生成、调用动态库

一、环境
名称版本
操作系统Red Hat Enterprise Linux Server release 7.9 (Maipo)
CPU12th Gen Intel® Core™ i7-12700H
内存5G
gcc4.8.5
二、文件及目录结构
[root@node0 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

[root@node0 c_test]# ll include/FindMax.h 
-rw-r--r-- 1 root root 90 6月  21 14:16 include/FindMax.h

[root@node0 c_test]# ll test_src/main.c 
-rw-r--r-- 1 root root 144 6月  21 14:32 test_src/main.c

[root@node0 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 
#include 

extern int FindMax(int* Array, int ArraySize);

int main()
{
    int Array[] = {1,2,3,4,5};
    printf("max : %dn",FindMax(Array,5));
    return 0;
}

extern int FindMax(int* Array, int ArraySize)
{
    int max = Array[0];
    int i;
    
    for(i=1; i
        if(max < Array[i])
        {
            max = Array[i];
        }
    }
    return max;
}

我看网上其他人的例子中只有函数定义和头文件引用,没有加main函数和函数声明这些,我暂时不知道有什么隐患,编译时是没有任何报错与警告的,我这边只是为了方便自己测试,嘻嘻。

(3)main.c

#include 
#include 

int main()
{
    int Array[] = {1,2,3,4,5};
    printf("max : %dn",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

SRC_PATH = ../test_src/
EXEC_PATH = ../exec/
TEMP_PATH = ../temp/
INCLUDE_PATH = ../include/
LIB_PATH = ../lib/

INCLUDE_COMMAND = -I
LIB_COMMAND = -L
LIB_NAME = -l
RM_COMM = rm -rf

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了

[root@node0 makefile]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/openssl/lib
/opt/c_test/lib
[root@node0 makefile]# 
[root@node0 makefile]# 
[root@node0 makefile]# /sbin/ldconfig -v
六、测试结果
[root@node0 makefile]# make clean
rm -rf ../exec/main ../temp/FindMax.o ../lib/libFindMax.so
[root@node0 makefile]# 
[root@node0 makefile]# 
[root@node0 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 
[root@node0 makefile]# 
[root@node0 makefile]# 
[root@node0 makefile]# ../exec/main 
max : 5

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982105.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号