如何从共享库中导出和向上符号? 我需要实现诸如插件机制之类的东西。对于库加载,我使用boost.dll。 我想返回在库中实现的后代类的实例。 标题中的代码

问题描述 投票:0回答:0
我想返回在图书馆中实现的后代类的实例。 共享库的标题中的编码:
class EXPORT_DECLSPEC BaseA { public: virtual void method() = 0; virtual ~BaseA() = default; }; class EXPORT_DECLSPEC DerivedA : public BaseA { public: DerivedA() = default; ~DerivedA() override = default; void method() override; void method2(); }; // Some function to get derived instance. BaseA* get_derived_instance() { return new DerivedA(); }

在共享库中.cpp:

void DerivedA::method() { std::cout << "hello from BaseA" << std::endl; }; void DerivedA::method2() { std::cout << "M2" << std::endl; };

Macro只是一个可见性属性:
#ifdef _WIN32
    #ifdef BUILDING_DLL
        #define EXPORT_DECLSPEC __declspec(dllexport)
    #else
        #define EXPORT_DECLSPEC __declspec(dllimport)
    #endif
#else // For POSIX
    #define EXPORT_DECLSPEC __attribute__((visibility("default")))
#endif

在这样的测试代码中未编译:
// Gtest.
TEST(BaseLoadTest, BaseLoadTest1)
{
    auto da = dynamic_cast<DerivedA*>(get_derived_instance());
    da->method2();
}

但链接阶段不会通过以下错误传递:
[100%] Linking CXX executable test_deb
/usr/bin/ld: CMakeFiles/test_deb.dir/loader_test.cpp.o: in function `BaseLoadTest_BaseLoadTest1_Test::TestBody()':
test_deb/loader_test.cpp:131: undefined reference to `typeinfo for DerivedA'
/usr/bin/ld: CMakeFiles/test_deb.dir/loader_test.cpp.o: in function `DerivedA::DerivedA()':
include/devices/dev_a.h:45: undefined reference to `vtable for DerivedA'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/test_deb.dir/build.make:124: test_deb] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:1174: CMakeFiles/test_deb.dir/all] Error 2
gmake: *** [Makefile:156: all] Error 2

adding
-E
选项无济于事(dynamic_cast vs Dynamic Library边界

)。

请建议如何解决此问题并从库中获取“ deriveda”实例。
	

使用外部“ C”进行get_derived_instance.

    
c++ dll linker shared-libraries
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.