How exactly does CMake work?

后端 未结 3 1397
猫巷女王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:11

    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 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 launch

    cmake .
    

    inside 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 same cmake . and it will generate all files necessary for you to build software via simple make or make 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 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.

提交回复
热议问题