# cmake pybind11 cannot create target because another target with the same name already exists, how to bypass?

**URL:** <https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [April 24, 2020, 6:09pm UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097 "2020-04-24T18:09:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ragnarokas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ragnarokas/32/904_2.png) [@ragnarokas](https://discourse.cmake.org/u/ragnarokas)\
**Post date:** [April 24, 2020, 6:09pm UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097/1 "2020-04-24T18:09:52Z")

</div>

Hello, I have the code, which cmakelists.txt looks like:

```
> cmake_minimum_required(VERSION 3.14)
> project(grapoli_lap)
> set(CMAKE_CXX_STANDARD 17)
> 
> set(SOURCE_FILES
> unit_test/geometry/monomer_test.cpp
> unit_test/geometry/monomer_test.hpp
> unit_test/geometry/polymer_test.cpp
> unit_test/geometry/polymer_test.hpp
> unit_test/geometry/unit_box_test.cpp
> unit_test/geometry/unit_box_test.hpp
> unit_test/geometry/rect_shape_3d_test.cpp
> unit_test/geometry/rect_shape_3d_test.hpp
> src/general/guard.cpp
> src/general/guard.hpp
> src/general/fmt_enum.hpp
> src/general/string_format.cpp
> src/general/string_format.hpp
> src/geometry/monomer.cpp
> src/geometry/monomer.hpp
> src/geometry/polymer.cpp
> src/geometry/polymer.hpp
> src/geometry/unit_box.cpp
> src/geometry/unit_box.hpp
> src/geometry/rect_shape_3d.cpp
> src/geometry/rect_shape_3d.hpp
> )
> include_directories(src/general)
> include_directories(src/geometry)
> include_directories(unit_test/general)
> include_directories(unit_test/geometry)
> 
> add_executable(
> grapoli_lap ${SOURCE_FILES}
> unit_test/general/string_format_test.cpp
> unit_test/general/string_format_test.hpp
> unit_test/geometry/monomer_test.cpp
> unit_test/geometry/monomer_test.hpp
> unit_test/geometry/polymer_test.cpp
> unit_test/geometry/polymer_test.hpp
> unit_test/geometry/unit_box_test.cpp
> unit_test/geometry/unit_box_test.hpp
> unit_test/geometry/rect_shape_3d_test.cpp
> unit_test/geometry/rect_shape_3d_test.cpp
> )
> 
> target_link_libraries(grapoli_lap gtest gtest_main)

```

I am adding pybind11 according the instructions:

```
add_subdirectory(pybind11)
pybind11_add_module(grapoli_lap grapoli_lib.cpp)

```

However, I am getting the error:

> add\_executable cannot create target “grapoli\_lap” because another target  
> with the same name already exists. The existing target is a module library  
> created in source directory “C:/Users/Alex/Dropbox/PC/grapoli\_lap”. See  
> documentation for policy CMP0002 for more details.

Now I understand that I cannot have two targets with the _same_ name yet I don’t get where exactly is the same name here and how to prevent this

Thank you  
Alex

---

<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:** [April 24, 2020, 7:43pm UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097/2 "2020-04-24T19:43:25Z")

</div>

`pybind11_add_module(grapoli_lap ...)` creates a target named `grapoli_lap`.  
`add_executable(grapoli_lap ...)` attempts to create another target named `grapoli_lap`

What is `grapoli_lap` supposed to be? An executable or a Python module?

---

<div class="post-metadata">

**Author:** ![ragnarokas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ragnarokas/32/904_2.png) [@ragnarokas](https://discourse.cmake.org/u/ragnarokas)\
**Post date:** [April 24, 2020, 7:52pm UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097/3 "2020-04-24T19:52:47Z")

</div>

grapoli\_lap is supposed to be an executable. I tried to use another name for pybind11, for example, pybind11\_add\_module(grapoli\_py grapoli\_lib.cpp) but then I get an error that source file cannot be found: grapoli\_lib.cpp

---

<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:** [April 24, 2020, 9:25pm UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097/4 "2020-04-24T21:25:05Z")

</div>

Are both `grapoli_lib.cpp` and the `CMakeLists.txt` file that contains `pybind11_add_module(grapoli_py grapoli_lib.cpp)` located in the same folder?

You can use `${CMAKE_CURRENT_LIST_DIR}` to construct an absolute path from the folder that contains the `CMakeLists.txt` file.

---

<div class="post-metadata">

**Author:** ![ragnarokas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ragnarokas/32/904_2.png) [@ragnarokas](https://discourse.cmake.org/u/ragnarokas)\
**Post date:** [April 25, 2020, 9:44am UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097/5 "2020-04-25T09:44:48Z")

</div>

Thank you for you help. I am sorry but I have a very poor skills in cmake. How exactly should I use `${CMAKE_CURRENT_LIST_DIR}`? Currently` CmakeLists.txt` is in grapoli\_lap folder and `grapoli_lib.cpp` is in grapoli\_lap/src folder

---

<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:** [April 25, 2020, 10:01am UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097/6 "2020-04-25T10:01:58Z")

</div>

Since `grapoli_lib.cpp` is in a sub-folder from the point-of-view of the `CMakeLists.txt` file, you can either use a relative path:

```cmake
pybind11_add_module(grapoli_py "src/grapoli_lib.cpp")

```

or construct an absolute path:

```cmake
pybind11_add_module(grapoli_py "${CMAKE_CURRENT_LIST_DIR}/src/grapoli_lib.cpp")

```

I hope this helps!

---

<div class="post-metadata">

**Author:** ![ragnarokas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ragnarokas/32/904_2.png) [@ragnarokas](https://discourse.cmake.org/u/ragnarokas)\
**Post date:** [April 25, 2020, 2:56pm UTC](https://discourse.cmake.org/t/cmake-pybind11-cannot-create-target-because-another-target-with-the-same-name-already-exists-how-to-bypass/1097/7 "2020-04-25T14:56:06Z")

</div>

Thanks, it helped indeed, however, the built is still not successful. I am following the manual, adding

> add\_subdirectory(pybind11)  
> pybind11\_add\_module(grapoli\_py “src/grapoli\_lib.cpp”)

So now there is no cmake mistake anymore, but grapoli\_lib.cpp still cannot find the header file:

> No such file or directory #include \<pybind11/pybind11.h\>
