Mac下使用C語言生成和使用動態鏈接庫


2020-07-06

  09:25:54

 

1.編寫程序;

1 //動態庫頭文件test.h
2 
3 
4 #ifndef NDK_TEST_H 5 #define NDK_TEST_H
6 
7 void sayHello(); 8 
9 #endif 
 1 //動態庫源文件test.c
 2 
 3 
 4 #include "test.h"
 5 #include <stdio.h>
 6 
 7 void sayHello(){  8     
 9     printf("HELLO WORLD/n"); 10     
11 }
//主函數源文件
 #include "test.h"

int main(){ sayHello();
return 0; }

 

2.使用;

  •   生成test動態函數庫
  • //1.編譯 //"-c",將.c源文件編譯成.o目標文件,不進行鏈接
     clang -c test.c 
    //2.生成動態鏈接庫 //"-o",鏈接 /**注意:動態庫名字必須為libxxxx.dylib(xxxx=文件名)
    不同的操作系統的動態鏈接庫后綴是不相同的,但都是一個道理
    Windows為 .dll
    Linux為 .so
    macOS里為 .dylib

    **/
        clang -shared test.o -o libtest.dylib

     

  • 使用動態鏈接庫
    //編譯main函數所在源文件
     clang main.c -c //鏈接
     clang main.o -L. -l test -o main //執行
     ./main 

     

  •  

     

     

     

     

      

引用自:https://www.jianshu.com/p/246e015f00d1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM