美文网首页开发环境搭建
CMake(一)基本使用

CMake(一)基本使用

作者: 芒鞋儿 | 来源:发表于2018-10-12 11:15 被阅读23次

    CMake的优点:可以跨平台进行自动编译,生成共享lib,并且很方便加入Ctest等。Make使用makefile, CMake使用CMakefile.txt。

    Reference:
    https://cmake.org/cmake-tutorial/
    https://blog.kitware.com/meta-configuration-of-cc-projects-with-cmake/

    先建立一个简单的cpp file 如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main (int argc, char* argv[])
    {
      if(argc < 2)
      {
        fprintf(stdout, "Usage: %s number\n",argv[0] );
        return 1;
      }
      double inputValue = atof(argv[1]);
      double outputValue = sqrt(inputValue);
      fprintf(stdout, "The square root of %g is %g \n", inputValue,outputValue);
      return 0;
    }
    

    准备一下CMake的文件: CMakeLists.txt:

    cmake_minimum_required (VERSION 3.11)
    project(Tutorial)
    add_executable(Tutorial tutorial.cpp)
    

    此处cmake 可以用以下的命令行确认一下:


    スクリーンショット 2018-10-12 11.47.41.png

    Project() 是标注一下项目名字
    add_executable()是标注生成的执行文件名和使用的source 文件名。

    编译过程如下:

    スクリーンショット 2018-10-12 11.47.30.png

    cmake 执行之后生成 MakeFile, 之前用make需要自己写,用cmake确实一下轻松很多了。
    下一步是执行make 生成可执行文件:


    スクリーンショット 2018-10-12 12.07.33.png

    图中可以看到, "Tutorial" 执行文件已经生成。

    source 请参看:https://github.com/xieheng0915/eosapps/tree/master/cmakeprj

    相关文章

      网友评论

        本文标题:CMake(一)基本使用

        本文链接:https://www.haomeiwen.com/subject/cczbaftx.html