歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Ubuntu下的進行動態鏈接庫創建和使用的方法

Ubuntu下的進行動態鏈接庫創建和使用的方法

日期:2017/3/1 9:58:07   编辑:Linux編程

這裡的是Ubuntu下的進行動態鏈接庫創建和使用的方法。

1.創建實驗程序dll_fun.c

#include <stdio.h>
void dll_function(const char* szString)
{
printf("%s\n", szString);
}

2.編譯生成動態鏈接庫

首先生成生成o文件

gcc -c -fPIC dll_fun.c //這裡一定要加上-fPIC選項,不然下一步編譯失敗

再編譯下

gcc -shared -fPIC -o libdllfun.so dll_fun.o //生成動態鏈接庫libdllfun.so

3. 將生成的文件拷貝到/usr/lib

4.創建調用動態庫方法main.c

void dll_function(const char* szString);
int main()
{
dll_function("This is the words of the dll function!!!!!!");
return 0;
}

5.編譯main.c生成可執行文件

6.執行./main 可得 This is the words of the dll function!!!!!!

7.編寫julia程序main.jl

ccall((:dll_function,"libdllfun"),Void,(Ptr{Uint8},),"Hello GONG Qingkui")

8.編譯運行 julia main.jl

得道結果Hello GONG Qingkui

ps:使用環境變量指出鏈接庫文件地址 export LD_LIBRARY_PATH=/home/owner/test/lib

更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2

Copyright © Linux教程網 All Rights Reserved