Add an existing library to a VTK module

In a project I am generating a VTK module with vtk_module_add_module(), to be built from a number of source files, and also to be linked with other modules with vtk_module_link(): one other self-built module, plus some Qt5 components (Qt5::Widgets etc.). So far so good.

But then I also need to link in a library of which I have just a header (.h) and library (.lib) file (yes, this is just for Windows).

If I try to achieve this using target_link_directories() and target_link_libraries(), I get an error message from both: “…can not be used on ALIAS target”.

Well, nice - I would need to call these with the “real” targets - but which are these? I am using a name with some prefix, like NNN::xyz.

So how do I get the “real name” for which NNN::xyz is only an alias?

Or else: Is there another way to add this lib file to the module? The code is actually used internally only, so any interface to other modules goes through my own functions.

Just found a solution that at least cmake accepts:

if(MSVC)
  add_library(ext_lib STATIC IMPORTED)
  if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set_property(TARGET ext_lib PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/lib/extD.lib)
  else()
    set_property(TARGET ext_lib PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/lib/ext.lib)
  endif()
  vtk_module_link(NNN::xyz
    PRIVATE
	  ext_lib)
endif()

Let’s see if it finally works!

1 Like

That looks like what I’d suggest :slight_smile: . You’ll need _vtk_module_install(ext_lib) there as well so that it gets added to the export set.

You can also do IMPORTED_LOCATION and IMPORTED_LOCATION_DEBUG instead of the if/else. An INTERFACE_INCLUDE_DIRECTORIES would also save you a vtk_module_include call.