简体   繁体   中英

Interdependancy of C++ libraries with CMake

What and how to do with CMake files in such a away one would not have to take care of the dependency order? (see my previous question related the issue Strange error: undefined reference to `class::class()' ).

For instance if you have lib A dependent of lib B, which by its turn is dependent of lib C one would code

add_library({MY_LIB} A B C)

How to do to not be forced to follow the order? In the near past I just did

target_link_libraries({MY_LIB} {MY_LIB})

But this is no longer working for me.... I do not know why (???). This problem is quite irritating since I have a large number of interdependent libraries...

Any suggestion please (am using cmake 3.5.2, gcc version 4.8.4 on Ubuntu 4.8.4-2ubuntu1~14.04.3)?

An add_library command to create each library, and then set the dependencies with target_link_libraries should be enough.

In your case you would have eg

add_library(A ${sources_for_A})
add_library(B ${sources_for_B})
add_library(C ${sources_for_C})

target_link_libraries(A B)  # A depends on B
target_link_libraries(B C)  # B depends on C

# Executable using the libraries
add_executable(program ${sources_for_program})
target_link_libraries(program A)  # Program uses library A (and B and C indirectly)

It should not matter if the libraries are STATIC or SHARED .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM