0

I would like to use and deploy an external library in my program.

My project's structure is as follow (simplified):

ProjectDir
   |_ _build
   |_ CMakeLists.txt
   |_ Src
   |   |_ CMakeLists.txt
   |   |_ .h and .cpp files
   |_ ThirdParty
       |_ CMakeLists.txt
       |_ ExternalLib
          |_ includes
          |   |_ .h files
          |_ lib
              |_ Windows
              |    |_ .dll files
              |_ Linux
                   |_ .so files

For this library, I've only the include files and the binaries for Windows and Linux.

I'm able to find the includes files by using target_include_directories() in the CMakeLists.txt of the ThirdParty directory but I cannot build my program even by declaring the libraries with :

target_link_libraries(${CMAKE_PROJECT_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/ExternalLib/lib/Windows/extLib)

Note: this command is also in the CMakeLists.txt of ThirdParty directory.

I get the following error:

'../../../ThirdParty/ExternalLib/lib/Windows/extLib', needed by 'program' missing and no known rule to make it

I develop on a Windows platform with VS2022 and my program will be build and installed on a Linux server.

On Windows I've the latest version of CMake but on Linux the version is 3.16.

Can somebody help me in fixing this error?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • You call `target_link_libraries` with path `${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/ExternalLib/lib/Windows/extLib`, but that path doesn't denote a library **file**. Exactly this CMake tells you: you should provide correct path. If you want to make your project to work both on Windows and Linux, and want CMake to automatically fill extension of the library file, then you could use `find_library` command. – Tsyvarev Sep 03 '22 at 13:07

1 Answers1

0

You should import library as a target of you project. For instance something like

add_library(your_lib SHARED IMPORTED)
set_property(TARGET your_lib PROPERTY
             IMPORTED_LOCATION
             "${CMAKE_SOURCE_DIR}/ThirdParty/ExternalLib/lib/Linux/extLib.so") # to be adapted for windows
target_include_directories(your_lib
                       PUBLIC
                       ${CMAKE_SOURCE_DIR}/ThirdParty/ExternalLib/include)

then you should be able to use it as any other library in cmake:

target_link_libraries(yourExecutable PRIVATE your_lib)

see doc in details here: https://cmake.org/cmake/help/latest/guide/importing-exporting/index.html

OznOg
  • 4,440
  • 2
  • 26
  • 35