0

I am build a Rubik's cube solver robot with modular programs on multiple devices. My main code is written in c++ and is shared across multiple executables. When i build my executables, I have noticed that it makes for each c++ file the same object file for each executable. Let me show with the filetree of my project, although cut to present the usefull files only.

.
├── build
│   ├── src
│   │   ├── CMakeFiles
│   │   │   ├── cubot.dir
│   │   │   │   ├── Algorithm
│   │   │   │   │   ├── Algorithm.cpp.o
│   │   │   │   │   └── ...
│   │   │   │   └── ...
│   │   │   ├── test_graphics.dir
│   │   │   │   ├── Algorithm
│   │   │   │   │   ├── Algorithm.cpp.o
│   │   │   │   │   └── ...
│   │   │   │   └── ...
│   │   │   └── ...
│   │   ├── cubot
│   │   ├── test_graphics
│   │   └── ...
│   └── ...
├── CMakeLists.txt
├── src
│   ├── Algorithm
│   │   ├── Algorithm.cpp
│   │   ├── Algorithm.h
│   │   └── ...
│   ├── CMakeLists.txt
│   └── ...
└── ..

'cubot' and 'test_graphics' are the executables I'm talking about and the each create the object file 'Algorithm.cpp.o'. I have checked and the contain the exact same binary content.

Now for the question: How do make cmake only create one copy of each object file i.e. a single 'Algorithm.cpp.o file'

Here is my main CMakeLists.txt

cmake_minimum_required(VERSION 3.24)
project(Cubot VERSION 2.1.0)
set(CMAKE_CXX_STANDARD 23)

enable_testing()

include_directories(src)

add_subdirectory(src)
#add_subdirectory(test)

And src/CMakeLists.txt

FILE(GLOB_RECURSE ALGORITHM_SRC Algorithm/*.cpp)
FILE(GLOB CUBE_SRC Cube/*.cpp)
FILE(GLOB LOGGING_SRC logging/*.cpp)
FILE(GLOB PARSERS_SRC parsers/*.cpp)
FILE(GLOB SOLVER_SRC solve/*.cpp)
FILE(GLOB SOLVER_CLIENT_SRC network/*.cpp network/client/*.cpp network/client/solver/*.cpp)
FILE(GLOB SIMULATOR_CLIENT_SRC network/*.cpp network/client/*.cpp network/client/simulator/*.cpp)
FILE(GLOB SIMULATOR_SRC simulator/*.cpp)
FILE(GLOB SERVER_SRC network/*.cpp network/server/*.cpp)

add_executable(cubot
        main.cpp
        ${ALGORITHM_SRC}
        ${CUBE_SRC}
        ${LOGGING_SRC}
        ${PARSERS_SRC}
        ${SOLVER_SRC}
        ${SOLVER_CLIENT_SRC})

find_package(Curses REQUIRED)
add_executable(server
        ${ALGORITHM_SRC}
        ${CUBE_SRC}
        ${LOGGING_SRC}
        ${PARSERS_SRC}
        ${SOLVER_SRC}
        ${SERVER_SRC})
target_link_libraries(server
        ${CURSES_LIBRARIES})

set(pybind11_DIR "${CMAKE_HOME_DIRECTORY}/venv/lib/python3.11/site-packages/pybind11/share/cmake/pybind11")
find_package(pybind11 REQUIRED)

pybind11_add_module(colors MODULE pywrap/pywrap.cpp ${SIMULATOR_CLIENT_SRC} ${SIMULATOR_SRC})

find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)

add_executable(test_graphics ${SIMULATOR_SRC} network/Link.cpp ${LOGGING_SRC} ${ALGORITHM_SRC} ${CUBE_SRC})
target_link_libraries(test_graphics
        ${OPENGL_LIBRARIES} glfw)

add_executable(test_test test.cpp ${ALGORITHM_SRC})

0 Answers0