Should I separate CMakeList.txt from the source folder?

I am new to creating the tool, when I come across Autotool, I have the opportunity to write only one Makefile.am file in the top assembly and leave the source folder containing all cpp files clean. I could use VPATH to tell automake to look for source code inside this folder instead of write / src / every where. (see my old question here: Automake Variables to remove Makefile.am )

However, it seems to me that CMake does not have VPATH, and some foxes around said it was impossible to do. Thus, there are two options:

  • Create CMakeList.txt in the source folder
  • Create CMakeList.txt in the top build folder and leave the source code alone, with the costs that I need to extend "/ src" for all source code files.

Which one is most commonly used? I prefer the second because it leaves my source code clean from any source related to the build. In this case, you still need to get rid of "/ src"?

+4
source share
2 answers

In our company, we use the first option with CMakeLists.txt in each subdirectory and create a tree from the root-CMakeLists.txt file using the add_subdirectory .

This is a modular approach in which each subcomponent (think that the project has different parts, such as boost, is divided into the system, stream, time_date, etc.) has its own build file. If the user wants him to just build a subcomponent or build an entire project.

We additionally use this as an easy way to include additional subcomponents in the project. The user can then set the Bool value to BUILD_SUBFOO , and add_subdirectory will be executed if this Bool is TRUE .

Well-known projects also use this approach. Here is a link to the root CMakeLists.txt from KDevelop (see lines 52-62).

+5
source

I am sure you can do:

 FILE(GLOB Source_files src/*.cpp) 

which will do exactly what you want.

0
source

All Articles