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

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

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?

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

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.

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

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:

pybind11_add_module(grapoli_py "src/grapoli_lib.cpp")

or construct an absolute path:

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

I hope this helps!

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>