I'm not asking this for just myself. I hope this question will be a reference for the many newbies who like me, found it utterly perplexing about what exactly what was going on behind the scenes when for such a small CMakeLists.txt
file
cmake_minimum_required (VERSION 2.6)
project(Tutorial)
add_executable(Tutorial tutorial.cpp)
and such a small tutorial.cpp
int main() { return 0; }
there are so many files generated
CMakeCache.txt cmake_install.cmake Makefile
CMakeLists.txt tutorial.cpp
and a CMakeFiles
folder with so many files and folders
CMakeCCompiler.cmake CMakeOutput.log Makefile.cmake
cmake.check_cache CMakeSystem.cmake progress.marks
CMakeCXXCompiler.cmake CMakeTmp TargetDirectories.txt
CMakeDetermineCompilerABI_C.bin CompilerIdC Tutorial.dir
CMakeDetermineCompilerABI_CXX.bin CompilerIdCXX
CMakeDirectoryInformation.cmake Makefile2
Not understanding what was going on behind the scenes (i.e: why so may files had to be generated and what their purpose was), was the biggest obstacle in being able to learn CMake.
If anyone knows, could you please explain it for the sake of posterity? What is the purpose of these files, and when I type cmake .
, what exactly is cmake configuring and generating before it builds the project?
The secret is that you don't have to understand what the generated files do.
CMake introduces a lot of complexity into the build system, most of which only pays off if you use it for building complex software projects.
The good news is that CMake does a good job of keeping a lot of this messiness away from you: Use out-of-source builds and you don't even have to look at the generated files. If you didn't do this so far (which I guess is the case, since you wrote
cmake .
), please check them out before proceeding. Mixing the build and source directory is really painful with CMake and is not how the system is supposed to be used.In a nutshell: Instead of
always use
instead. I usually use an empty subfolder
build
inside my source directory as build directory.To ease your pain, let me give a quick overview of the relevant files which CMake generates:
ccmake
orcmake-gui
. This can be useful to look at from time to time, but I would recommend to use the aforementioned tools for changing any of the values if possible.CMakeFiles
subdirectory.In general you should not mess with any of the files that CMake generates for you. All problems can be solved from within
CMakeLists.txt
in one way or the other. As long as the result builds your project as expected, you are probably fine. Do not worry too much about the gory details - as this is what CMake was trying to spare you of in the first place.As stated on its website:
In most cases it is used to generate project/make files - in your example it has produced
Makefile
which are used to build your software (mostly on Linux/Unix platform).Cmake allows to provide cross platform build files that would generate platform specific project/make files for particular compilation/platform.
For instance you may to try to compile your software on Windows with Visual Studio then with proper syntax in your
CMakeLists.txt
file you can launchinside your project's directory on Windows platform,Cmake will generate all the necessary project/solution files (
.sln
etc.).If you would like to build your software on Linux/Unix platform you would simply go to source directory where you have your
CMakeLists.txt
file and trigger the samecmake .
and it will generate all files necessary for you to build software via simplemake
ormake all
.Here you have some very good presentation about key Cmake functionalities http://www.elpauer.org/stuff/learning_cmake.pdf
EDIT
If you'd like to make platform dependent library includes / variable definitions etc. you can use this syntax in
CMakeLists.txt
fileThere is also a lot of commands with use of which you are able to prevent the build from failing and in place Cmake will notify you that for instance you do not have boost libraries
filesystem
andregex
installed on your system. To do that you can use the following syntax:Having checked that it will generate the makefiles for appropriate system/IDE/compiler.