【发布时间】:2017-12-15 12:37:47
【问题描述】:
我需要从带有 boost.dll 库的 Linux .so 库中导入一个函数。我的代码是这样的:
namespace n1 {
namespace n2 {
struct st {
std::string n;
int m;
}
}
}
void foo(std::string const&, n1::n2::st& attr) {
/*some implementation*/
}
这里我尝试导入函数foo():
int main(int argc, char** argv) {
boost::filesystem::path path("some path");
boost::dll::experimental::smart_library lib(path);
auto f2 = lib.get_function<void(std::string const&, n1::n2::st&)>(path, "n1::n2::foo"); //<<----here runtime error
f2( std::string(), st{});
}
但我收到此运行时错误:
在抛出 'boost::exception_detail::clone_impl >' 的实例后调用终止 what(): boost::dll::shared_library::get() failed (dlerror system message: /path_to_my_library.so: undefined symbol: n1::n2::foo): Illegal seek
【问题讨论】:
-
嗯?您是否在定义
main()的翻译单元中包含了标头,或者让编译器知道在这些命名空间中声明了st?您似乎混淆了从 DLL 导入符号与编译使用这些符号及其依赖项的代码。即使您可以从 DLL 中导入函数,也不会自动为您声明它所依赖的所有符号。 -
@underscore_d,我必须使用 -I(要包含的路径)编译并包含包含实现 st 的头文件?
-
如果您引用
st之类的符号,引用它的翻译单元需要通过声明可用来了解它。就这么简单。 -
@underscore_d,谢谢
-
从发布的代码中我看到 n1::n2::st 是一种类型,而您可以从 dll 导入实例(通常是函数指针,但也可以是变量)。从问题描述中我得到的印象是您希望导入一个类型,但事实并非如此。