zoukankan      html  css  js  c++  java
  • Linux C

    在Linux C中生成动态库方法如下:
    1,测试程序:
    1)生成动态库的源代码文件Test.c:

    #include "stdio.h"
    
    int get_result(int firstNum,int secondNum)
    {
    printf("so file "get_result" called!
    ");
    return firstNum+secondNum;
    }

    其接口文件为:

    #ifndef _TEST_H_
    #define _TEST_H_
    
    int get_result(int firstNum,int secondNum);
    
    #endif //test.h

    2)动态链接库测试程序main.c:

    #include <stdio.h>
    #include "Test.h"
    
    int main()
    {
    int rlt; 
    rlt = get_result(23,7);
    printf("The result is: rlt = %d
    ",rlt);
    
    return 0;
    }

    2,生成动态链接库

    gcc Test.c –fPIC –shared –o libtest.so

    该命令生成动态库libtest.so,默认以lib开头,以.so为后缀;
    -fPIC:编译为位置独立的代码;
    -shared:编译为动态库。
    3,调用动态库

    gcc main.c -L. -ltest -o main

    指定动态库路径:

    export LD_LIBRARY_PATH=$(pwd)

    假设动态链接库libtest.so和可执行文件位于同一目录,如无此命令会显示下述错误:
    ./main: error while loading shared libraries:libtest.so:cannot open shared object file:No sush file or directory.
    4,执行:
    ./main
    5,结果输出为:

  • 相关阅读:
    317 随笔
    316 随笔
    315 随笔
    python 第一章
    matlab 第四章 第一节 字符串 元胞
    matlab 第三章 第二节 多维数组
    matlab 第三章
    python 循环+break continue
    Springboot 教程 导入
    matlab 第二章 第三节 数值表示、变量及表达式
  • 原文地址:https://www.cnblogs.com/Pan-Z/p/6831068.html
Copyright © 2011-2022 走看看