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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mac下通过gcc命令手动编译动态链接库示例

發(fā)布時間:2024/3/24 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mac下通过gcc命令手动编译动态链接库示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

? ? 編譯動態(tài)鏈接庫,windows,linux,mac平臺各不相同,從文件上來說,windows下是dll,linux下是so,mac下是dylib;命令上也會有區(qū)別,windows下用cl,linux下用gcc但是參數(shù)是-fPIC -shared,而在mac下則是gcc -dynamiclib -o libxxx.dylib xxx.c。

? ? 這里介紹在mac下手動編譯動態(tài)庫示例:

? ? 準(zhǔn)備hello.h

#ifndef SO_H #define SO_H int add(int a,int b); void hello(char* str,char* res); #endif

? ? hello.c

#include <string.h> #include "hello.h" int add(int a,int b){return a + b + 1; } void hello(char* str,char* res){strcat(str,"world.");strcpy(res,str); }

? ? 開始編譯動態(tài)庫:

gcc -dynamiclib -o libhello.dylib hello.c

? ? 編譯完成,在目錄下會生成一個文件libhello.dylib

? ? 編寫測試程序,調(diào)用動態(tài)鏈接庫:

? ? test.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #include "hello.h" int main(){int sum = add(2,3);printf("sum=%d\n",sum);char str[] = "hello,";char res[20];hello(str,res);printf("hello()->%s\n",res);return 0; }

? ? 首先需要編譯程序,生成可執(zhí)行程序:

gcc test.c -lhello -L. -o test

? ? 編譯不報錯,會生成test可執(zhí)行程序,該程序可以直接運行:

admin@localhost:~/workspace/myapp2/dllapp$ ./test sum=6 hello()->hello,world.

?

總結(jié)

以上是生活随笔為你收集整理的mac下通过gcc命令手动编译动态链接库示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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