1

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)?

Community
  • 1
  • 1
Courier
  • 920
  • 2
  • 11
  • 36
  • Could you include the CMakeLists.txt for which `target_link_libraries({MY_LIB} {MY_LIB})` is failing? – buratino May 31 '16 at 12:05

1 Answers1

2

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 e.g.

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • target_link_library or target_link_libraries? What is the difference please? – Courier May 31 '16 at 12:19
  • Am getting this error message `Unknown CMake command "target_link_library"` – Courier May 31 '16 at 12:34
  • @polar [`target_link_libraries`](https://cmake.org/cmake/help/v3.4/command/target_link_libraries.html). And you need to tell CMake the dependencies you have, otherwise it could build library B before library C, and for a shared library that won't work. If you only have static libraries, and the order doesn't matter (and there's no auto-generated files that parts of your project depends on) then you can skip the order, and add all libraries for the `program` target. – Some programmer dude May 31 '16 at 12:47
  • But I was able in the near past to link the libraries regardless of their order, by just duplicating the the libraries; e.g. `target_ling_libraries(program {MY_LIB} {MY_LIB}.... {MY_LIB})`. I do not understand why I would need to take care of the order ONLY NOW... Very strange – Courier Jun 01 '16 at 03:45
  • @polar What a surprise, you got [again the same answer](http://stackoverflow.com/a/37541138/2436175). :P – Antonio Jun 01 '16 at 12:44