parallel build (2) : add_subdirectory to a non-subdirectory

In the meanwhile, I could figure out what is the problem.

To mention also: its a Linux CMake build.

I had instead of this

  • A set(MY_STATIC_LIB_NAME myStaticLib)

used this

  • B set(MY_STATIC_LIB_NAME libmyStaticLib.a)

The call below (target_link_libraries) functions with both variants.

target_link_libraries(${EXE_NAME} PUBLIC ${MY_STATIC_LIB_NAME})

But the build behaves differently:

  • A. Everything works fine, i.e. the lib is build in parallel, and the linker is called after its been built.
  • B. CMake didn’t make a connection between libmyStaticLib.a and myStaticLib from the add_subdirectory call. Thus the two were build separately. The linker stripped though the libmyStaticLib.a to myStaticLib and issued an error (/usr/bin/ld: cannot find -lmyStaticLib) of not finding the library.

Two final questions

  1. Is the above CMake behavior a bug or a feature ?
  2. How does CMake internally handles build dependencies, so that it wont call the linker before everything is build, i.e. how is build order handled?