Linux最简单的动态库开发

1 编写头文件

  test.h代码:

#ifndef __TEST_H_
#define __TEST_H_

void sayHello(void);

#endif

2 编写库代码

  test.c的代码:

#include <stdio.h>
#include "test.h"

void sayHello()
{
        printf("Hello my friend.\n");
}

3 编译动态库

gcc -fPIC -c test.c -I.
gcc -shared -o libtest.so test.o

4 编写main.c

#include "test.h"

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

5 编译调用动态库

gcc main.c -ltest -L. -I.

临时设置LD_LIBRARY_PATH为当前目录./

 

posted @ 2020-03-28 21:00  ziwuxian  阅读(524)  评论(0编辑  收藏  举报