【cmake】Ahellocmake

# $ cmake --version 设置cmake最小版本号
cmake_minimum_required(VERSION 3.5)
# 指定工程名
PROJECT (HELLO)
# 现阶段,你只需要了解 SET 指令可以用来显式的定义变量即可
# 将 hello.cpp 赋值给 SRC_LIST 变量,也可以指定多个源文件,用空格隔开
# SET(SRC_LIST hello.cpp add.cpp sub.cpp)
SET(SRC_LIST hello.cpp)
# 输出打印构建目录
MESSAGE(STATUS "This is HELLO_BINARY_DIR " ${HELLO_BINARY_DIR})
# 输出打印资源目录
MESSAGE(STATUS "This is HELLO_SOURCE_DIR " ${HELLO_SOURCE_DIR})
# 输出打印资源目录,与HELLO_SOURCE_DIR 一样
MESSAGE(STATUS "This is PROJECT_SOURCE_DIR " ${PROJECT_SOURCE_DIR})
# 输出打印 CMake 资源目录,与 PROJECT_SOURCE_DIR 一样
MESSAGE(STATUS "This is CMAKE_SOURCE_DIR " ${CMAKE_SOURCE_DIR})
# 生成可执行文件 hello ,${SRC_LIST}是引用变量,也就是源文件 hello.cpp
ADD_EXECUTABLE(hello ${SRC_LIST})

文件结构:

[root@centos8 A-hello-cmake]# tree .
.
├── CMakeLists.txt
├── main.cpp
└── README.adoc

 执行:

[root@centos8 A-hello-cmake]# mkdir build
[root@centos8 A-hello-cmake]# cd build
[root@centos8 build]# cmake ..
-- The C compiler identification is GNU 8.4.1
-- The CXX compiler identification is GNU 8.4.1
-- 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
-- This is HELLO_BINARY_DIR /sunbin/cmake-examples/01-basic/A-hello-cmake/build
-- This is HELLO_SOURCE_DIR /sunbin/cmake-examples/01-basic/A-hello-cmake
-- This is PROJECT_SOURCE_DIR /sunbin/cmake-examples/01-basic/A-hello-cmake
-- This is CMAKE_SOURCE_DIR /sunbin/cmake-examples/01-basic/A-hello-cmake
-- Configuring done
-- Generating done
-- Build files have been written to: /sunbin/cmake-examples/01-basic/A-hello-cmake/build

参考资料

1. A-hello-cmake

2. CMake 语法 - 详解 CMakeLists.txt

原文地址:https://www.cnblogs.com/sunbines/p/15422707.html