0

In my project I ship a compiled static library foo.lib I want to link to from my main.cpp executable. I want to import the compiled library as an IMPORTED library.

File structure:

external/foo/lib/foo.lib
external/foo/include/foo.h
external/CMakeLists.txt
src/CMakeLists.txt
src/main.cpp
CMakeList.txt
external/CMakeLists.txt:

add_library(foolib STATIC IMPORTED GLOBAL)
set_target_properties(foolib PROPERTIES
    IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/foo/lib/"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/foo/include/"
)

Currently when I try to compile I get the error:

could not open '../../../external/foo/lib/': no such file or directory

so maybe using ${CMAKE_CURRENT_LIST_DIR} is not the correct approach.

  • What cmake variable should I use to define the path of the library?
  • Is this approach correct, or should I write a FindFoo.cmake module to locate this library (even if within my project)?
  • Can I use target_include_directories instead of setting INTERFACE_INCLUDE_DIRECTORIES directly?
Simone Gaiarin
  • 340
  • 2
  • 15
  • 1
    Please see the response [here](https://stackoverflow.com/a/10550334/3987854) for linking external libraries as CMake imported targets. The `IMPORTED_LOCATION` property must contain the **full path** to the library, including the library name. – Kevin Sep 29 '20 at 18:06
  • Right! Adding the full path to the .lib file allows me linking against it correctly indeed. – Simone Gaiarin Sep 29 '20 at 20:00

0 Answers0