0

I am very new in cmake. I am trying to build a simple test project. This is my directory tree.

testproj
   |---CMakeLists.txt
   |
   |---build (directory)
   |
   |---bin (directory)
   |
   |---include
   |   |---add.h
   |
   |---lib
   |   |---add.cxx
   |
   |---test
   |   |---main.cxx

After configuring and building, I want the static library *.a and the binary executable to go into bin/ subdirectory. So currently, my CmakeLists.txt looks like this.

CmakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(testproj)
set(CMAKE_CXX_STANDARD 20)

set(EXECUTABLE_OUTPUT_PATH "bin/")
set(LIBRARY_OUTPUT_PATH "bin/")

file(GLOB main_src "test/main.cxx")
file(GLOB lib_src "lib/*.cxx")

add_library(lib STATIC ${lib_src})
add_executable(main ${main_src})

target_link_libraries(main lib)

After building the project, the source directory looks like this.

testproj
   |---CMakeCache.txt
   |
   |---CMakeFiles (directory)
   |
   |---CMakeLists.txt
   |
   |---Makefile
   |
   |---bin
   |   |---liblib.a
   |   |---main
   |
   |---build (directory)
   |
   |---cmake_install.cmake
   |
   |---compile_commands.json
   |
   |---include
   |   |---add.h
   |
   |---lib
   |   |---add.cxx
   |
   |---test
   |   |---main.cxx

Library *.a and binary executable main went into the bin/ subdirectory as expected. But, I want the following files and directories to go into the build/ subdirectory everytime I build the project:

CMakeCache.txt, CMakeFiles, Makefile, cmake_install.cmake, compile_commands.json

How can I achieve this in CMakeLists.txt? I don't want to do

cd build
cmake ..
make

EDIT: I configure from the testproj directory. I don't want to cd into the build/ subdirectory.

SuperNoob
  • 170
  • 1
  • 10
  • `After configuring and building` How do you configure? – KamilCuk Jul 05 '21 at 16:19
  • 1
    Files like `CMakeCache.txt`, `CMakeFiles` are created in the directory which is specified as a **build** directory for CMake. Normally, this is the current directory, but it is possible to pass this directory as a parameter to `cmake`. Note that placing **output** files **outside** of the build directory is a bad practice. This is what you want to achieve by putting the library and the executable under `bin/` while building the project under `build/`. Instead you could **install** your files under any location. See what `install` CMake command does. – Tsyvarev Jul 05 '21 at 16:26
  • @Tsyvarev My question is already marked as duplicate and the given link doesn't answer my question. I would look into "install". How can I pass the current directory as a parameter in cmake? Shouldn't it be possible just to change into the build/ subdirectory and build the project in CMakeLists.txt? – SuperNoob Jul 05 '21 at 16:51
  • 1
    [That answer](https://stackoverflow.com/a/52024730/3440745) describes how to tell CMake to use particular directory (instead of the current one) as a build directory. The script `CMakeLists.txt` cannot affect on a build directory. – Tsyvarev Jul 05 '21 at 17:08

0 Answers0