# How to use the same name for a target and a library

**URL:** https://discourse.cmake.org/t/how-to-use-the-same-name-for-a-target-and-a-library/5044
**Category:** Code
**Created:** [February 17, 2022, 5:19pm UTC](https://discourse.cmake.org/t/how-to-use-the-same-name-for-a-target-and-a-library/5044 "2022-02-17T17:19:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [February 17, 2022, 5:19pm UTC](https://discourse.cmake.org/t/how-to-use-the-same-name-for-a-target-and-a-library/5044/1 "2022-02-17T17:19:42Z")

</div>

We have a project that creates both a library and an executable. Moreover, we want the exec and library to have the same name.

For example:

| What | Windows | Linux |
| --- | --- | --- |
| executable | foobar.exe | foobar |
| static library | foobar.lib | libfoobar.a |

How do I achieve this in CMake?

I get hung up with the fact that CMake’s `add_executable` and `add_library` commands automagically add the suffix, and the name has to be unique.

For example the following wouldn’t work

```auto
add_executable(foobar ......)
add_library(foobar .....)

```

Because the name `foobar` is the same and is supposed to be unique.

Right now the legacy CMake code does some creative gymnastics to rename the generated files, but I suspect there has to be an easier way than what has been historically implemented.

What is the correct, or most efficient way to do this?

---

<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 17, 2022, 5:36pm UTC](https://discourse.cmake.org/t/how-to-use-the-same-name-for-a-target-and-a-library/5044/2 "2022-02-17T17:36:26Z")

</div>

You can set [`OUTPUT_NAME`](https://cmake.org/cmake/help/latest/prop_tgt/OUTPUT_NAME.html) on a target to modify its basename. So:

```cmake
add_library(foobar)
add_executable(foobar_exe)
set_property(TARGET foobar_exe PROPERTY OUTPUT_NAME foobar)

```

---

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [February 17, 2022, 5:39pm UTC](https://discourse.cmake.org/t/how-to-use-the-same-name-for-a-target-and-a-library/5044/3 "2022-02-17T17:39:28Z")

</div>

Hahahahahaha… BEAUTIFUL.

I **knew** there had to be an easier way than the gymnastics I saw. Thank you Ben!
