一、 A-hello-camke项目构成
1 C++项目
main.cpp
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello CMake!" << std::endl;
return 0;
}
2 CMakeLists.txt
#设置可用于查找CMake版本运行的CMake的最低版本
cmake_minimum_required(VERSION 3.5)
#设置项目名称
project (hello_cmake)
#添加一个可执行文件
add_executable(hello_cmake main.cpp)
二 项目分析
介绍
展示了一个非常基础的 名为hello world 的示例.
本教程中的文件如下所示:
A-hello-cmake$ 树
.
├── CMakeLists.txt
├── main.cpp
- link:CMakeLists.txt[CMakeLists.txt] - 包含要运行的CMake命令
- link:main.cpp[main.cpp] - 一个简单的"Hello World"cpp文件.
概念
CMakeLists.txt
CMakeLists.txt是存储所有CMake命令的文件。当cmake在文件夹中运行时,它将查找该文件,如果该文件不存在,cmake将以错误退出。
Minimum CMake version
使用CMake创建项目时,可以指定最低版本支持的CMake的。例如:
cmake_minimum_required(VERSION 3.5)
Projects
CMake生成可以包含一个项目名称,以确定引用使用多个项目时更容易使用变量。例如:
project (hello_cmake)
Creating an Executable
add_executable()命令指定应从指定的源文件生成可执行文件,在本例中为main.cpp。函数的第一个参数是要编译的可执行文件,第二个参数是要编译的源文件列表。
注意:有些人使用的一种简写方法是将项目名称和可执行文件名称相同。
例如:
cmake_minimum_required(VERSION 2.6)
project (hello_cmake)
add_executable(${PROJECT_NAME} main.cpp)
Binary Directory
运行cmake命令的根文件夹或顶级文件夹称为CMAKE_BINARY_DIR,是所有二进制文件的根文件夹。CMake支持就地和异地构建和生成二进制文件。
- In-Place Build(就地构建)
就地生成在与源代码相同的目录结构中生成所有临时生成文件。这意味着所有的makefile和object文件都散布在普通代码中。要创建就地生成目标,请在根目录中运行cmake命令。例如:
> (base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake> cmake -G "MinGW Makefiles" .
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/Qt/Qt5.12.2/Tools/mingw730_32/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/Qt/Qt5.12.2/Tools/mingw730_32/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Solutions/cmakeStudy/cmake-examples/01-basic/A-hello-cmake
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake> tree
卷 新加卷 的文件夹 PATH 列表
卷序列号为 F23A-9276
D:.
├─build
│ └─CMakeFiles
│ ├─3.20.0-rc4
│ │ ├─CompilerIdC
│ │ │ └─tmp
│ │ └─CompilerIdCXX
│ │ └─tmp
│ ├─CMakeTmp
│ └─hello_cmake.dir
└─CMakeFiles
├─3.22.0-rc1
│ ├─CompilerIdC
│ │ └─tmp
│ └─CompilerIdCXX
│ └─tmp
├─CMakeTmp
└─hello_cmake.dir
- Out-of-Source Build(异地构建)
源代码外生成允许您创建单个生成文件夹,该文件夹可以位于文件系统的任何位置。所有临时生成和对象文件都位于该目录中,以保持源代码树的干净。要创建源代码外构建,请在build文件夹中运行cmake命令,并将其指向根CMakeLists.txt文件所在的目录。如果要重新创建cmake环境,请使用源代码外版本从头开始,只需要删除构建目录,然后重新运行cmake。例如(加粗为命令):
> (base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake> **mkdir build**
> (base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake> **cd build**
> (base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake\build> **cmake -G "MinGW Makefiles" ..**
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/Qt/Qt5.12.2/Tools/mingw730_32/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/Qt/Qt5.12.2/Tools/mingw730_32/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Solutions/cmakeStudy/cmake-examples/01-basic/A-hello-cmake/build
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake\build>
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake\build> cd ..
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake> tree
卷 新加卷 的文件夹 PATH 列表
卷序列号为 F23A-9276
D:.
└─build
└─CMakeFiles
├─3.22.0-rc1
│ ├─CompilerIdC
│ │ └─tmp
│ └─CompilerIdCXX
│ └─tmp
├─CMakeTmp
└─hello_cmake.dir
本教程中的所有示例都将使用源代码外版本(即在源码外建立一个构建的文件夹,build).
构建案例
下面是构建此示例的完整输出过程:
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake> mkdir build
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake> cd build
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake\build> cmake -G "MinGW Makefiles" ..
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/Qt/Qt5.12.2/Tools/mingw730_32/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/Qt/Qt5.12.2/Tools/mingw730_32/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Solutions/cmakeStudy/cmake-examples/01-basic/A-hello-cmake/build
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake\build>
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake\build> mingw32-make
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.obj
[100%] Linking CXX executable hello_cmake.exe
[100%] Built target hello_cmake
(base) PS D:\Solutions\cmakeStudy\cmake-examples\01-basic\A-hello-cmake\build> ./hello_cmake
Hello CMake!