C++中有两种加载共享对象的方法
对于这两种方法中的任何一种,您始终需要要使用的对象的头文件。标头将包含您要在代码中使用的类或对象的定义。
静态地:
#include "blah.h"
int main()
{
ClassFromBlah a;
a.DoSomething();
}
gcc yourfile.cpp -lblah
动态(在 Linux 中):
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("libm.so", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); /* Clear any existing error */
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
return 0;
}
*盗自dlopen Linux 手册页 http://linux.die.net/man/3/dlopenWindows或其他平台下的过程是相同的,只需将dlopen替换为动态符号搜索的平台版本即可。
为了使动态方法发挥作用,您想要导入/导出的所有符号都必须具有 extern'd C 链接。
有一些话Here http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.prftungd/doc/prftungd/when_dyn_linking_static_linking.htm关于何时使用静态链接以及何时使用动态链接。