4

我正在玩弄 c++17 和插件,我遇到了一个我无法解决的错误。在下面的 MWE 中,我可以调用一个带有 a 的本地函数,std::any当我尝试读取内容时,一切都按预期工作。当我通过插件(dlopen)加载这个完全相同的功能时,它可以正确地看到任何类型的类型,但看不到std::any_cast内容。

在找出导致此错误的原因时,将不胜感激任何帮助。

这是我的环境,MWE,以及由此产生的错误。

>> g++ --version

g++ (GCC) 7.1.1 20170526 (Red Hat 7.1.1-2)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

>> scons --version

SCons by Steven Knight et al.:
    script: v2.5.1.rel_2.5.1:3735:9dc6cee5c168[MODIFIED], 2016/11/03 14:02:02, by bdbaddog on mongodog
    engine: v2.5.1.rel_2.5.1:3735:9dc6cee5c168[MODIFIED], 2016/11/03 14:02:02, by bdbaddog on mongodog
    engine path: ['/usr/lib/scons/SCons']
Copyright (c) 2001 - 2016 The SCons Foundation

>> tree

.
├── SConstruct
└── src
    ├── main.cpp
    ├── plugin.cpp
    └── SConscript

1 directory, 4 files

>> cat SConstruct

SConscript('src/SConscript', variant_dir='build', duplicate=0)

>> cat src/SConscript

env = Environment()
env.Append(CXXFLAGS=['-std=c++17'])
plugin = env.SharedLibrary('plugin', 'plugin.cpp')
Install('../lib', plugin)
driver_env = env.Clone()
driver_env.Append(LIBS=['dl', 'stdc++fs'])
driver = driver_env.Program('driver', 'main.cpp')
Install('../bin', driver)

>> cat src/plugin.cpp

#include <any>
#include <iostream>
using namespace std;
extern "C" {
int plugin(any& context) {
    cout << "    Inside Plugin" << endl;
    cout << "    Has Value? " << context.has_value() << endl;
    cout << "    Type Name: " << context.type().name() << endl;
    cout << "    Value: " << any_cast<double>(context) << endl;
}
}

>> cat src/main.cpp

#include <functional>
#include <iostream>
#include <stdexcept>
#include <any>
#include <experimental/filesystem>
#include <dlfcn.h>

using namespace std;
using namespace std::experimental;

function< void(any&) > loadplugin(string filename) {
    function< void(any&) > plugin;
    filesystem::path library_path(filename);
    filesystem::path library_abspath = canonical(library_path);
    void * libraryHandle = dlopen(library_abspath.c_str(), RTLD_NOW);
    if (!libraryHandle) {
        throw runtime_error("ERROR: Could not load the library");
    }
    plugin = (int(*) (any&))dlsym(libraryHandle, "plugin");
    if (!plugin) {
        throw runtime_error("ERROR: Could not load the plugin");
    }
    return plugin;
}

int local(any& context) {
    cout << "    Inside Local" << endl;
    cout << "      Has Value? " << context.has_value() << endl;
    cout << "      Type Name: " << context.type().name() << endl;
    cout << "      Value: " << any_cast<double>(context) << endl;
}

int main(int argc, char* argv[]) {
    cout << "  Resolving Paths..." << endl;
    filesystem::path full_path = filesystem::system_complete( argv[0] ).parent_path();
    filesystem::path plugin_path(full_path/".."/"lib"/"libplugin.so");
    cout << "  Creating Context..." << endl;
    any context(.1);
    cout << "  Loading Plugin..." << endl;
    function<void(any&) > plugin = loadplugin(plugin_path.string());
    cout << "  Calling Local..." << endl;
    local(context);
    cout << "  Calling Plugin..." << endl;
    plugin(context);
}

>> scons

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o build/main.o -c -std=c++17 src/main.cpp
g++ -o build/driver build/main.o -ldl -lstdc++fs
Install file: "build/driver" as "bin/driver"
g++ -o build/plugin.os -c -std=c++17 -fPIC src/plugin.cpp
g++ -o build/libplugin.so -shared build/plugin.os
Install file: "build/libplugin.so" as "lib/libplugin.so"
scons: done building targets.

>> tree

.
├── bin
│   └── driver
├── build
│   ├── driver
│   ├── libplugin.so
│   ├── main.o
│   └── plugin.os
├── lib
│   └── libplugin.so
├── SConstruct
└── src
    ├── main.cpp
    ├── plugin.cpp
    └── SConscript

4 directories, 10 files

>> bin/driver

  Resolving Paths...
  Creating Context...
  Loading Plugin...
  Calling Local...
    Inside Local
      Has Value? 1
      Type Name: d
      Value: 0.1
  Calling Plugin...
    Inside Plugin
    Has Value? 1
    Type Name: d
terminate called after throwing an instance of 'std::bad_any_cast'
  what():  bad any_cast
    Value: Aborted (core dumped)
4

1 回答 1

9

libstdc++any依赖于相同模板实例化的地址在程序中是相同的,这意味着如果您正在使用,则需要采取预防措施dlopen

编译器必须在任何需要它们存在的翻译单元中发出[具有模糊链接的对象,如模板实例化],然后依靠链接和加载过程来确保在最终的可执行文件中只有一个是活动的。对于静态链接,所有这些符号都在链接时解析,但对于动态链接,在加载时会发生进一步的解析。您必须确保针对可执行文件和其他共享库中的对象解析共享库中的对象。

  • 对于链接到共享库的程序,不需要额外的预防措施。
  • 您无法使用该-Bsymbolic选项创建共享库,因为这会阻止上述解决方案。
  • 如果您使用dlopen从共享库显式加载代码,您必须做几件事。首先,通过将可执行文件与-E标志链接来从可执行文件中导出全局符号(您必须指定这一点,就-Wl,-E好像您以通常的方式从编译器驱动程序 g++ 调用链接器一样)。RTLD_GLOBAL您还必须通过向 提供标志来使加载库中的外部符号可用于后续库dlopen。符号解析可以是立即的或惰性的。
于 2017-06-11T01:27:30.183 回答