介紹
本節展示一個非常基本的hello world的例子。
本節中的文件如下:
A-hello-cmake$ tree
.
├── CMakeLists.txt
├── main.cpp
-
[CMakeLists.txt] - 包含你希望運行的 CMake 命令
# Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.5) # Set the project name project (hello_cmake) # Add an executable add_executable(hello_cmake main.cpp)
-
[main.cpp-]一個簡單的"Hello World"的C++文件。
#include <iostream> int main(int argc, char *argv[]) { std::cout << "Hello CMake!" << std::endl; return 0; }
概念
CMakeLists.txt
CMakeLists.txt是存儲所有CMake命令的文件。當cmake在文件夾中運行時,它將查找此文件,如果不存在,cmake 將因錯誤退出。
最小 CMake 版本
使用 CMake 創建項目時,你可以指定支持的最低版本的 CMake。
cmake_minimum_required(VERSION 3.5)
項目
一個CMake構建文件可以包括一個項目名稱,以便在使用多個項目時更容易引用某些變量。
project (hello_cmake)
創建可執行文件
add_executable()
命令規定,應從指定的源文件構建可執行文件,在此示例中是main.cpp。add_executable()
函數的第一個參數是要構建的可執行文件的名稱,第二個參數是要編譯的源文件列表。
add_executable(hello_cmake main.cpp)
注意 | 有些人使用的一種簡寫方式是使項目名稱和可執行文件名稱相同。這允許你按如下方式指定CMakeLists.txt。在本例中,project()函數將創建一個值為hello_cmake的變量${PROJECT_NAME}。然后可以將其傳遞給add_executable()函數以輸出‘hello_cmake’可執行文件。 |
---|
cmake_minimum_required(VERSION 2.6)
project (hello_cmake)
add_executable(${PROJECT_NAME} main.cpp)
二進制目錄
運行cmake命令的根文件夾或頂級文件夾稱為CMAKE_BINARY_DIR,是所有二進制文件的根文件夾。CMake既支持就地構建和生成二進制文件,也支持在源代碼外構建和生成二進制文件。
就地構建
就地構建將會在與源代碼相同的目錄結構中生成所有臨時文件。這意味着所有的Makefile和目標文件都散布在你的普通代碼中。要創建就地構建目標,請在根目錄中運行cmake命令。例如:
A-hello-cmake$ cmake .
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/A-hello-cmake
A-hello-cmake$ tree
.
├── CMakeCache.txt
├── CMakeFiles
│ ├── 2.8.12.2
│ │ ├── CMakeCCompiler.cmake
│ │ ├── CMakeCXXCompiler.cmake
│ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ ├── CMakeSystem.cmake
│ │ ├── CompilerIdC
│ │ │ ├── a.out
│ │ │ └── CMakeCCompilerId.c
│ │ └── CompilerIdCXX
│ │ ├── a.out
│ │ └── CMakeCXXCompilerId.cpp
│ ├── cmake.check_cache
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeOutput.log
│ ├── CMakeTmp
│ ├── hello_cmake.dir
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── DependInfo.cmake
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ └── progress.make
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── progress.marks
│ └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main.cpp
├── Makefile
源外構建
使用源外構建,你可以創建單個生成文件夾,該文件夾可以位於文件系統的任何位置。所有臨時構建的目標文件都位於此目錄中,以保持源碼目錄結構的整潔。要進行源外構建,請運行build文件夾中的cmake命令,並將其指向根CMakeLists.txt文件所在的目錄。使用源外構建時,如果你想從頭開始重新創建cmake環境,只需刪除構建目錄,然后重新運行cmake。
舉個例子:
A-hello-cmake$ mkdir build
A-hello-cmake$ cd build/
A-hello-cmake/build$ make ..
make: Nothing to be done for `..'.
matrim@freyr:~/workspace/cmake-examples/01-basic/A-hello-cmake/build$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/A-hello-cmake/build
A-hello-cmake/build$ cd ..
A-hello-cmake$ tree
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 2.8.12.2
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ └── CMakeCCompilerId.c
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ └── CMakeCXXCompilerId.cpp
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── hello_cmake.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ └── progress.make
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ └── Makefile
├── CMakeLists.txt
├── main.cpp
在本教程的所有例子中,都將使用源外構建。
構建示例
以下是構建此示例的輸出:
$ mkdir build
$ cd build
$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /workspace/cmake-examples/01-basic/hello_cmake/build
$ make
Scanning dependencies of target hello_cmake
[100%] Building CXX object CMakeFiles/hello_cmake.dir/hello_cmake.cpp.o
Linking CXX executable hello_cmake
[100%] Built target hello_cmake
$ ./hello_cmake
Hello CMake!