How exactly does CMake work?

后端 未结 3 1401
猫巷女王i
猫巷女王i 2020-12-12 08:27

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 goin

3条回答
  •  鱼传尺愫
    2020-12-12 09:17

    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.

提交回复
热议问题