zoukankan      html  css  js  c++  java
  • linux 下动态库的制作及使用

    1、动态链接库的制作

    动态链接库与普通的程序相比,没有main函数。 通过 -shared 和 -fPIC 编译参数产生 .so 动态链接库文件。程序在调用库函数时

    只要链接上这个库即可。

    1)编写动态库代码

    定义头文件 reader.h

    #ifndef READER_H_
    #define READER_H_
    
    int open(char *name);
    int close(char *name);
    
    #endif // READER_H_

    编写函数体 reader.c

    #include <stdio.h>
    
    int open(char *name)
    {
        printf("%s opened.
    ", name);
        return 0;
    }
    
    int close(char *name)
    {
        printf("%s closed.
    ", name);
        return 0;
    }

    2)生成动态库文件

    $ gcc -shared -fPIC reader.c -o libreader.so

    2、动态库的使用

    第一步:编写测试代码 testlib.c

    #include "reader.h"
    
    int main()
    {
        char readerName[20] = "81Reader";
    
        open(readerName);
        close(readerName);
       
        return 0;
    }

    第二步:调用动态库 (-L 指明动态链接库的路径)

    $ gcc testlib.c -o testlib -L ./ -lreader

    第三步:测试输出

    81Reader opened.
    81Reader closed.
  • 相关阅读:
    结对编程第一次作业
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    第五次作业(结对第2次)
    第四次作业
    第三次作业
    第二次作业(多图预警)
    第一次作业
    软工第四次作业——结对编程二
  • 原文地址:https://www.cnblogs.com/aqing1987/p/4560718.html
Copyright © 2011-2022 走看看