一、入门
首先编写一个输出helloworld的文件,名为main.cpp
#include <iostream>int main() {std::cout << "HelloWorld << std::endl;return 0;}
然后在同目录下,新建文件CMakeLists.txt文件,内容如下
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (hello)
# 指定生成目标
add_executable(hello main.cpp)
其中每个命令的作用分别为:
cmake_minimum_required:指定运行此配置文件所需的 CMake 的最低版本project:参数值是hello,该命令表示项目的名称是hello。add_executable: 将名为main.cpp的源文件编译成一个名称为hello的可执行文件。
然后编译项目:cmake .可以生成一些文件,然后使用make即可编译成功
二、项目中有多个源文件
可以使用aux_source_directory命令将指定文件夹内的所有源文件设置到一个变量中
aux_source_directory(${path} ${var_name})
然后在add_executable中使用这个变量即可
aux_source_directory(. dir)
add_executable(hello ${dir})
