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