使用 g++ 编译动态共享库 [英] Dynamic Shared Library compilation with g++

查看:43
本文介绍了使用 g++ 编译动态共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 编译以下简单的 DL 库示例代码使用 g++ 的程序库-HOWTO.这只是一个示例,因此我可以学习如何使用和编写共享库.我正在开发的库的真实代码将用 C++ 编写.

I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with g++. This is just an example so I can learn how to use and write shared libraries. The real code for the library I'm developing will be written in C++.

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
    void *handle;
    double (*cosine)(double);
    char *error;

    handle = dlopen ("/lib/libm.so.6", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }

    cosine = dlsym(handle, "cos");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }

    printf ("%f
", (*cosine)(2.0));
    dlclose(handle);
}

如果我用 gcc 编译程序,它可以正常工作.

If I compile the program with gcc it works fine.

gcc -o foo foo.c -ldl

当我将文件名和编译器更改为以下内容时

When I change the filename and compiler to the following

g++ -o foo foo.cpp -ldl

我收到以下错误:

foo.cpp:16: 错误:从 'void*' 到 'double (*)(double)' 的无效转换

foo.cpp:16: error: invalid conversion from 'void*' to 'double (*)(double)'

我理解(我认为我理解,如果有错请纠正我)我不能从 C++ 中的 void 指针进行隐式转换,但 C 允许我这样做,这就是为什么上面的代码将使用 gcc 但不使用 g++ 编译.所以我通过将上面的第 16 行更改为:

I understand (I think I understand, correct me if this is wrong) that I can't do an implicit cast from a void pointer in C++, but C lets me, and this is why the above code will compile using gcc but not using g++. So I tried an explicit cast by changing line 16 above to:

cosine = (double *)dlsym(handle, "cos");

有了这个,我得到以下错误:

With this in place, I get the following error:

foo.cpp:16: 错误:不能在赋值中将 'double*' 转换为 'double (*)(double)'

foo.cpp:16: error: cannot convert 'double*' to 'double (*)(double)' in assignment

这些问题可能更多地与我自己对适当的 C++ 编码标准的普遍无知有关,而不是其他任何事情.谁能给我指出一个关于使用 C++ 示例代码为 Linux 开发动态库的好教程?

These problems probably have more to do with my own general ignorance of proper C++ coding standards than anything else. Can anyone point me to a good tutorial on developing dynamic libraries for Linux that uses C++ example code?

推荐答案

C 允许从 void * 到任何指针类型(包括函数指针)的隐式强制转换;C++ 需要显式转换.正如 leiflundgren 所说,您需要将 dlsym() 的返回值转换为您需要的函数指针类型.

C allows implicit casts from void * to any pointer type (including function pointers); C++ requires explicit casting. As leiflundgren says, you need to cast the return value of dlsym() to the function pointer type you need.

很多人觉得 C 的函数指针语法很尴尬.一种常见的模式是 typedef 函数指针:

Many people find C's function pointer syntax awkward. One common pattern is to typedef the function pointer:

typedef double (*cosine_func_ptr)(double);

您可以将函数指针变量 cosine 定义为您的类型的成员:

You can define your function pointer variable cosine as a member of your type:

cosine_func_ptr cosine;

并使用类型而不是笨拙的函数指针语法进行转换:

And cast using the type instead of the awkward function pointer syntax:

cosine = (cosine_func_ptr)dlsym(handle, "cos");

这篇关于使用 g++ 编译动态共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆