cmake工具使用简明教程(基于命令行和gui,编译到windows和linux双平台)

引用
https://cmake.org/runningcmake/
https://cmake.org/cmake-tutorial/
http://www.hahack.com/codes/cmake/
http://www.cnblogs.com/cuiocean/p/5460419.html

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适用于管理跨多个平台的大型项目,小项目其实有点繁琐
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值