一、为什么要使用 cmake

  1. cmake 可以实现 c/c++ 跨平台代码的编译

二、cmake 简单使用

    # the minimum version 
    cmake_minimum_required(VERSION 3.10)
    
    # set the project name
    project(Tutorial VERSION 1.0)

    # set verbose
    set(CMAKE_VERBOSE_MAKEFILE on)

    # add the executable 
    add_executable(Tutorial tutorial.cxx)

    configure_file(TutorialConfig.h.in TutorialConfig.h)
    target_include_directories(Tutorial PUBLIC
                            "${PROJECT_BINARY_DIR}"
                            )

    # specify the C++ standard
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED True)
    //TutorialConfig.h.in
    // the configured options and settings for Tutorial
    #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
    #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
    // tutorial.cxx
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include "TutorialConfig.h"



    int main(int argc, char* argv[]) {
        if (argc < 2) {
            // report version
            std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
                    << Tutorial_VERSION_MINOR << std::endl;
            std::cout << "Usage: " << argv[0] << " number" << std::endl;
            return 1;
        }

        // convert input to double
        const double inputValue = atof(argv[1]);

        // calculate square root
        const double outputValue = sqrt(inputValue);
        std::cout << "The square root of " << inputValue << " is " << outputValue
                << std::endl;
        return 0;
    }