CMake是一个跨平台的Makefile生成工具,可以根据特定的规则生成相应的Makefile文件,并对C/C++源代码进行编译和管理。

有一篇博客介绍CMake的使用,比较通俗易懂,链接地址是:Cmake 详解

CMake的官方下载地址为:https://cmake.org/download/

官方文档地址为:CMake 3.16 Documentation

官方的CMake指南地址为:CMake Tutorial

一、CMake中添加对C++11的支持

1、在对应的CMakeLists.txt文件中加入以下语句:

add_definitions(-std=c++11)

或者

 if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11 -g ${CMAKE_CXX_FLAGS}")
endif(CMAKE_COMPILER_IS_GNUCXX)

2、延伸 如何写cmake使其包含c++11特性 (-std=c++11如何写进cmakeList.txt)

使用的g++版本和cmake版本分别是g++ 4.8.2和cmake 2.8

之前写cmkae编译带有c++11特性的代码有这么一句:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

但是总会出现cc1plus: error: unrecognized command line option “-std=c++11” 报错。

所以set(QMAKE_CXXFLAGS “-std=c++11”) 类似的写法肯定不行。

后来发现是std=c++11 这种写法老版本不支持。

ok

直接测试新写法 CMakeLists.txt文件如下所示:

#CMakeLists.txt
project(test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST}${PROJECT_NAME}.cpp) include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

测试c++11代码如下:

//test.cc
#include <iostream>
#include<vector>
using namespace std;
int main()
{
const std::vector<int>v(1);
auto a = v[0];//a为int类型
cout <<"a : "<< a <<endl;
decltype(v[0]) b = 0;//b为const int&类型,即std::vector<int>::operator[](size_type)const的返回类型
auto c = 0;//c为int类型
auto d = c;//d为int类型
decltype(c) e;//e为int类型,c实体的类型
decltype((c)) f = e;//f为int&类型,因为(c)是左值
decltype(0) g;//g为int类型,因为0是右值 return 0;
}

examples_CMake项目

github上面有一个韩国人jacking75写的简单的cmake使用示例,

examples_CMake项目地址是:https://github.com/jacking75/examples_CMake

CMake例子

范例介绍

示例代码在CMake_example目录中。

01 helloworld 一个简单文件中的-C ++代码
  • main.cpp
#include <iostream>

int main()
{
auto name = "jacking";
std::cout << "hello world: " << name << std::endl;
return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_definitions(-std=c++11)
add_executable(Main main.cpp)
02 helloworld-设置编译器选项。 -Wall,C ++ 14
  • main.cpp
#include <iostream>

int main()
{
auto name = "jacking";
std::cout << "hello world: " << name << std::endl;
return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_definitions("-Wall -std=c++14")
add_executable(Main main.cpp)
03 helloworld-如果您有除主代码文件以外的其他代码文件
  • main.cpp
#include "test.h"

int main()
{
TEST test;
test.Print();
return 0;
}
  • test.h
class TEST
{
public:
void Print();
};
  • test.cpp
#include "test.h"

#include <iostream>

void TEST::Print()
{
std::cout << "Test::Print" << std::endl;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_executable(Main
main.cpp
test.cpp
)
04 helloworld-如果mai.cpp以外的文件位于其他目录中

源代码04_helloworld目录结构如下:

[root@ltcos01 04_helloworld]$ tree -L 2
.
├── CMakeLists.txt
├── main.cpp
├── test01
│ ├── test01.cpp
│ └── test01.h
└── test02
├── test02.cpp
└── test02.h 2 directories, 6 files
  • main.cpp
#include "test01/test01.h"
#include "test02/test02.h" int main()
{
TEST01 test01;
test01.Print(); TEST02 test02;
test02.Print();
return 0;
}

test01目录下 有test01.h和test01.cpp这两个文件

  • test01/test01.h
class TEST01
{
public:
void Print();
};
  • test01/test01.cpp
#include "test01.h"
#include <iostream> void TEST01::Print()
{
std::cout << "Test01::Print" << std::endl;
}

test02目录下有test02.h和test02.cpp这两个文件

  • test02/test02.h
class TEST02
{
public:
void Print();
};
  • test02/test02.cpp
#include "test02.h"
#include <iostream> void TEST02::Print()
{
std::cout << "Test02::Print" << std::endl;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_executable(Main
main.cpp
test01/test01.cpp
test02/test02.cpp
)
05 helloworld-reference 创建静态文件后

05_helloworld源代码目录树结构如下所示:

[root@ltcos01 05_helloworld]$ tree -L 2
.
├── CMakeLists.txt
├── main.cpp
├── test01
│ ├── CMakeLists.txt
│ ├── test01.cpp
│ └── test01.h
└── test02
├── CMakeLists.txt
├── test02.cpp
└── test02.h 2 directories, 8 files
  • main.cpp
#include "test01/test01.h"
#include "test02/test02.h" int main()
{
TEST01 test01;
test01.Print(); TEST02 test02;
test02.Print();
return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_subdirectory(test01)
add_subdirectory(test02)
add_executable(Main main.cpp)
target_link_libraries(Main Test01 Test02)

test01目录下有test01.h和test01.cpp以及相应的CMakeLists.txt文件

  • test01/test01.h
class TEST01
{
public:
void Print();
};
  • test01/test01.cpp
#include "test01.h"

#include <iostream>

void TEST01::Print()
{
std::cout << "Test01::Print" << std::endl;
}
  • test01/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_library(Test01 STATIC
test01.cpp
)

上面的test01目录下的CMakeLists.txt的add_library(Test01 STATIC test01.cpp)指令会生成相应的静态库文件libTest01.a

test02目录下和test01目录结构一样,也有test02.h和test02.cpp以及相应的CMakeLists.txt文件

  • test01/test02.h
class TEST02
{
public:
void Print();
};
  • test02/test02.cpp
#include "test02.h"

#include <iostream>

void TEST02::Print()
{
std::cout << "Test02::Print" << std::endl;
}
  • test02/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_library(Test02 STATIC
test02.cpp
)

同样的,在上面的test02目录下执行cmake命令会生成相应的静态库文件libTest02.a。具体操作过程如下:新建一个build目录,然后进入到build目录下执行cmake …运行上一级目录即test02下的CMakeLists.txt文件,操作如下:

[root@ltcos01 test02]$ ls
build CMakeLists.txt test02.cpp test02.h
[root@ltcos01 test02]$ cd build/
[root@ltcos01 build]$ ls
[root@ltcos01 build]$ 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /data/public/home/cchufeng/GithubProjects/examples_CMake/CMake_example/05_helloworld/test02/build
[root@ltcos01 build]$ make
Scanning dependencies of target Test02
[ 50%] Building CXX object CMakeFiles/Test02.dir/test02.cpp.o
[100%] Linking CXX static library libTest02.a
[100%] Built target Test02
[root@ltcos01 build]$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake libTest02.a Makefile
[root@ltcos01 build]$
06 helloworld-指定编译器
  • main.cpp
#include <iostream>

int main()
{
auto name = "jacking";
std::cout << "hello world: " << name << std::endl;
return 0;
}
  • CMakeLists.txt
PROJECT(hello)

set(CMAKE_CXX_COMPILER g++)
add_definitions("-Wall -std=c++14") ADD_EXECUTABLE(main main.cpp)
07 helloworld-使用外部库(此处为Boost库)
  • main.cpp
#include <boost/thread.hpp>
#include <iostream> int main()
{
std::cout << "Boost.Thread !!!" << std::endl;
boost::thread Thread1( [] ()
{
for( int i = 0; i < 5; ++i )
{
std::cout << "Thread Num : " << i << std::endl;
}
} ); Thread1.join();
return 0;
}
  • CMakeLists.txt
PROJECT(hello)

set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_FLAGS "-m64")
add_definitions("-Wall -std=c++14") INCLUDE_DIRECTORIES(/$ENV{HOME}/Dev/C++/ThirdParty/boost_1_60_0)
LINK_DIRECTORIES(/$ENV{HOME}/Dev/C++/ThirdParty/boost_1_60_0/stage/gcc/lib) ADD_EXECUTABLE(hello-boost hello-boost.cpp) TARGET_LINK_LIBRARIES(hello-boost pthread boost_thread boost_system boost_chrono)

C++中使用CMake编译管理项目的更多相关文章

  1. AndroidNDK开发中使用CMake编译JNI

    虽然一直在做NDK的开发工作,但是由于项目比较久远,都是使用Makefile进行底层编译,对于目前AndroidStudio官方提供的CMake编译方式并不是很了解,现在学习下这种已经不算新潮的新方式 ...

  2. 在linux中使用cmake编译运行cocos2d-x 3.4 projects

    原因: 由于不想在真机环境和 ide中调试环境, 只想在linux端进行 调试和运行, 需要使用cmake对现有的游戏进行编译(cocos2dx-lua 3.4) 修改步骤: 1.修改framewor ...

  3. eclipse中设置在编译运行项目之前自动保存修改的文件

    Window -> Preferences -> General -> Workspace -> “Save automatically before build” Windo ...

  4. 用CMAKE编译配置的项目进行调试的方法

    在Linux 下用CMAKE编译的项目进行Debug 需进行设置: 1.在未设置之前 进行调试可能会出现错误报告:No source available for ...等一系列错误,这些错误可能就是你 ...

  5. 管理项目中的贴图-Texture overview插件

    Texture overview插件管理项目中的贴图 1. Assetstore地址 2. 总览项目中所有贴图 3. 针对不同平台的贴图压缩设置 在插件的右上角 4. 支持多选批量修改 5. 点击表头 ...

  6. 在Eclipse中编译maven项目出的问题

    在Eclipse中编译Maven项目,运行 jetty:run 指令的时候会出错,在 JRE选项卡中加入: -Dorg.mortbay.util.URI.charset=GBK-Xms512m -Xm ...

  7. 如何使用cygwin去编译cocos2dx项目中的C++文件

    将生成的cocos2dx的Android项目导入到eclipse 可以先测试一下如何编译C++项目: 1.打开cygwin,进入到Android项目对应的目录下面去: 2.编译脚本 在编译脚本之间,如 ...

  8. itest 开源测试管理项目中封装的下拉列表小组件:实现下拉列表使用者前后端0行代码

    导读: 主要从4个方面来阐述,1:背景:2:思路:3:代码实现:4:使用 一:封装背景       像easy ui 之类的纯前端组件,也有下拉列表组件,但是使用的时候,每个下拉列表,要配一个URL ...

  9. VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战(原创)

    VS2013中Nuget程序包管理器控制台使用入门(三)-项目实战 1.给指定项目安装Newtonsoft.Json ,Version 4.5.11 PM> Install-Package Ne ...

随机推荐

  1. spark浅谈(3):

    一.shuffle操作 1.spark中特定的操作会触发我们都知道的shuffle事件,shuffle是spark进行数据重新分布的机制,这通常涉及跨执行程序和机器来赋值数据,使得混洗称为复杂而且昂贵 ...

  2. k3 cloud列表中出现很多空白

    解决办法:找到单据体:过滤面板默认隐藏打勾

  3. Java Split()方法按点(.)切分注意细节

    按点(.)切分,必须要注意转义!如:split("\\."). 例子: public class Test { public static void main(String[] a ...

  4. ES常见错误

    1. Request cannot be executed; I/O reactor status: STOPPED RestClient被关闭了 2. SpringBoot启动后 Stopping ...

  5. JS基础入门篇(七)—运算符

    1.算术运算符 1.算术运算符 算术运算符:+ ,- ,* ,/ ,%(取余) ,++ ,-- . 重点:++和--前置和后置的区别. 1.1 前置 ++ 和 后置 ++ 前置++:先自增值,再使用值 ...

  6. 线程中sleep和wait方法的区别

    sleep() 方法: 线程主动放弃CPU,使得线程在指定的时间内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态.典型地,sleep()被用在等待某个资源就绪的情形:测试发 ...

  7. OC中SEL,类别,继承,协议的使用

    1.SEL SEL是selector的缩写,selector在OC中作用是定义一个方法变量,通过该方法变量来调用方法.我们在后面的UI中会经常用selector来调用事件方法.下面我将举两个例子来说明 ...

  8. 本页面用来演示如何通过JS SDK,创建完整的QQ登录流程,并调用openapi接口

    QQ登录将用户信息存储在cookie中,命名为__qc__k ,请不要占用 __qc__k : 1) :: 在页面顶部引入JS SDK库: 将“js?”后面的appid参数(示例代码中的:100229 ...

  9. Python_009(函数,命名空间)

    一.函数 1.函数格式: #定义函数: def function(): 函数体 return 返回值 #调用函数: function() ret = function() 这是得到返回值. 这里是用关 ...

  10. c#image与byte数组的转换

    // image to byte[] Image photo = new Bitmap(selectPictureFile); System.IO.MemoryStream ms = new Syst ...