$ mkdir hello $ cd hello $ touch CMakeLists.txt main.cpp $ ll -rw-r--r-- 1 staff staff 124B 8 14 17:19 CMakeLists.txt -rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { /* code */ cout << "Hello,World!" << endl; return 0; }
# cmake 最低版本需求,不加入此行會受到警告信息 cmake_minimum_required(VERSION 3.15.0) # 項目名設置爲 hello project(hello) # 將當前目錄中的源文件名稱賦值給變量 SRC_LIST aux_source_directory(. SRC_LIST) # 指示變量 SRC_LIST 中的源文件須要編譯成一個名稱爲 hello 的可執行文件 add_executable(hello ${SRC_LIST})
# 進入 hello 目錄 $ pwd /Users/staff/Desktop/hello $ ll -rw-r--r--@ 1 staff staff 124B 8 14 17:19 CMakeLists.txt -rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp # 執行 cmake . 命令 $ cmake . -- The C compiler identification is AppleClang 10.0.1.10010046 -- The CXX compiler identification is AppleClang 10.0.1.10010046 -- 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/staff/Desktop/hello # 執行 make 命令 $ make Scanning dependencies of target hello [ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o [100%] Linking CXX executable hello [100%] Built target hello # 運行咱們的 hello 程序 $ ./hello Hello,World!
cmake 能夠直接在當前目錄進行編譯,可是,這種作法會將全部生成的中間文件和源代碼混在一塊兒,並且 cmake 生成的 makefile 沒法跟蹤全部的中間文件,即不容易將全部的中間文件刪除。ios
$ pwd /Users/staff/Desktop/hello $ ll -rw-r--r-- 1 staff staff 13K 8 14 21:50 CMakeCache.txt drwxr-xr-x 12 staff staff 384B 8 14 21:51 CMakeFiles -rw-r--r--@ 1 staff staff 124B 8 14 17:19 CMakeLists.txt -rw-r--r-- 1 staff staff 4.7K 8 14 21:50 Makefile -rw-r--r-- 1 staff staff 1.3K 8 14 21:50 cmake_install.cmake -rwxr-xr-x 1 staff staff 18K 8 14 21:51 hello -rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp $ ./hello Hello,World!
$ pwd /Users/staff/Desktop/hello $ ll -rw-r--r--@ 1 staff staff 124B 8 14 17:19 CMakeLists.txt -rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp # 在 hello 文件夾中新建 build 文件夾 $ mkdir build $ cd build $ pwd /Users/staff/Desktop/hello/build # 在 hello/build 文件夾中,執行 cmake & make 編譯 $ cmake .. -- The C compiler identification is AppleClang 10.0.1.10010046 -- The CXX compiler identification is AppleClang 10.0.1.10010046 -- 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/hubin/Desktop/hello/build $ make Scanning dependencies of target hello [ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o [100%] Linking CXX executable hello [100%] Built target hello $ ./hello Hello,World!
$ pwd /Users/staff/Desktop/hello $ tree . . ├── CMakeLists.txt ├── build │ ├── CMakeCache.txt │ ├── CMakeFiles │ │ ├── 3.15.0-rc4 │ │ │ ├── ... 省略 │ │ └── progress.marks │ ├── Makefile │ ├── cmake_install.cmake │ └── hello └── main.cpp
相關參考:
CMake 官方網站c++
聯繫做者:shell