一 内容
-
CMake最简单的例子只需要一个3行的CMakeLists.txt和一个源文件例如main.cc
-
CMakeLists.txt
# 设置CMake最低版本 cmake_minimum_required(VERSION 3.16) # 设置项目名称及版本 project(Tutorial VERSION 1.0) # 可仅设置项目名称 # project(Tutorial) # 生成可执行文件 add_executable(Tutorial main.cc)
-
main.cc
#include <iostream> int main() { std::cout << "first case." << std::endl; return 0; }
二 构建
-
创建并进入构建目录。因为构建会生成其他文件,单独的构建目录可以避免源文件目录被污染。
lee@leedeMacBook-Pro step1 % ls -l total 16 -rw-r--r-- 1 lee staff 228 5 27 17:03 CMakeLists.txt -rw-r--r-- 1 lee staff 90 5 27 15:44 main.cc lee@leedeMacBook-Pro step1 % mkdir build lee@leedeMacBook-Pro step1 % ls -l total 16 -rw-r--r-- 1 lee staff 228 5 27 17:03 CMakeLists.txt drwxr-xr-x 2 lee staff 64 5 27 19:32 build -rw-r--r-- 1 lee staff 90 5 27 15:44 main.cc lee@leedeMacBook-Pro step1 % cd build lee@leedeMacBook-Pro build %
-
build&run
lee@leedeMacBook-Pro build % cmake .. -- The C compiler identification is AppleClang 12.0.0.12000032 -- The CXX compiler identification is AppleClang 12.0.0.12000032 -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/lee/research/cmake_learn/cmake_tutorial/step1/build lee@leedeMacBook-Pro build % ls CMakeCache.txt Makefile CMakeFiles cmake_install.cmake lee@leedeMacBook-Pro build % cmake --build . Scanning dependencies of target Tutorial [ 50%] Building CXX object CMakeFiles/Tutorial.dir/main.cc.o [100%] Linking CXX executable Tutorial [100%] Built target Tutorial lee@leedeMacBook-Pro build % ls CMakeCache.txt Makefile cmake_install.cmake CMakeFiles Tutorial lee@leedeMacBook-Pro build % ./Tutorial first case. lee@leedeMacBook-Pro build %
三 Github
- 代码已上传Github