编写GNUGCC下的动态链接库程序
先来一个简单的编译时动态链接的例子
创建文件function.h
#include <stdlib.h> #define int_t intint_t foo(int_t i, int_t j);
#include "function.h" int_t foo(int_t i, int_t j){ if (i > j) return i; else return j;}#include "function.h" int_t foo(int_t i, int_t j){ if (i > j) return i; else return j;}gcc -shared -fPIC function.c -o function.so
gcc -o main -L ./ main.c function.so
#include <stdlib.h>#include <dlfcn.h>--------------------------void main()-{- int (*func)(int i, int j);- void *dlhandle;- char *dlError;- printf("hello! I'm main.c\n"); dlhandle = dlopen("./function.so", RTLD_LAZY);- if (dlhandle == NULL) { printf("dlhandle error\n"); } dlError = dlerror();- func = dlsym(dlhandle, "foo");- printf("%i\n", func(10, 30)); dlclose(dlhandle);}gcc -o dltest -ldl dltest.c