0

My current project structure looks like this:

Project
      --src/
           --CMakeLists.txt 
           --Graph.cpp
           --Graph.hpp
           --main.cpp
      --build/
      --CMakeLists.txt

The CMakeLists.txt in the Project folder lookalike this:

cmake_minimum_required(VERSION 3.18)

project(GRAPHTHEORY VERSION 1.0.0)

set (CMAKE_CXX_STANDARD 17)

add_subdirectory(src)

the CMakeLists.txt in src file looks like this:


add_library(Graph Graph.hpp Graph.cpp)

add_executable(graph_theory main.cpp)

target_link_libraries(graph_theory PRIVATE Graph )

When I go into the build/ directory, and run cmake .. and then make, the executable graph_theory is in a new src/ directory within build

I am not sure why a new src/ directory is being created in my `build/' directory.

Edit:

So what I was trying to do is replicate running

cmake -S src/ -B build/ from Project directory.

When I do the above and then cd into build/ and then run make, the executable is created in build/ directory.

2
  • it holds the build files related to the source files in the src directory? Commented Jan 15, 2021 at 17:36
  • @user253751 is there a way for the executable to appear in the build directory directly? I mean so what I am describing is expected behavior? Commented Jan 15, 2021 at 17:37

1 Answer 1

2

Build artifacts and cache is kept in a directory that reflect the subdirectory you added in CMake with the add_subdirectory command.

However, you can change where the final target end up by changing the default value of RUNTIME_OUTPUT_DIRECTORY. For example, this code will put it in the bin directory:

set_target_properties(graph_theory
PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/"
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I added some additional info and the reason why I was doing this in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.