您的位置:首页 > 运维架构 > Linux

【linux】 静态库编译

2016-04-28 16:15 549 查看
文件如下:

root@ubuntu:/home/test# ll
total 72
drwxr-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_H

void caller();

#endif
root@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_H

void func_api();

#endif
root@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 0
int main()
{
funcapi();
return 0;
}
#endif
root@ubuntu:/home/test# cat libsystemapi.h
#ifndef SYSTEM_API_H
#define SYSTEM_API_H

void system_api();

#endif
root@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 -f

OBJS = $(CUR_DIR)/*.o
LIB_DIR = $(CUR_DIR)/lib

all: pre_work libsystemapi.a libfuncapi.a libcaller.a test

libsystemapi.a:
g++ -c libsystem.cpp -o libsystem.o
ar cr $@ libsystem.o

libfuncapi.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 exe

pre_work:
mkdir -p $(LIB_DIR)

post_work:
mv ./*.a $(LIB_DIR)/

clean:
$(RM) $(OBJS) $(LIB_DIR)/*
root@ubuntu:/home/test#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: