准備三個文件test.h, test.c, main.c
test.h
#include <stdio.h>
void say_hello();
test.c
#include "test.h" void say_hello(char *name){ printf("hello %s\n", name); }
main.c
#include "test.h" int main(){ say_hello("guanxianseng"); return 0; }
執行生成so文件命令
gcc test.c -fPIC -shared -o libtest.so
編譯生成main可執行文件
gcc main.c -L. -ltest -o main
備注:這里編譯main.c需要提供.h頭文件,不然會報編譯錯誤