CMake 入門2/5:第一個程序 helloworld

CMake 入門1/5:基於阿里雲 ECS搭建體驗環境,我們搭建了 CMake的運行環境,下面我們以 helloworld 爲例,體驗 CMake 工作的基本流程。

1 源文件說明

共包含2個文件,一個 c++文件 helloworld.cpp,另一個是CMakeLists.txt。也可到這裏下載:https://github.com/huangjunlei/How2Work/tree/master/DevOps/cmakedemo

1.1 helloworld.cpp

helloworld.cpp爲你開發的源碼文件,自由命名,自由書寫內容。

#include<iostream>

int main(int argc, char *argv[]){
   std::cout << "Hello World!" << std::endl;
   return 0;
}

1.2 CMakeLists.txt

CMakeLists.txt爲 cmake 工作的輸入文件,命名固定,格式語法固定,以下爲示例,實際開發中要根據工程結構來編寫。

cmake_minimum_required(VERSION 2.8.9)
project (hello)
add_executable(hello helloworld.cpp)
第1行聲明需要的最低 cmake版本

完整語法爲
cmake_minimum_required(VERSION major[.minor[.patch[.tweak]]] [FATAL_ERROR])

擴展閱讀:https://cmake.org/cmake/help/v2.8.12/cmake.html#command:cmake_minimum_required

第2行聲明項目名

完整語法爲: project(<projectname> [languageName1 languageName2 ... ] )

擴展閱讀:https://cmake.org/cmake/help/v2.8.12/cmake.html#command:project

第3行聲明執行體名稱及源碼列表

完整語法如下:

  add_executable(<name> [WIN32] [MACOSX_BUNDLE]
                 [EXCLUDE_FROM_ALL]
                 source1 source2 ... sourceN)

擴展閱讀:https://cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable

2 編譯過程

2.1 生成 Makefile

cmake 的命令形式爲:

  cmake [options] <path-to-source>
  cmake [options] <path-to-existing-build>

擴展閱讀:https://cmake.org/cmake/help/v2.8.12/cmake.html#section_Usage

Demo示例執行過程如下:

[root@myecs]# cmake .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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/cmakedemo/demo1
解釋

首先,CMake檢測運行環境,如沒有 c++編譯器會報附件三的錯誤;檢測通過後,CMake 會生成工程所對應的 Makefile。需要強調的是,Makefile允許查看,但不要嘗試編輯,而且下次執行時它也會被覆蓋。

2.2 查看生成結果

[root@myecs]# ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  helloworld.cpp  Makefile
[root@myecs]# ls -l Makefile 
-rw-r--r-- 1 root root 4767 1月  30 20:04 Makefile
[root@myecs]# more Makefile 
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
......
# because they might be regenerated.
cmake_check_build_system:
    $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

2.3 編譯

在 Makefile 生成後,可以使用make 進行編譯工程。

[root@myecs]# make
Scanning dependencies of target hello
[100%] Building CXX object CMakeFiles/hello.dir/helloworld.cpp.o
Linking CXX executable hello
[100%] Built target hello

2.4 查看編譯結果

[root@myecs]# ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  hello  helloworld.cpp  Makefile
[root@myecs]# ls -l hello
-rwxr-xr-x 1 root root 9176 1月  30 20:13 hello

2.5 執行著名程序 helloworld

[root@myecs]# ./hello
Hello World!

至此 cmake 使用完整流程就結束了。當然對於 helloworld 這個程序,有些殺雞用牛刀,但利於整體理解 cmake 的應用流程。

 3 相關文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章