0
votes

I am trying to build a plugin based application in C++.

I have two targets - base and plugin.

This is a portion of my CMakeLists.txt that does the main work

# 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

That symbol corresponds to a statement in the plugin dlib::logger dlog("main.abstract_cnn");

dlib is a static library which I am using in both the base and the plugin. 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.

Can anyone advise? Please?

How I am Loading I am using a library called Libsourcey which has a module called pluga for easy loading. My program is the basic test program given here pluga. It works for me, but when I try to include a 3rd party library I am stuck.

If anyone could point to resources that explain how to use 3rd party libraries in plugins, it would be great!

1
You link shared libraries just like you link executable programs. That target_link_libraries that you commented out for the shared library, what happens if you actually let CMake run it? - Some programmer dude
i tried that, but I get this error /usr/bin/ld: deps_build/dlib_build/libdlib.a(logger_kernel_1.cpp.o): relocation R_X86_64_32 against '.bss' can not be used when making a shared object; recompile with -fPIC - azmath
Okay, that's a problem. Then you need to tell us how you load the "plugin"? Please create a Minimal, Complete, and Verifiable Example and show us. - Some programmer dude
Try linking your executable with -rdynamic flag, that might export the missing symbol. - Maxim Egorushkin
@MaximEgorushkin do you mean compile the base with that flag? how to specify that in cmake? - azmath

1 Answers

1
votes

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).
I did that and now I am able to link my plugin against the static library.