How to link with external library?

Hi,

I have a C++ ParaView plugin that does not use VTK (so no vtk.module file). I want to link my plugin with the external library spdlog, which is contained as a Git submodule in my project. This library integrates well with the CMake commands

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/spdlog)
target_link_libraries(mytarget PUBLIC spdlog::spdlog)

in a non ParaView project. However, I don’t know the corresponding VTK/ParaView wrapper commands. In my plugin, I use the following wrappers:

paraview_plugin_find_plugins
paraview_plugin_scan
paraview_plugin_build
paraview_add_plugin

and I have a paraview.plugin file. I cannot use vtk_module_link, as my plugin does not use VTK modules.

Could you tell me how to expose the headers of spdlog and link it with my ParaView plugin ?

target_link_libraries(plugintarget PRIVATE spdlog::spdlog) should be fine. Plugins and VTK modules are CMake targets at heart (VTK and ParaView add additional properties on top that “make” them what they are).

At which level should I put target_link_libraries? After paraview_add_plugin(plugintarget ...)?
And target_include_directories to let CMake recognize the include directory of the source of the library?

Yes, paraview_add_plugin makes the target, so it has to go after that. Ideally your package’s target would be enough and you wouldn’t need target_include_directories.

Thanks, it works fine, no need for target_include_directories. However, I noticed that sometimes an explicit include directory command is necessary. I have another plugin, which wraps a VTK module, that links with my own library mylib. In that case, vtk_module_link is not sufficient, I still have to use vtk_module_include. Do you know why this is the case? Maybe the CMake export is properly written for spdlog, but not for mylib?

EDIT: I keep this paragraph because it may help others, but the solution is here.
Turning back to the original question about spdlog: during the build, libspdlog.so is put in the build directory, so my plugin works when launched from the build directory. However, the installation step does not copy libspdlog.so into the install directory. Instead of doing it manually (which works by the way), I tried

install(TARGETS spdlog DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})

but it still doesn’t copy. Do you know why?

Your target does not export its usage requirements properly. See this guide.

You cannot install IMPORTED targets with install(TARGETS). There is install(IMPORTED_RUNTIME_ARTIFACTS) though.

Perfect, thanks a lot.