CMake tutorial 翻译

1.最简实例

  使用cmake的最简实例是由一个源程序文件生成一个可执行文件。例如由下述C++源程序文件生成可执行文件tutorial。

  main.cpp

#include<iostream>
using namespace std;
int main(){

    cout<<"hello world"<<endl;
}

  需要编辑CMakeLists.txt文件如下:

cmake_minimum_required(VERSION 2.6)
project (tutorial)
add_executable(tutorial main.cpp)

  其中cmake_minimum_required指定了cmake最低版本限制,project指定了项目名称,add_executable指定了生成的可执行文件名称为tutorial,源程序文件为main.cpp。

  需要生成项目可以cd到此目录,然后依次执行下述命令:

cmake .
make 

  可见,cmake帮助我们快速生成了项目的makefile文件,从而可以通过make命令直接生成项目的可执行文件。此时,在该路径下就生成了可执行文件tutorial,执行该程序,有:

  使用cmake就是如此简单快捷!

2.设置版本号和配置头文件

  如果在cmake中指定版本号这样更加灵活,我们可以这么操作:通过CMakeLists.txt文件指定版本号,然后通过CMake链接到CMakeLists.txt 文件生成含有该版本号的头文件,之后就可以引用该头文件中的版本号了。

  例如:我们在CMakeLists.txt文件中指定了下述两个版本号:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_BINARY_DIR}/config.h"
  )

# add the binary tree to the search path for include files
# so that we will find config.h
include_directories("${PROJECT_BINARY_DIR}")

# add the executable
add_executable(Tutorial main.cpp)

  config.h.in 中引用了CMakeLists.txt中定义的两个版本号,如下所示:

#define Tutorial_VERSION_MAJOR @[email protected]
#define Tutorial_VERSION_MINOR @[email protected]

  main.cpp文件直接引用config.h头文件中定义的版本号,如下所示:

#include "config.h"
#include<iostream>
using namespace std;
int main(){
    cout<<"Tutorial_VERSION_MAJOR: "<<Tutorial_VERSION_MAJOR<<endl;
    cout<<"Tutorial_VERSION_MINOR: "<<Tutorial_VERSION_MINOR<<endl;
    cout<<"hello world"<<endl;
    return 0;

}

  此时,执行命令cmake不仅生成了makefile文件,还链接生成了config.h文件,如下所示:

#define Tutorial_VERSION_MAJOR 1
#define Tutorial_VERSION_MINOR 0

  执行命令make,然后顺利生成了可执行文件Tutorial,执行该文件有:

  完美!

3.在项目中添加库

时间: 2024-08-03 08:04:17

CMake tutorial 翻译的相关文章

Deep Learning Tutorial (翻译) 之 RNN-RBM

英文原文请参考http://www.deeplearning.net/tutorial/rnnrbm.html 使用RNN-RBM建模和生成复调音乐序列 本教程提供了论文(pdf)中描述的RNN-RBM的基本实现. 即用RNN-RBM来model复调音乐,训练过程中采用的是midi格式的音频文件,接着用建好的model来产生复调音乐.对音乐建模的难点在与每首乐曲中帧间是高度时间相关的(这样样本的维度会很高),用普通的网络模型是不能搞定的(普通设计网络模型没有考虑时间维度,图模型中的HMM有这方面

Deep Learning Tutorial (翻译) 之 LSTM

英文原文请参考http://www.deeplearning.net/tutorial/lstm.html LSTM 网络用于情感分析

Deep Learning Tutorial (翻译) 之 RBM

英文原文请参考http://www.deeplearning.net/tutorial/rbm.html 能量模型 Energy-based models associate a scalar energy to each configuration of the variables of interest.学习并修改相应的能量函数使得shape有较好的属性.

Deep Learning Tutorial (翻译) 之 Denoising Autoencoder

英文原文请参考http://www.deeplearning.net/tutorial/dA.html 自编码 一个自编码接受x作为输入,映射成隐藏层表示y: 其中,s是非线性函数,如sigmod.y通过一个decoder映射成与x有着相同shape的重构的z,通过相似的转换: z可以看作是x的预测,给定编码code y.可选地,W'可以是W的转置,W‘=WT,目标是优化参数W,b,b'使得平均重构误差最小. 重构误差取决于输入数据的合适的分布假设,可以使用传统的平方差,如果输入是位变量,可以使

CMake Tutorial

Django1.7官方文档中的tutorial——翻译

写下你的第一个Django应用,第一部分 让我们通过例子来学习. 通过这篇指南,我们将会带你浏览一遍一个基本投票应用的创建. 它由两部分组成: 1一个让人们查看投票和进行投票的公共站点 2一个让你添加,改变和删除投票的管理站点 我们假设你已经安装了Django.你可以检查Django是否被安装以及是哪个版本的通过运行下列命令: python -c “import django; print(django.get_version())” 如果Django被安装了,你应该会看到你安装的版本,否则,你

[cmake] Basic Tutorial

Basic Project The most basic porject is an executable built from source code file. CMakeLists.txt cmake_minimum_required (version 2.6) project (Tutorial) add_executable(Tutorial tutorial.cxx) tutorial.cxx #include <stdio.h> int main(int argc, char*

CMake使用教程

转: CMake使用教程 CMake是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目.通过编写CMakeLists.txt,可以控制生成的Makefile,从而控制编译过程.CMake自动生成的Makefile不仅可以通过make命令构建项目生成目标文件,还支持安装(make install).测试安装的程序是否能正确执行(make test,或者ctest).生成当前平台的安装包(make package).生成源码包(ma

[c++] Getting Started - CMake

CMake is an open-source cross platform build system, according to CMake's creator, Kitware. But CMake is not actually  a build system. What CMake provides is an easy way to build C/C++ projects accross platform. CMake generates a cofiguration for you