CMake imported shared library only works when add_library is called in the same file as target_link_libraries

25 Views Asked by At

I have a dedicated CMakeLists.txt thirdparty/CMakeLists.txt for wrapping a few thirdparty libraries to include in the project.

Some of these libraries are distributed as compiled shared librares, and I'm trying to import them and their respective header files into the project.

My root CMakeLists.txt is structured like so:

add_subdirectory(thirdparty)
add_subdirectory(mylib)

The part of the CMake I have to import this library (in thirdparty/CMakeLists.txt) is below:

add_library(myexternallib SHARED IMPORTED)
set_property(TARGET myexternallib
        PROPERTY IMPORTED_LOCATION
        "${CMAKE_SOURCE_DIR}/thirdparty/myexternallib/x64/myexternallib.so"
        )
target_include_directories(myexternallib
        INTERFACE
        "${CMAKE_SOURCE_DIR}/thirdparty/myexternallib"
        )

I then try to link it to my own library (in mylib/CMakeLists.txt) with:

target_link_libraries(mylib PRIVATE
        ...
        myexternallib
        ...
        )

CMake compiles fine when running cmake, but when trying to build, I get an error with it trying to include one of the files in the /thirdparty/myexternallib directory, saying that file was not found.

I then moved the add_library(myexternallib) and other bits to the mylib/CMakeLists.txt and it then built fine.

Is there something I'm missing here that would allow me to specify the library in thirdparty/CMakeLists.txt but for it to be linked in a different CMakeLists (mylib/CMakeLists.txt)?

0

There are 0 best solutions below