简体   繁体   English

C ++将静态库链接到共享库

[英]C++ Linking Static Library against Shared Library

I am trying to build a plugin based application in C++. 我正在尝试使用C ++构建基于插件的应用程序。

I have two targets - base and plugin. 我有两个目标-基本和插件。

This is a portion of my CMakeLists.txt that does the main work 这是我的CMakeLists.txt文件的一部分,主要工作

# Executables
# build plugin
file (GLOB SRCP "plugin/*.?pp")
include_directories(plugin/)
MESSAGE ( STATUS "SRC: " "${SRCP}" )
add_library(testplugin MODULE ${SRCP} )
add_dependencies( testplugin ${DEPS_TARGETS})
#target_link_libraries( testplugin dlib::dlib )
#build main
file( GLOB SRC "*.?pp" )
message( STATUS "SRC: " "${SRC}" )
add_executable( exec ${SRC} )
add_dependencies( exec ${DEPS_TARGETS})
target_link_libraries( exec dlib::dlib )

It builds successfully but when I try to load the plugin, it fails with this error 它构建成功,但是当我尝试加载插件时,失败并显示此错误

Error: Cannot load library: libtestplugin.so: undefined symbol: _ZN4dlib6loggerD1Ev 错误:无法加载库:libtestplugin.so:未定义的符号:_ZN4dlib6loggerD1Ev

That symbol corresponds to a statement in the plugin dlib::logger dlog("main.abstract_cnn"); 该符号对应于插件dlib::logger dlog("main.abstract_cnn");一条语句dlib::logger dlog("main.abstract_cnn");

dlib is a static library which I am using in both the base and the plugin. dlib是我在基础和插件中都使用的静态库。 I link this library to the base application but I don't know how to link it to a library/module? 我将此库链接到基本应用程序,但是我不知道如何将其链接到库/模块? Won't the base application pass on the symbols to the plugin? 基本应用程序不会将符号传递给插件吗? What can I do in this situation? 在这种情况下我该怎么办?

I would also like a cross-platform solution. 我也想要一个跨平台的解决方案。 Mainly linux+windows. 主要是linux + windows。

Can anyone advise? 有人可以建议吗? Please? 请?

How I am Loading I am using a library called Libsourcey which has a module called pluga for easy loading. 我如何加载我正在使用一个名为Libsourcey的库,该库具有一个名为plugina的模块,可轻松加载。 My program is the basic test program given here pluga . 我的程序是这里给定的基本测试程序pluga It works for me, but when I try to include a 3rd party library I am stuck. 它对我有用,但是当我尝试包含第3方库时,我陷入了困境。

If anyone could point to resources that explain how to use 3rd party libraries in plugins, it would be great! 如果任何人都可以指向说明如何在插件中使用第三方库的资源,那就太好了!

SOLVED! 解决了!

I added this to my cmake file set(CMAKE_POSITION_INDEPENDENT_CODE ON) which is what the error message was trying to tell me (compile with -fPIC ). 我将此添加到我的cmake文件set(CMAKE_POSITION_INDEPENDENT_CODE ON) ,这是错误消息试图告诉我的内容(使用-fPIC编译)。
I did that and now I am able to link my plugin against the static library. 我做到了,现在我可以将插件链接到静态库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM