cmake-基础脚本
CMakeList
安装
- sudo apt install cmake
- 源码安装,官方下载,命令行编译安装
基础脚本
CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
message("myProject")
add_executable(myExec main.cc)
main.cc
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
cout << "Hello" << endl;
return 0;
}
编译方法
mkdir build && cd buildcmake .. or cmake -G "Unix Makefiles" ..make or cmake --build .
project
- project(myProject LANGUAGES C CXX)
- 第一个是项目名字,参数LANGUAGES :表示需要寻找的编译器
- https://cmake.org/cmake/help/latest/command/project.html
使用Ninja
-
安装
sudo apt install ninja -
进入build之后,
cmake -GNinja ..orcmake -G "Ninja" ..,在之后是ninjaorcmake --build . -
注意,ninja是构建器,不是编译器,是利用并行加快构建
多文件
.
├── build
├── CMakeLists.txt
├── main.cc
├── math.cc
└── math.hh
main.cc
#include <iostream>
#include "math.hh"
using namespace std;
int main(int argc, char **argv)
{
cout << "Hello" << endl;
sum();
return 0;
}
math.cc
#include <iostream>
void sum(void)
{
std::cout << "sum is 23" << std::endl;
}
math.hh
#ifndef _MATH_HH_
#define _MATH_HH_
void sum(void);
#endif
CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
message("Hello myProject")
project(myProject LANGUAGES C CXX)
add_executable(myExec main.cc math.cc)

浙公网安备 33010602011771号