Skip to content

Latest commit

 

History

History
89 lines (63 loc) · 2.8 KB

CMake.md

File metadata and controls

89 lines (63 loc) · 2.8 KB
tags created
cmake
tool
2023-03-16T00:43:34 (UTC +08:00)

CMake

01-基础

A-hello-cmake

一个非常简单的CMake项目,只包含两个文件:

  1. CMakeLists.txt - 包含CMake命令的文件,表示你想怎么运行项目。在执行CMake命令时,CMake会首先查看这个文件,如果不存在会报错

  2. main.cpp - 简单的源代码

CMakeLists 参数说明

  1. cmake_minimum_required :项目支持的最小的CMake版本

  2. project (hello_cmake):项目名称

  3. add_executable(hello_cmake main.cpp):add_executable()命令指定应从指定的源文件构建可执行文件,在本例中为main.cpp。add_executable()函数的第一个参数是要构建的可执行文件的名称,第二个参数是要编译的源文件列表。如图,项目构建后会创建hello_cmake 可执行文件|

$ cmake ..
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - 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: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/cjj/cmake-examples/01-basic/A-hello-cmake/build
$ make
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
$ ./hello_cmake
Hello CMake!

B-hello-headers

与hello_cmake不同,这个项目新增了头文件。

$ tree
.
├── CMakeLists.txt
├── include
│   └── Hello.h
├── README.adoc
└── src
    ├── Hello.cpp
    └── main.cpp
Variable Info
CMAKE_SOURCE_DIR The root source directory
CMAKE_CURRENT_SOURCE_DIR The current source directory if using sub-projects and directories.
PROJECT_SOURCE_DIR The source directory of the current cmake project.
CMAKE_BINARY_DIR The root binary / build directory. This is the directory where you ran the cmake command.
CMAKE_CURRENT_BINARY_DIR The build directory you are currently in.
PROJECT_BINARY_DIR The build directory for the current project.