Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I install shared imported library?

Tags:

I have an external project and an imported shared library. The include directories and implib all work correctly, but trying to install the shared library (dll) fails with the following error:

install TARGETS given target "my_shared_lib" which does not exist in this directory. 

Here's code to reproduce:

add_library(my_shared_lib SHARED IMPORTED GLOBAL) set_property(TARGET my_shared_lib PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/ExternalProjects/my_shared_lib") set_property(TARGET my_shared_lib PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/ExternalProjects/my_shared_lib/my_shared_lib.dll") set_property(TARGET my_shared_lib PROPERTY IMPORTED_IMPLIB "${CMAKE_CURRENT_BINARY_DIR}/ExternalProjects/my_shared_lib/my_shared_lib.lib")  add_executable(main main.cpp) add_dependencies(main my_shared_lib) target_link_libraries(main PUBLIC my_shared_lib)  install(TARGETS main DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/DIST") install(TARGETS my_shared_lib DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/DIST") 

Any ideas?


EDIT: For now I've gotten around this problem by using get_property to pull out the IMPORTED_LOCATION, then using INSTALL FILES and giving the value of that property. It seems to work, but is there a better, more idiomatic-cmake solution?

like image 412
Jeff M Avatar asked Dec 16 '16 00:12

Jeff M


2 Answers

CMake doesn't allow to install IMPORTED libraries as TARGETS. Use install(FILES) instead.

There are at least 2 reasons for such behavior:

  1. Сitation of one of CMake developer from bug report

    Imported targets were originally designed for importing from an existing installation of some external package so installing did not make sense at the time.

  2. When install normal library, CMake is able to modify it for adjust some properties like RPATH. Such modification is possible because CMake knows how the library has been built. This is a main advantage of installing library as a TARGET.

    But for IMPORTED library CMake has no information about the library's compilation process, and cannot perform any reasonable modification of it. So, CMake may only install the library file as is: no advantages against simple install(FILES).

like image 130
Tsyvarev Avatar answered Sep 28 '22 03:09

Tsyvarev


In CMake 3.21, there is a new subcommand to install(...) called IMPORTED_RUNTIME_ARTIFACTS.

See the full documentation.

In your case, you could just do something like this:

install(IMPORTED_RUNTIME_ARTIFACTS my_shared_lib) 
like image 39
nyibbang Avatar answered Sep 28 '22 02:09

nyibbang