博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cmake
阅读量:2186 次
发布时间:2019-05-02

本文共 2266 字,大约阅读时间需要 7 分钟。

CMake 是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以产生标准的构建文件,如 Unix 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。文件 CMakeLists.txt 需要手工编写,也可以通过编写脚本进行半自动的生成。CMake 提供了比 autoconfig 更简洁的语法。在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:

  1. 编写 CmakeLists.txt
  2. 执行命令 cmake PATH 或者 ccmake PATH 生成 Makefile ( PATH CMakeLists.txt 所在的目录 )
  3. 使用 make 命令进行编译。

install CMake

我用CMake并不关注它的跨平台特性,因为我只专注于64位 Linux C++ server领域。

sudo apt-get install cmake
chenshu@chenshu-ubuntu:~$ cmake —version
cmake version 2.8.3

HelloWorld工程

mkdir -p examples/helloworld

cd examples/helloworld
创建main.cpp 文件,代码如下:
创建CMakeLists.txt文件,配置如下:
在同目录下,运行cmake .
chenshu@chenshu-ubuntu:~/Ubuntu One/c++/cmake/examples/helloworld$ cmake .
— The C compiler identification is GNU
— The CXX compiler identification is GNU
— Check for working C compiler: /usr/bin/gcc
— Check for working C compiler: /usr/bin/gcc — 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
— This is BINARY dir /home/chenshu/Ubuntu One/c++/cmake/examples/helloworld
— This is SOURCE dir /home/chenshu/Ubuntu One/c++/cmake/examples/helloworld
— Configuring done
— Generating done
— Build files have been written to: /home/chenshu/Ubuntu One/c++/cmake/examples/helloworld
Makefile以及其他一些文件被cmake生成了。执行make命令,hello二进制文件被编译出来。运行./hello,可以看到结果。
Hello World from Main!
make VERBOSE=1 可以看到详细的编译过程。
make clean 就可以清理工程

外部构建

HelloWorld采用内部构建,cmake产生的代码和自己的源代码文件在同一个目录,非常不好。因此需要采用cmake的外部构建方式。

创建helloworld2目录
这次创建一个src目录存放源代码,doc目录存放项目文档,
CMakeLists.txt需要出现在项目根目录和src目录中。
项目根目录下的内容如下:
project (HelloWorld2)
add_subdirectory(src bin)
src目录下内容如下:
add_executable(hello2 main.cpp)
创建一个build目录
cd build
cmake ..
make
build/bin下会找到hello2可执行文件。

支持gdb调试

在src/CMakeLists.txt文件中添加一行: set(CMAKE_BUILD_TYPE Debug)

http://wenku.baidu.com/view/7e5a8f145f0e7cd18425364a.html

转载地址:http://zorkb.baihongyu.com/

你可能感兴趣的文章
【LEETCODE】143- Reorder List [Python]
查看>>
【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
查看>>
【LEETCODE】86- Partition List [Python]
查看>>
【LEETCODE】147- Insertion Sort List [Python]
查看>>
【算法】- 动态规划的编织艺术
查看>>
用 TensorFlow 让你的机器人唱首原创给你听
查看>>
对比学习用 Keras 搭建 CNN RNN 等常用神经网络
查看>>
深度学习的主要应用举例
查看>>
word2vec 模型思想和代码实现
查看>>
怎样做情感分析
查看>>
用深度神经网络处理NER命名实体识别问题
查看>>
用 RNN 训练语言模型生成文本
查看>>
RNN与机器翻译
查看>>
用 Recursive Neural Networks 得到分析树
查看>>
RNN的高级应用
查看>>
TensorFlow-7-TensorBoard Embedding可视化
查看>>
轻松看懂机器学习十大常用算法
查看>>
一个框架解决几乎所有机器学习问题
查看>>
特征工程怎么做
查看>>
机器学习算法应用中常用技巧-1
查看>>