动态加载,无需外部"C"

Dynamic Loading Without extern "C"

本文关键字:外部 加载 动态      更新时间:2023-10-16

我一般想使用libdl来动态加载c++。这个问题是在运行时识别已命名混乱的符号。

正如这里所描述的,一种解决方案是通过使用extern "C"来消除名称混淆。

http://www.tldp.org/HOWTO/C + + dlopen/theproblem.html

这个解决方案的缺点是将动态加载的资源限制为C风格的接口。例如,动态加载的函数不能是重载函数。

克服这个限制的好方法是什么?

一种可能的解决方案是,当需要链接库时,使用工具对库源代码进行命名处理,并附带一个函数来获取修改后的名称。llvm是否为此提供了工具?

也许一个笨拙的解决方案是一个函数,它接受一个函数签名,用一个有签名的函数创建假代码,管道到编译器中,该编译器与一个用于生成汇编的标志一起使用,解析输出以检索经过修改的名称,并将经过修改的名称作为字符串返回。然后可以将字符串传递给dlsym()。

为了使问题具体化,这里有两个示例程序来说明外部"C"解决方案在不修改库代码的情况下无法动态加载的东西。第一个是用传统的c++方式动态链接一个库。第二种用法是"open"。在第一个程序中链接一个重载函数很简单。没有简单的方法来链接第二个程序中的重载函数。

程序1:Loadtime动态链接

main.cpp

// forward declarations of functions that will be linked
void say(int);
void say(float);
int main() {
    int myint = 3;
    say(myint);
    float myfloat = 5.0f;
    say(myfloat);
}

say.cpp

#include <iostream>
//extern "C" function signatures would collide
//extern "C" void say(int a) {
void say(int a) {
    std::cout << "The int value is " << a << ".n";
}
//extern "C" void say(float a) {
void say(float r) {
    std::cout << "The float value is " << r << ".n";
}

输出
$ ./main
The int value is 3.
The float value is 5.

程序2:Runtime动态链接

main_with_dl.cpp

#include <iostream>
#include <dlfcn.h>
int main() {
    // open library
    void* handle = dlopen("./say_externC.so", RTLD_LAZY);
    if (!handle) {
        std::cerr << "dlopen error: " << dlerror() << 'n';
        return 1;
    }
    // load symbol
    typedef void (*say_t)(int);
    // clear errors, find symbol, check errors
    dlerror();
    say_t say = (say_t) dlsym(handle, "say");
    const char *dlsym_error = dlerror();
    if (dlsym_error) {
        std::cerr << "dlsym error: " << dlsym_error << 'n';
        dlclose(handle);
        return 1;
    }
    // use function
    int myint = 3;
    say(myint);
    // can't load in void say(float)
    // float myfloat = 5.0f;
    // say(myfloat);
    // close library
    dlclose(handle);
}

输出
$ ./main_with_dl
The int value is 3.

Makefile

CXX = g++
all: main main_with_dl say_externC.so
main: main.cpp say.so
    $(CXX) -o $@ $^
main_with_dl: main_with_dl.cpp
    $(CXX) -o $@ $<
%.so : %.cpp
    $(CXX) -shared -o $@ $<
.PHONY: clean
clean:
    rm main main_with_dl say.so say_externC.so

多亏了Mooing Duck,我才能够在Visual Studio的启发下使用clang想出一个解决方案。

键是由Visual Studio和clang提供的一个宏。__FUNCDNAME__宏解析为封闭函数的混乱名称。通过定义与我们想要动态链接的函数具有相同签名的函数,我们可以使__FUNCDNAME__解析到所需的名称manggle。

这是程序2的新版本,它可以调用void say(int)void say(float)

EDIT moving Duck给了我更多的知识。这是main_with_dl.cpp的一个版本,在问题中与say.cpp一起工作。

#include <iostream>
#include <dlfcn.h>
void* handle;
template<class func_sig> func_sig get_func(const char* signature)
{
    dlerror();
    func_sig func = (func_sig) dlsym(handle, signature);
    const char *dlsym_error = dlerror();
    if (dlsym_error) {
        std::cerr << "dlsym error: " << dlsym_error << 'n';
        dlclose(handle);
        exit(1);
    }
    return func;
}
void say(int a) {
    typedef void(*func_sig)(int);
    static func_sig func = get_func<func_sig>(__FUNCDNAME__);
    return func(a);
}
void say(float a) {
    typedef void(*func_sig)(float);
    static func_sig func = get_func<func_sig>(__FUNCDNAME__);
    return func(a);
}
int main() {
    // open library
    //void* handle = dlopen("./say_externC.so", RTLD_LAZY);
    handle = dlopen("./say.so", RTLD_LAZY);
    if (!handle) {
        std::cerr << "dlopen error: " << dlerror() << 'n';
        return 1;
    }
    // use function
    int myint = 3;
    say(myint);
    float myfloat = 5.0f;
    say(myfloat);
    // close library
    dlclose(handle);
}
http://coliru.stacked-crooked.com/a/7249cc6c82ceab00

代码必须使用clang++-fms-extensions标志进行编译,__FUNCDNAME__才能工作