【使用CMake 组织C工程】CMake Hello World

【前言】
本文介绍了如何使用CMake来构建一个Hello World的C工程.

【CMake使用惯例流程】

使用过CMake构建项目的朋友都知道,CMake的使用有如下的“惯例”:

 1. 在项目根目录建立一个build目录:mkdir build && cd build.(该目录用于存储编译生成的相应文件)
 2. 进入build目录,编译源码生成Makefile
 3. 确定生成Makefile成功,执行make.(或者打开生成的工程文件,如Visual Studio项目文件或者是Xcode项目文件)

【目录结构】

下面我要建立一个Hello World也是按照这个过程来使用CMake构建。

整个项目目录结构如下


.
└── cmaketest
    ├── build
    ├── CMakeLists.txt
    └── hello.c


主要过程

第一步:新建hello.c
#include <stdio.h>  
int main()  
{  
  printf("hello,this is my first using cmake project/n");  
  return 0;
}  
第二步:新建CMakeLists.txt
add_executable(hello hello.c)
第三步:使用“惯例”方法来构建
  1. 在cmaketest下创建一个build目录

  2. 进入build目录,编译源码生成Makefile

    cmake [选项] <源码路径>

    所以在build下执行cmake ../

    这里写图片描述

    [phpuser@node1 build]$ cmake ../
    输出如下:
    -- The C compiler identification is GNU 4.8.5
    -- The CXX compiler identification is GNU 4.8.5
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /phpuser/rubbish/cmaketest/build

    此时查看build目录下内容:

    [phpuser@node1 build]$ ll -a
    总用量 28
    drwxrwxr-x 3 phpuser phpuser    89 1月  30 15:10 .
    drwxrwxr-x 3 phpuser phpuser    56 1月  30 15:10 ..
    -rw-rw-r-- 1 phpuser phpuser 12617 1月  30 15:10 CMakeCache.txt
    drwxrwxr-x 5 phpuser phpuser   303 1月  30 15:10 CMakeFiles
    -rw-rw-r-- 1 phpuser phpuser  1504 1月  30 15:10 cmake_install.cmake
    -rw-rw-r-- 1 phpuser phpuser  4748 1月  30 15:10 Makefile

    可以看到生成的Makefile

  3. 执行 make

    这里写图片描述

    [phpuser@node1 build]$ make
    Scanning dependencies of target hello
    [ 50%] Building C object CMakeFiles/hello.dir/hello.c.o
    [100%] Linking C executable hello
    [100%] Built target hello

    再次查看build目录下内容,可以看到生成的hello文件

    [phpuser@node1 build]$ ll -a
    总用量 40
    drwxrwxr-x 3 phpuser phpuser   102 1月  30 15:20 .
    drwxrwxr-x 3 phpuser phpuser    56 1月  30 15:10 ..
    -rw-rw-r-- 1 phpuser phpuser 12617 1月  30 15:10 CMakeCache.txt
    drwxrwxr-x 5 phpuser phpuser   303 1月  30 15:20 CMakeFiles
    -rw-rw-r-- 1 phpuser phpuser  1504 1月  30 15:10 cmake_install.cmake
    -rwxrwxr-x 1 phpuser phpuser  8520 1月  30 15:20 hello
    -rw-rw-r-- 1 phpuser phpuser  4748 1月  30 15:10 Makefile
  4. 运行 ./hello

    [phpuser@node1 build]$ ./hello 
    hello,this is my first using cmake project


总结

以上就是使用CMake来构建一个Hello World C++项目的过程,可以看到相对于自己编写Makefile,使用CMake是简单很多的,仅需要在CMakeLists.txt加入一句:

add_executable(hello, hello.c)

就搞定了。

CMakeLists.txt中可以配置很多参数,具体可以查阅

CmakeList的编写和参数详解

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Casionx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值