0
votes

I've recently began experimenting with CMake, and have written a simple Hello, World program in C++.

Here is my directory structure:

CMakeLists.txt
src/
    CMakeLists.txt
    main.cpp
build/

The top-level CMakeLists.txt reads as follows:

#Require at least CMake version 2.8
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

#set up the project name, version and language to compile in
project(HelloWorld)

#tell CMake that we have some source files located in the src directory
add_subdirectory(src)

And the CMakeLists.txt in /src reads:

add_executable(helloworld 
                main.cpp)

install(TARGETS helloworld
            RUNTIME DESTINATION ../build)

Now, I would expect that these two scripts would cause the makefile to create the helloworld binary in the /build directory, but after running make, it creates the helloworld binary in /build/src.

If I move the add_executable and install function calls into the top-level CMakeLists.txt, then the helloworld binary is placed in /build, not /build/src.

So, as I understand, it places the build in the folder relative to where CMake was invoked. Why does it appear as though install is doing nothing, then? Additionally, what if I have multiple complex subdirectories? How should I write out my CMakeLists.txt while avoiding file(GLOB ...)?

For example;

CMakeLists.txt
/build
/src
   /class_A
       a.hpp
       a.cpp
   /class_B
      b.hpp
      b.cpp
      /class_CB
         cb.hpp
         cb.cpp
   /class_D
      d.hpp
      d.cpp

Would I just have a giant list of .cpp files, all with relative paths to each cpp file, and pass that list into the add_executable(executable ${SOURCE_FILES})? Or is there an easier way with using multiple CMakeLists.txt?

1

1 Answers

1
votes

You misinterpret the install command. It is used to install your files, for example with make install.

When you configure your project, CMake will mimic the structure of your project. This means all folders are created in the same structure and the binaries appear in the according build directories.

Either place your targets in the main directory, which I would consider bad stile. Better live with build/src or give it a more meaningful name.