Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I separate CMakeList.txt from source folder?

Tags:

c++

build

cmake

I'm new to build tool, when I come across Autotool, I have an option to write only one Makefile.am in the top build folder and leave the source folder containing all cpp files clean. I could use VPATH to tell automake to look for the source code inside that folder instead of write /src/ every where. (refer to my old question here: Automake Variables to tidy up Makefile.am)

However It appears to me that CMake have no VPATH to set and some fox around said that It's impossible to do so. So there are two choices:

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

Which one is more commonly use? I prefer the second because It leave my source code clean from any build-relating-source. In that case is there anyway to get rid of "/src"?

like image 478
w00d Avatar asked Jul 07 '11 17:07

w00d


2 Answers

In our company we're using the first option, having a CMakeLists.txt in every subdirectory and building the tree from a root-CMakeLists.txt by using the add_subdirectory command.

This is a modular approach where each subcomponent (think about the project has different parts like boost is split up into system, thread, date_time etc) has its own build-file. If a user wants he is able to just build the subcomponent or to build the whole project.

We're additionally using this as an easy way to include optional subcomponents to the project. The user then can set a Bool value like BUILD_SUBFOO and the add_subdirectory will just be executed, if this Bool is TRUE.

Well-known projects are using this approach too. Here is a link to the root CMakeLists.txt from KDevelop (look at lines 52-62).

like image 91
MOnsDaR Avatar answered Oct 01 '22 02:10

MOnsDaR


I'm quite sure you can do:

FILE(GLOB Source_files src/*.cpp)

which would do exactly what you want.

like image 20
rubenvb Avatar answered Oct 01 '22 01:10

rubenvb