| 名称 | 版本 |
|---|---|
| 操作系统 | Red Hat Enterprise Linux Server release 7.9 (Maipo) |
| CPU | 12th Gen Intel® Core™ i7-12700H |
| 内存 | 5G |
| gcc | 4.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



