首页 > 代码库 > 【linux】 静态库编译

【linux】 静态库编译

文件如下:

root@ubuntu:/home/test# ll total 72drwxr-xr-x 3 root root 4096 Sep  2 10:20 ./drwxr-xr-x 4 root root 4096 Sep  2 08:31 ../-rwxr-xr-x 1 root root 7901 Sep  2 10:09 a.out*-rwxr-xr-x 1 root root 7901 Sep  2 10:16 exe*drwxr-xr-x 2 root root 4096 Sep  2 10:14 lib/-rwxrwxrwx 1 root root  149 Sep  2 10:00 libcaller.cpp*-rw-r--r-- 1 root root   66 Sep  2 09:35 libcaller.h-rw-r--r-- 1 root root 1184 Sep  2 10:14 libcaller.o-rwxrwxrwx 1 root root  232 Sep  2 10:07 libfuncapi.cpp*-rw-r--r-- 1 root root   64 Sep  2 10:05 libfuncapi.h-rw-r--r-- 1 root root 1196 Sep  2 10:14 libfuncapi.o-rw-r--r-- 1 root root   70 Sep  2 09:56 libsystemapi.h-rwxrwxrwx 1 root root  116 Sep  2 08:44 libsystem.cpp*-rw-r--r-- 1 root root 1156 Sep  2 10:14 libsystem.o-rw-r--r-- 1 root root  590 Sep  2 10:14 makefile-rwxrwxrwx 1 root root   82 Sep  2 09:37 test.cpp*root@ubuntu:/home/test# 

文件内容分别如下:

root@ubuntu:/home/test# cat test.cpp #include <iostream>extern void caller();int main(){        caller();        return 0;}root@ubuntu:/home/test# cat libcaller.h #ifndef LIB_CALLER_H#define LIB_CALLER_Hvoid caller();#endifroot@ubuntu:/home/test# cat libcaller.cpp#include <stdio.h>#include "libfuncapi.h"void caller(){        printf("this is %s @ %s:%d.", __FUNCTION__,__FILE__,__LINE__);        func_api();        return;}root@ubuntu:/home/test# cat libfuncapi.h#ifndef FUNC_API_H#define FUNC_API_Hvoid func_api();#endifroot@ubuntu:/home/test# cat libfuncapi.cpp#include <stdio.h>#include "libsystemapi.h"#include "libfuncapi.h"void func_api(){        printf("this is %s @ %s:%d.\n", __FUNCTION__,__FILE__,__LINE__);        system_api();        return;}#if 0int main(){        funcapi();        return 0;}#endif     root@ubuntu:/home/test# cat libsystemapi.h #ifndef SYSTEM_API_H#define SYSTEM_API_Hvoid system_api();#endifroot@ubuntu:/home/test# cat libsystem.cpp #include <stdio.h>void system_api(){        printf("this is %s @ %s:%d.", __FUNCTION__,__FILE__,__LINE__);        return;}root@ubuntu:/home/test# 

makefile:

root@ubuntu:/home/test# root@ubuntu:/home/test# cat makefile CUR_DIR=$(PWD)RM = rm -fOBJS = $(CUR_DIR)/*.oLIB_DIR = $(CUR_DIR)/liball: pre_work libsystemapi.a libfuncapi.a libcaller.a testlibsystemapi.a:        g++ -c libsystem.cpp -o libsystem.o        ar cr $@ libsystem.olibfuncapi.a:libsystem.o        g++ -c libfuncapi.cpp -o libfuncapi.o        ar cr $@ libfuncapi.o $^libcaller.a:libfuncapi.o libsystem.o        g++ -c libcaller.cpp -o libcaller.o         ar cr $@ libcaller.o $^test: post_work $(LIB_DIR)/libcaller.a        g++ test.cpp $(LIB_DIR)/libcaller.a -o exepre_work:        mkdir -p $(LIB_DIR)post_work:        mv ./*.a $(LIB_DIR)/clean:        $(RM) $(OBJS) $(LIB_DIR)/*root@ubuntu:/home/test# 

 

【linux】 静态库编译