找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1491|回复: 0

Makefile 编译静态库文件及链接静态库

[复制链接]

96

主题

6

回帖

108

积分

屌炸天

积分
108
发表于 2017-9-26 18:23:49 | 显示全部楼层 |阅读模式
1.代码目录结构如下:   
[url=][/url]
1 comm/errorhandler.c
2 comm/inc/apue.h
3
atexit.c 4 Makefile
5
6 staticlib/lib/ 7 staticlib/inc/staticlibs.h 8 staticlib/staticlib_add.c 9 staticlib/staticlib_mul.c10 staticlib/Makefile[url=][/url]

2.目录 staticlib/lib/ 是用来盛放静态库文件的——libmytest.a,在编译静态库之前该目录是空的
    staticlib/inc/staticlibs.h 头文件内容如下:
[url=][/url]
1 #ifndef __staticlibs_h
2 #define __staticlibs_h

3 int static_lib_func_add(int i1, int i2);4 int static_lib_func_mul(int i1, int i2);

5 #endif
[url=][/url]

    staticlib/staticlib_add.c 文件内容如下:
[url=][/url]
1 #include "apue.h"2 int static_lib_func_add(int i1, int i2) 3 {4     int iret = i1 + i2; 5     printf("in a static library, return value %d\n", iret);6     return iret;7 }[url=][/url]

    staticlib/staticlib_mul.c 文件内容如下:
[url=][/url]
1 #include "apue.h"2 3 int static_lib_func_mul(int i1, int i2) 4 {5     int iret = i1 * i2; 6     printf("in a static library, return value is %d\n", iret);7     return iret;8 }[url=][/url]

    staticlib/Makefile 文件内容如下:
[url=][/url]
1 CC        = gcc 2 CFLAGS    = -Wall -O -g 3 CXXFLAGS  = 4 INCLUDE   = -I ./inc -I ../comm/inc 5 TARGET    = libmytest.a 6 LIBPATH   = ./lib/ 7 8 vpath %.h ./inc 9 10 OBJS      = staticlib_add.o staticlib_mul.o11 SRCS      = staticlib_add.c staticlib_mul.c12 13 $(OBJS)(SRCS)14    $(CC) $(CFLAGS) $(INCLUDE) -c $^15 16 all(OBJS)17    ar rcs $(TARGET) $^          打包 .o 文件到库文件 libmytest.a 18    mv $(TARGET) $(LIBPATH)19 20 clean:21    rm -f *.o22    rm -f $(LIBPATH)*[url=][/url]

3.文件 atexit.c 的内容如下:
[url=][/url]
1 #include "apue.h" 2 #include "staticlibs.h"   //包含静态库的头文件 3 4 static void my_exit1(void); 5 static void my_exit2(void); 6 7 int main(void) 8 { 9     static_lib_func_add(1, 9); //静态库函数10     static_lib_func_mul(1, 9); //静态库函数11 12     if(0 != atexit(my_exit2))13         err_sys("can't register my_exit2");14     if(0 != atexit(my_exit1))15         err_sys("can't register my_exit1");16     if(0 != atexit(my_exit1))17         err_sys("can't register my_exit1");18     printf("main is done\n");19     return 0;20 }21 22 static void my_exit1(void)23 {24     printf("first exit handler\n");25 }26 27 static void my_exit2()28 {29     printf("second exit handler\n");30 }[url=][/url]

    文件 Makefile 的内容如下:
[url=][/url]
1 CC       = gcc 2 CFLAGS   = -Wall -O -g 3 CXXFLAGS = 4 INCLUDE  = -I ./comm/inc -I ./staticlib/inc 5 TARGET   = atexit 6 LIBVAR   = -lmytest   链接 libmytest.a 7 LIBPATH  = -L./staticlib/lib 8 #search paths for errorhandler.c 9 vpath %.c ./comm10 #下行是为依赖项 apue.h 准备的,比如 [errorhandler.o:errorhandler.c apue.h] 里的 apue.h11 vpath %.h ./comm/inc12 13 OBJS     = errorhandler.o atexit.o14 #下行的 apue.h,可以不必写出来15 errorhandler.o:errorhandler.c apue.h16    $(CC) $(CFLAGS) $(INCLUDE) -c $^17 atexit.o:atexit.c apue.h18    $(CC) $(CFLAGS) $(INCLUDE) -c $^19 20 all(OBJS) $(LIB)21    cd ./staticlib && make all    执行staticlib/Makefile 里的 make all22    $(CC) $(CFLAGS) $(INCLUDE) -o $(TARGET) $(OBJS) $(LIBVAR) $(LIBPATH)23 24 clean:25    rm -f *.o26    rm -f $(TARGET)27    cd ./staticlib && make clean   执行staticlib/Makefile 里的 make clean[url=][/url]

  我在编译的过程中出现了 /usr/bin/ld: cannot find -lc,然后在系统上 locate libc.a,发现系统上没有 libc.a 静态库文件,
  这是由于我的系统还没安装glibc的静态版本,安装 glibc-static-2.12-1.107.el6.x86_64.rpm 之后,就不再出现该问题。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Comsenz Inc. ( 粤ICP备15088888号-2 )

GMT+8, 2024-4-20 21:05 , Processed in 0.123633 second(s), 10 queries , File On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表