1. 程式人生 > >cmake工具使用簡明教程(基於命令列和gui,編譯到windows和linux雙平臺)

cmake工具使用簡明教程(基於命令列和gui,編譯到windows和linux雙平臺)

cmake可以用來構建跨平臺的專案,本文簡要講解針對多目錄原始碼專案使用cmake構建和編譯的方法。

專案結構

這裡寫圖片描述

整個工程多目錄多檔案組織而成,其中build目錄用於生成各平臺解決方案檔案的,程式碼如下
bird.h

class bird
{
public:
    void fly();
};

bird.cpp

#include <iostream>
#include "bird.h"

void bird::fly()
{
    std::cout << "the bird is flying" << std::endl;
}

main.cpp

#include <iostream>
#include "include/bird.h"
using namespace std;

int main()
{
    bird b;
    b.fly();
    return 0;
}

在根目錄放入cmake的配置檔案
CMakeLists.txt

# set minimum cmake version
cmake_minimum_required(VERSION 3.0)

# set project name
project(cpptest)

# bring the include dir to project
include_directories(include) # bring the src dir to project # set(SOURCES src/bird.cpp src/main.cpp) # method1: add cxx file one by one file(GLOB SOURCES "src/*.cpp" "./*.cpp") # method2: add cxx file once for all # set the executable binary target add_executable(cpptest ${SOURCES})

其實,cmake配置檔案寫法千千萬,具體可以檢視官網文件。

windows下使用cmake

在windows下使用cmake的gui tool配置並聲稱visual studio的sln解決方案
(其實windows下也可以用命令列來配置生成)

step1:gui tool生成sln

開啟cmake gui工具,配置CMakeLists.txt和build目錄,點選configure
這裡寫圖片描述

其中,

  • source code路徑填CMakeLists.txt所在的根目錄
  • binaries填寫一個自定義的路徑,存放生成的各平臺編譯檔案

選擇vs2015編譯,點選finish
這裡寫圖片描述

這裡寫圖片描述

然後點選generate,生成visual studio解決方案結束
這裡寫圖片描述

step2:編譯執行sln

在build目錄可以看到生成的解決方案
這裡寫圖片描述

用vs2015開啟,有三個project,執行cpptest專案即可
這裡寫圖片描述

linux下使用cmake

使用純命令列組織專案:
在linux用cmake的命令列工具生成makefile

step1:cmake命令列生成makefile

配置好cmake的環境變數,在工程根目錄下,輸入命令:

$ cmake -G "Unix Makefiles" .
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Check for working C compiler: C:/cygwin/bin/cc.exe
-- Check for working C compiler: C:/cygwin/bin/cc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working CXX compiler: C:/cygwin/bin/c++.exe
-- Check for working CXX compiler: C:/cygwin/bin/c++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Configuring done
-- Generating done
-- Build files have been written to: D:/codetest/cpptest

step2:編譯makefile檔案

生成的makefile檔案
這裡寫圖片描述
根據make的規則編譯執行即可

$ make
Scanning dependencies of target cpptest
[ 33%] Building CXX object CMakeFiles/cpptest.dir/src/bird.cpp.obj
D:/codetest/cpptest/src/bird.cpp:7:2: warning: no newline at end of file
[ 66%] Building CXX object CMakeFiles/cpptest.dir/main.cpp.obj
[100%] Linking CXX executable cpptest.exe
[100%] Built target cpptest
$ ./cpptest.exe
the bird is flying

也可以用IDE來組織專案,以Qt Creator為例:

step1

用qt creator開啟專案根目錄的cmake配置檔案
這裡寫圖片描述

step2

配置cmake編譯生成目錄
這裡寫圖片描述

用cmake嚮導編譯專案

此處可以配置編譯引數,比如: -G "Unix Makefiles"

點選Run CMake –> 編譯完成後點選Finish

(本步驟在高版本qt creator中可能沒有,改成了在程式run或者debug時候編譯)

step3

開啟程式碼目錄,用debug或者run執行程式即可
這裡寫圖片描述
生成目錄
這裡寫圖片描述

注意:

  • 為了保證cmake能正常編譯和generate,確保專案所在目錄到許可權為777
  • 如果需要在qt creator中debug,需要cmake配置檔案最後加上:set( CMAKE_BUILD_TYPE Debug )

tips

  • windows下也可以用命令生成sln或者nmake檔案
  • linux的makefile也可以用gui工具生成,但是需要配置cygwin或者mingw
  • mac os下用法類似,生成的是xcode解決方案
  • cmake適用於管理跨多個平臺的大型專案,小專案其實有點繁瑣