# target\_link\_libraries just referencing?

**URL:** https://discourse.cmake.org/t/target-link-libraries-just-referencing/6050
**Category:** Usage
**Created:** [July 12, 2022, 3:59am UTC](https://discourse.cmake.org/t/target-link-libraries-just-referencing/6050 "2022-07-12T03:59:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [July 12, 2022, 3:59am UTC](https://discourse.cmake.org/t/target-link-libraries-just-referencing/6050/1 "2022-07-12T03:59:56Z")

</div>

Hi,  
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})`

–Folder\_A  
— libA.a  
–Folder\_B  
— 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:

```auto
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?

Thanks,

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [July 16, 2022, 10:10am UTC](https://discourse.cmake.org/t/target-link-libraries-just-referencing/6050/2 "2022-07-16T10:10:27Z")

</div>

> [@Glad](#):
>
> static libraries should copy the objects in their archive.

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

---

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [July 16, 2022, 10:16am UTC](https://discourse.cmake.org/t/target-link-libraries-just-referencing/6050/3 "2022-07-16T10:16:53Z")

</div>

> `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 👍
