Reputation: 79
Say I have C project with the following structure (simplified):
|- CMakeLists.txt <- This is root CMake
|- lib
|- <some source files>
|- CMakeLists.txt <- CMake file for building the library
|- demo
|- <some source files>
|- CMakeLists.txt <- CMake for building demo apps
|- extra_lib
|- <some source files>
|- CMakeLists.txt <- CMake for building supplementary library
Now, I want to build my library (living in lib
) as a shared library to be used by demo apps from demo
directory.
Additional library, that can not be a part of my library (it is essentially a wrapper for some C++ external library) is also to be compiled as a shared library and then linked to my library.
I have a problem with including dependencies for additional library. In its CMakeLists.txt
I've defined link_directories
to point location where .so
libs are stored and then target_link_libraries
to point which should be linked. At the end I did export target.
include_directories(${EXTERNAL_DIR}/include)
link_directories(${EXTERNAL_DIR}/lib)
add_library(extra_lib SHARED extra_lib.cpp)
target_link_libraries(extra_lib
some_lib
)
export(TARGETS extra_lib FILE extra_lib.cmake)
The point is that when I try to compile lib
and link it against extra_lib
I get an error that some_lib
is not found what I guess means that link_directories
is local to the extra_lib
.
Now, question is how can I make it propagate together with dependencies? I'd like it to work in the way that adding extra_lib
as subdirectory and as a dependency for my lib would automatically add linked directories from extra_lib
to the lib
linking process.
The linking process would look like:
(some external library) --> extra_lib --> lib --> demo app
Upvotes: 2
Views: 870
Reputation: 1258
First off, the CMake docs state that commands like include_directories
and link_directories
are rarely necessary. In fact, it is almost always better to use target_include_directories
and target_link_libraries
instead.
Secondly, the reason your approach fails is because you need to let CMake know about the existence of some_lib
. You can do this like so:
add_library(some_lib SHARED IMPORTED)
set_target_properties(some_lib
PROPERTIES
IMPORTED_LOCATION ${EXTERNAL_DIR}/lib/libsome_lib.so)
Then, afterwards:
target_link_libraries(extra_lib some_lib)
Upvotes: 2