日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Ubuntu下makefile及gcc生成静态库动态库的简单使用举例

發(fā)布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ubuntu下makefile及gcc生成静态库动态库的简单使用举例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

環(huán)境:Ubuntu-13.10? 32位(虛擬機(jī))、gcc4.8.1

首先創(chuàng)建一個test_makefile_gcc文件夾,此test_makefile_gcc文件夾下包括:src文件夾用于存放源文件; include文件夾用于存放頭文件;bin文件夾用于存放生成的動態(tài)庫.so文件;lib文件夾用于存放生成的靜態(tài)庫.a文件;project_makefile文件夾存放此工程的makefile文件;test文件夾存放用來測試靜態(tài)庫和動態(tài)庫的源文件;另外在test_makefile_gcc文件夾下還包含一個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)庫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)庫的調(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)用的動態(tài)庫的路徑 (注:個人Ubuntu操作系統(tǒng)上實(shí)際的存放路徑);

8、執(zhí)行命令:./test2?,輸出結(jié)果:-6256,Debug動態(tài)庫的調(diào)用完成;

9、若生成Release的動態(tài)庫或靜態(tài)庫,執(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生成静态库动态库的简单使用举例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉(zhuǎn)載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:Ubuntu下makefile及gcc生成静态库动态库的简单