Ubuntu下makefile及gcc生成静态库动态库的简单使用举例
環(huán)境:Ubuntu-13.10? 32位(虛擬機(jī))、gcc4.8.1
首先創(chuàng)建一個(gè)test_makefile_gcc文件夾,此test_makefile_gcc文件夾下包括:src文件夾用于存放源文件; include文件夾用于存放頭文件;bin文件夾用于存放生成的動(dòng)態(tài)庫(kù).so文件;lib文件夾用于存放生成的靜態(tài)庫(kù).a文件;project_makefile文件夾存放此工程的makefile文件;test文件夾存放用來(lái)測(cè)試靜態(tài)庫(kù)和動(dòng)態(tài)庫(kù)的源文件;另外在test_makefile_gcc文件夾下還包含一個(gè)makefile文件。
include文件夾下包括add.h、divide.h、hybrid.h、multiply.h、subtract.h:
add.h文件內(nèi)容為:
int CalAdd(int a, int b);
divide.h文件內(nèi)容為:
int CalDivide(int a, int b);
hybrid.h文件內(nèi)容為:
int CalHybrid(int a, int b, int c, int d);
multiply.h文件內(nèi)容為:
int CalMultiply(int a, int b);
subtract.h文件內(nèi)容為:
int CalSubtract(int a, int b);
src文件夾下包括add.c、divide.cpp、hybrid.cpp、multiply.cpp、subtract.c:
add.c文件內(nèi)容為:
#include "add.h"int CalAdd(int a, int b)
{return a + b;
}
divide.cpp文件內(nèi)容為:
#include "divide.h"int CalDivide(int a, int b)
{return a / b;
}
hybrid.cpp文件內(nèi)容為:
#include "hybrid.h"extern "C" {
#include "add.h"
#include "subtract.h"
}
#include "multiply.h"
#include "divide.h"int CalHybrid(int a, int b, int c, int d)
{int tmp1=0, tmp2=0, tmp3=0, tmp4=0, result=0;tmp1 = CalDivide(a, b);tmp2 = CalMultiply(c, d);tmp3 = CalAdd(tmp1, tmp2);tmp4 = CalSubtract(tmp2, tmp1);result = CalAdd(tmp3, tmp4);return result;
}
multiply.cpp文件內(nèi)容為:
#include "multiply.h"int CalMultiply(int a, int b)
{return a * b;
}
subtract.c文件內(nèi)容為:
#include "subtract.h"int CalSubtract(int a, int b)
{return a - b;
}
project_makefile文件夾中makefile內(nèi)容為:
add_cflags = -I../includecxxsources = \../src/divide.cpp \../src/multiply.cpp \../src/hybrid.cppcsources = \../src/add.c \../src/subtract.csources = $(cxxsources) $(csources)include ../makefile
test_makefile_gcc文件夾中的makefile文件內(nèi)容為:
debug: $(sources)gcc -c -fPIC -ggdb $(add_cflags) $(sources)release: $(sources)gcc -c -Os -fPIC $(add_cflags) $(sources)
test文件夾中test_makefile_gcc.cpp文件的內(nèi)容為:
#include "../include/hybrid.h"
#include <iostream>using namespace std;int main(int argc, char* argv[])
{int a=10, b=200, c=-34, d=92;int result = CalHybrid(a, b, c, d);cout<<result<<endl;return 0;
}
詳細(xì)操作步驟:
1、將終端定位到project_makefile文件夾下,執(zhí)行命令: make debug ;在project_makefile文件夾下生成add.o、divide.o、hybrid.o、multiply.o、subtract.o文件;
2、在lib文件夾下生成靜態(tài)庫(kù)libtest[linux_dbg_32].a,執(zhí)行命令:ar?-r?../lib/libtest[linux_dbg_32].a ?*.o ;
3、將終端定位到test文件夾,執(zhí)行命令:g++ -o test?test_makefile_gcc.cpp ?-L ?../lib ?-ltest[linux_dbg_32] ,生成test執(zhí)行文件;
4、執(zhí)行命令:./test ,輸出結(jié)果:-6256,Debug靜態(tài)庫(kù)的調(diào)用完成;
5、將終端重新定位到project_makefile文件夾下,執(zhí)行命令:gcc ?-shared ?-o ../bin/libtest[linux_dbg_32].so ?*.o ,在bin文件夾下生成libtest[linux_dbg_32].so ;
6、執(zhí)行命令:g++ ?-o ?../test/test2 ?../test/test_makefile_gcc.cpp -L ?../bin -ltest[linux_dbg_32] ,在test文件夾下生成test2執(zhí)行文件;
7、執(zhí)行命令:export ?LD_LIBRARY_PATH=/home/spring/test_makefile_gcc/bin ,用于指定文件需調(diào)用的動(dòng)態(tài)庫(kù)的路徑 (注:個(gè)人Ubuntu操作系統(tǒng)上實(shí)際的存放路徑);
8、執(zhí)行命令:./test2?,輸出結(jié)果:-6256,Debug動(dòng)態(tài)庫(kù)的調(diào)用完成;
9、若生成Release的動(dòng)態(tài)庫(kù)或靜態(tài)庫(kù),執(zhí)行 make release,其它步驟僅需修改生成的文件名即可。
Execute in turn:$ mkdir lib; mkdir bin$ cd project_makefile$ make debug$ ar -r ../lib/libtest[linux_dbg_32].a *.o$ cd ..; cd test$ g++ -o test test_makefile_gcc.cpp -L ../lib -ltest[linux_dbg_32]$ ./test$ cd ..; cd project_makefile$ gcc -shared -o ../bin/libtest[linux_dbg_32].so *.o$ g++ -o ../test/test2 ../test/test_makefile_gcc.cpp -L ../bin -ltest[linux_dbg_32]$ cd ..; cd test$ export LD_LIBRARY_PATH=../bin$ ./test2
GitHub:https://github.com/fengbingchun/Linux_Code_Test
總結(jié)
以上是生活随笔為你收集整理的Ubuntu下makefile及gcc生成静态库动态库的简单使用举例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenCV中图像旋转(warpAffi
- 下一篇: Ubuntu下CodeBlocks的安装