如何在 C++ 中加载共享对象?

发布于 2024-07-27 17:21:16 字数 290 浏览 2 评论 0原文

我有一个共享对象(so - Windows dll 的 Linux 等效项),我想将其导入并与我的测试代码一起使用。

我确信这不是这么简单;)但这就是我想做的事情..

#include "headerforClassFromBlah.h"

int main()
{
    load( "blah.so" );

    ClassFromBlah a;
    a.DoSomething();
}

我认为这是一个非常基本的问题,但我在网上搜索时找不到任何可以跳出来的东西。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野の 2024-08-03 17:21:16

在 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 手册页 窃取
Windows或其他平台下的过程是相同的,只需将dlopen替换为动态符号搜索的平台版本即可。

为了使动态方法发挥作用,您想要导入/导出的所有符号都必须具有 extern'd C 链接。

有一些词 这里介绍何时使用静态链接以及何时使用动态链接。

There are two ways of loading shared objects in 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):

#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 page
The process under windows or any other platform is the same, just replace dlopen with the platforms version of dynamic symbol searching.

For the dynamic method to work, all symbols you want to import/export must have extern'd C linkage.

There are some words Here about when to use static and when to use dynamic linking.

羁〃客ぐ 2024-08-03 17:21:16

这取决于平台。 要在运行时执行此操作,在 Linux 上,您可以使用 dlopen,在 Windows 上,您使用 LoadLibrary

要在编译时执行此操作,请在 Windows 上使用 dllexportdllimport。 在 Linux 上,gcc 导出所有公共符号,因此您可以正常链接到它并调用该函数。 在这两种情况下,通常都需要您在头文件中包含符号名称,然后#include,然后使用编译器的功能链接到库。

It depends on the platform. To do it at runtime, on Linux, you use dlopen, on windows, you use LoadLibrary.

To do it at compile time, on windows you export the function name using dllexport and 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.

余生一个溪 2024-08-03 17:21:16

您需要 #include 与共享库关联的任何标头才能获取 ClassFromBlah 等内容的声明。 然后,您需要链接到 .so - 具体如何执行此操作取决于您的编译器和一般安装,但对于 g++ 来说,类似:

g++ myfile.cpp -lblah

可能会起作用。

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:

g++ myfile.cpp -lblah

will probably work.

陌路终见情 2024-08-03 17:21:16

它是 -l 链接像 libblah.a 这样的存档文件,或者如果将 -PIC 添加到 gcc,您将获得一个“共享对象”文件 libblah.so (它是构建它的链接器)。
我曾经有过一个 SUN,并构建了此类文件。
文件的修订号必须准确或更高(代码可能因错误而更改)。 但带参数的调用必须与输出相同。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文