可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
cd cmake .
always use
cd cmake
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:
- Project files/Makefiles - What you are actually interested in: The files required to build your project under the selected generator. This can be anything from a Unix Makefile to a Visual Studio solution.
- CMakeCache.txt - This is a persistent key/value string storage which is used to cache value between runs. Values stored in here can be paths to library dependencies or whether an optional component is to be built at all. The list of variables is mostly identical to the one you see when running
ccmake
or cmake-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. - Generated files - This can be anything from autogenerated source files to export macros that help you re-integrate your built project with other CMake projects. Most of these are only generated on demand and will not appear in a simple project such as the one from your question.
- Anything else is pretty much noise to keep the build system happy. In particular, I never needed to care about anything that is going on inside the
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.
回答2:
As stated on its website:
Cmake is cross-platform, open-source build system for managing the build process of software using a compiler-independent method
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 compilator/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 launch
cmake .
inside your project's directory on Windows platform and 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 same cmake .
and it will generate all files necessary for you to build software via sipmle make
or make all
.
Here you have some very good presentation about key Cmake funcitonalities 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
file
IF(WIN32) ...do something... ELSE(WIN32) ...do something else... ENDIF(WIN32)
There 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
and regex
installed on your system. To do that you can use the following syntax:
find_package(Boost 1.45.0 COMPONENTS filesystem regex)
Having checked that it will generate the makefiles for appropriate system/IDE/compiler.