cmake 精简入门

c++文件源代码:main.cc

#include <stdio.h>
#include <stdlib.h>

class MyCalc
{
public:
  int a;
  int b;
  int getAdd();
};

int MyCalc::getAdd()
{
  return a+b;
}

int main(int argc, char *argv[])
{
  printf("====Welcome cpp test=======\n");
  MyCalc mc;
  mc.a=10;
  mc.b=90;
  printf("Add res = %d \n", mc.getAdd() );

  return 0;
}

当前目录下的CMakeLists.txt

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo1)
# 指定生成目标
add_executable(Demo main.cc)

然后在当前目录下新建一个文件夹build (任意定义,推荐名称build)

然后进入build目录下终端运行命令: cmake ..

(cmake后跟着点点,代表运行当前目录前一级的Lists配置,输出的缓存文件在当前build目录)

终端显示:

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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: /home/bnis/Desktop/TestCode/build

看到build目录下生成很多中间文件,其中有个是Makefile,然后执行make  

[ 50%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo

得到最终输出执行文件Demo

若需要重新cmake,需要清空build文件夹。在build当前目录运行 rm -rf *

自动以最大线程数编译cmake
sudo cmake --build . --target all -- -j $(nproc)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值