target_link_libraries just referencing?

I am not sure to understand something.

  • Context:

I have separate folders, each one having header/source files and a CMakelist.txt. Each CMakelist defines a project. They are created with:

add_library(${APP_NAME} STATIC ${SOURCES})

— libA.a
— libB.a

Now, libB.a references libA.a inside its CMakelists.txt, with:
target_link_libraries(${APP_NAME} PUBLIC ${Apps_folder_root}/Folder_A/libA.a)

The goal is to create multiple small libs that I reuse in other projects.
I need them to be static because from the final environment, I only know the architecture and OS my code will be running on. Embedding everything in one package is easier.

  • Question:

I added a “cout << “test” << endl;” in my libA sources and recompiled that library ONLY using ninja in the libA build directory.

I created an app which references the two libs:

add_executable(${APP_NAME} ${SOURCES} main.cpp )
target_link_libraries(${APP_NAME} PUBLIC ${Apps_folder_root}/Folder_A/libA.a)
target_link_libraries(${APP_NAME} PUBLIC ${Apps_folder_root}/Folder_B/libB.a)

In that final app, the prints appear. Note, I didn’t recompile libB.a

Did the linker erase the version of libA it found in libB, and just replaced it with the original (and updated) version?
I get that static libraries should copy the objects in their archive. So what happened here?
Is it an expected behavior with target_link_libraries?


Exactly. They copy the objects, not other libraries. libB doesn’t contain a copy of libA.

libB doesn’t contain a copy of libA.

Ah! this is the part I missed. I thought libA was copied into it.

All clear!

Thanks :+1: