# Function Linkage difference between binary and library

**URL:** https://discourse.cmake.org/t/function-linkage-difference-between-binary-and-library/10153
**Category:** Usage
**Tags:** os:linux
**Created:** [February 21, 2024, 9:04pm UTC](https://discourse.cmake.org/t/function-linkage-difference-between-binary-and-library/10153 "2024-02-21T21:04:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Vekleon](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Vekleon](https://discourse.cmake.org/u/Vekleon)
#### Post date: [February 21, 2024, 9:04pm UTC](https://discourse.cmake.org/t/function-linkage-difference-between-binary-and-library/10153/1 "2024-02-21T21:04:53Z")

</div>

I’m seeing that when I compile an executable with a large library, and I check the function definitions via `nm` I see that the functions in the binary is defined i.e

```auto
nm program
....... T funcnameFromLibrary

```

and the CMakeList.txt file is along the lines of

```auto
some_setup
...
add_executable(${PROJECT_NAME} program.cpp ...)
target_link_libraries(${PROJECT_NAME} ${SOMELIBRARY_LINK_LIBRARIES})

```

However when I do the same thing but make it a library (i.e. use add\_library instead of add\_executable) I see the following when I run `nm`

```auto
nm program.a
......... U funcnameFromLibrary

```

Am I missing something else when I’m linking the function for library compilation? my CMakeList.txt is the exact same except for the fact that I use add\_library instead of add\_executable

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 27, 2024, 1:57pm UTC](https://discourse.cmake.org/t/function-linkage-difference-between-binary-and-library/10153/2 "2024-02-27T13:57:55Z")

</div>

Static libraries do not copy symbols from anywhere other than the objects passed in. I suspect that `funcnameFromLibrary` comes from a static library. Building a shared library or executable against a static library will copy any object files which provide symbols into the resulting binary. This causes it to be defined in those but “undefined” in a static library.

There is an issue for a new library type which is a static library that copies in any used objects from linked static libraries: [https://gitlab.kitware.com/cmake/cmake/-/issues/22679](https://gitlab.kitware.com/cmake/cmake/-/issues/22679)

---

<div class="post-metadata">

### Author: ![Vekleon](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Vekleon](https://discourse.cmake.org/u/Vekleon)
#### Post date: [February 28, 2024, 8:40pm UTC](https://discourse.cmake.org/t/function-linkage-difference-between-binary-and-library/10153/3 "2024-02-28T20:40:29Z")

</div>

Ye, I added SHARED in my add\_library build, and that seemed to fix the exporting of the functions with what I need, running into a whole new weird issue now.
