如何在 C++ 中加载共享对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1142103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I load a shared object in C++?
提问by Ben L
I have a shared object (a so - the Linux equivalent of a Windows dll) that I'd like to import and use with my test code.
我有一个共享对象(so - 相当于 Windows dll 的 Linux),我想导入它并与我的测试代码一起使用。
I'm sure it's not this simple ;) but this is the sort of thing I'd like to do..
我敢肯定这不是那么简单;) 但这是我想做的事情..
#include "headerforClassFromBlah.h"
int main()
{
load( "blah.so" );
ClassFromBlah a;
a.DoSomething();
}
I assume that this is a really basic question but I can't find anything that jumps out at me searching the web.
我认为这是一个非常基本的问题,但我找不到任何让我在网上搜索的内容。
回答by Salgar
There are two ways of loading shared objects in C++
C++中有两种加载共享对象的方式
For either of these methods you would always need the header file for the object you want to use. The header will contain the definitions of the classes or objects you want to use in your code.
对于这些方法中的任何一种,您总是需要要使用的对象的头文件。标头将包含您要在代码中使用的类或对象的定义。
Statically:
静态:
#include "blah.h"
int main()
{
ClassFromBlah a;
a.DoSomething();
}
gcc yourfile.cpp -lblah
Dynamically (In Linux):
动态(在 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;
}
*Stolen from dlopen Linux man pageThe process under windows or any other platform is the same, just replace dlopen with the platforms version of dynamic symbol searching.
*盗自dlopen Linux man pagewindows下或其他任何平台下的过程都是一样的,只需将dlopen替换为动态符号搜索的平台版本即可。
For the dynamic method to work, all symbols you want to import/export must have extern'd C linkage.
要使动态方法起作用,您要导入/导出的所有符号都必须具有 extern'd C 链接。
There are some words Hereabout when to use static and when to use dynamic linking.
有一些词在这里何时使用静态以及何时使用动态链接。
回答by 1800 INFORMATION
It depends on the platform. To do it at runtime, on Linux, you use dlopen, on windows, you use LoadLibrary.
这取决于平台。要在运行时执行此操作,在 Linux 上使用dlopen,在 Windows 上使用LoadLibrary。
To do it at compile time, on windows you export the function name using dllexportand dllimport. On linux, gcc exports all public symbols so you can just link to it normally and call the function. In both cases, typically this requires you to have the name of the symbol in a header file that you then #include
, then you link to the library using the facilities of your compiler.
要在编译时执行此操作,请在 Windows 上使用dllexport和dllimport导出函数名称。在 linux 上,gcc 导出所有公共符号,因此您可以正常链接到它并调用该函数。在这两种情况下,通常这都要求您在头文件中具有符号的名称#include
,然后使用编译器的功能链接到库。
回答by 1800 INFORMATION
You need to #include any headers associated with the shared library to get the declrarations of things like ClassFromBlah. You then need to link against the the .so - exactly how you do this depends on your compiler and general instalation, but for g++ something like:
您需要 #include 与共享库关联的任何标头,以获取诸如 ClassFromBlah 之类的声明。然后你需要链接到 .so - 你如何做到这一点取决于你的编译器和一般安装,但对于 g++ 是这样的:
g++ myfile.cpp -lblah
will probably work.
可能会工作。
回答by user3213642
It is -l that link the archive file like libblah.a or if you add -PIC to gcc you will get a 'shared Object' file libblah.so (it is the linker that builds it). I had a SUN once and have build this types of files. The files can have a revision number that must be exact or higher (The code can have changed due to a bug). but the call with parameters must be the same like the output.
它是 -l 链接存档文件,如 libblah.a 或者如果您将 -PIC 添加到 gcc,您将获得一个“共享对象”文件 libblah.so(它是构建它的链接器)。我曾经有一个 SUN 并且已经构建了这种类型的文件。文件可以有一个必须精确或更高的修订号(由于错误,代码可能已更改)。但带参数的调用必须与输出相同。