例子2:有目录的项目

我现在有个文件夹ProjectDate,有如下文件结构

E:.
│  CMakeLists.txt

├─include
│      Date.h

└─src
        date.cpp
        main.cpp

include文件夹下是头文件Date.h

#ifndef DATE_H
#define DATE_H
 
class Date
{
private:
    int m_year;
    int m_month;
    int m_day;
public:
    Date(int year, int month, int day);
    void SetDate(int year, int month, int day);
    int getYear() { return m_year; }
    int getMonth() { return m_month; }
    int getDay()  { return m_day; }
};
#endif

src文件夹下面是源文件Date.cpp和main.cpp

// Date.cpp
#include "Date.h"
 
// Date constructor
Date::Date(int year, int month, int day)
{
    SetDate(year, month, day);
}
 
// Date member function
void Date::SetDate(int year, int month, int day)
{
    m_month = month;
    m_day = day;
    m_year = year;
}

 

// Main.cpp
#include <iostream>
#include "date.h"

int main()
{
    Date date(2016,05,05);
    std::cout<<"Year:"<<date.getYear()<<" Month:"<<date.getMonth()
    <<" day:"<<date.getDay()<<std::endl;
}

 

CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.5.2)
project(directory_test)
 
#Bring the headers, such as Date.h into the project
include_directories(include)
 
#Can manually add the sources using the set command as follows:
#set(SOURCES src/mainapp.cpp src/Date.cpp)
 
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
 
add_executable(testDate ${SOURCES})

 

  • 第一行已经介绍过,设置CMake最低版本
  • 第二行project命令设置项目名称
  • include_directories() 命令加入头文件所在文件夹
  • set(SOURCES … )可以加入文件到SOURCES变量中,但是你需要一个文件一个文件的单独加入,使用范围比较小,所以被注释掉了
  • 经常使用的是file()命令来把文件加入到项目中。GLOB表示要将后面表达式"src/*.cpp"表示的所有文件加入到SOURCES变量中
  • add_executable使用SOURCES变量生成testDate可执行文件

PROJECT指令的语法是:

PROJECT(projectname [CXX] [C] [Java])
用于定义工程名称,并可指定工程支持的语言,默认表示支持所有语言。

 

这次我们使用cmake命令行,依然建立build文件夹,进入build。首先运行cmake --help看看都有哪些参量可以使用。

 

 

> cmake --help                                                          
Usage                                                                   
                                                                        
  cmake [options] <path-to-source>                                      
  cmake [options] <path-to-existing-build>                              
                                                                        
Specify a source directory to (re-)generate a build system for it in the
current working directory.  Specify an existing build directory to      
re-generate its build system.                                           
                                                                        
Options                                                                 
  -C <initial-cache>           = Pre-load a script to populate the cache.
  -D <var>[:<type>]=<value>    = Create a cmake cache entry.            
  -U <globbing_expr>           = Remove matching entries from CMake cache
  -G <generator-name>          = Specify a build system generator.      
  -T <toolset-name>            = Specify toolset name if supported by   
                                 generator.                             
  -A <platform-name>           = Specify platform name if supported by  
                                 generator.                             
  -Wdev                        = Enable developer warnings.             
  -Wno-dev                     = Suppress developer warnings.           
  -Werror=dev                  = Make developer warnings errors.        
  -Wno-error=dev               = Make developer warnings not errors.    
  -Wdeprecated                 = Enable deprecation warnings.           
  -Wno-deprecated              = Suppress deprecation warnings.         
  -Werror=deprecated           = Make deprecated macro and function warni
                                 errors.                                
  -Wno-error=deprecated        = Make deprecated macro and function warni
                                 not errors.                            
  -E                           = CMake command mode.                    
  -L[A][H]                     = List non-advanced cached variables.    
  --build <dir>                = Build a CMake-generated project binary t
  -N                           = View mode only.                        
  -P <file>                    = Process script mode.                   
  --find-package               = Run in pkg-config like mode.           
  --graphviz=[file]            = Generate graphviz of dependencies, see 
                                 CMakeGraphVizOptions.cmake for more.   
  --system-information [file]  = Dump information about this system.    
  --debug-trycompile           = Do not delete the try_compile build tree
                                 Only useful on one try_compile at a time
  --debug-output               = Put cmake in a debug mode.             
  --trace                      = Put cmake in trace mode.               
  --trace-expand               = Put cmake in trace mode with variable  
                                 expansion.                             
  --warn-uninitialized         = Warn about uninitialized values.       
  --warn-unused-vars           = Warn about unused variables.           
  --no-warn-unused-cli         = Don't warn about command line options. 
  --check-system-vars          = Find problems with variable usage in sys
                                 files.                                 
  --help,-help,-usage,-h,-H,/? = Print usage information and exit.      
  --version,-version,/V [<f>]  = Print version number and exit.         
  --help-full [<f>]            = Print all help manuals and exit.       
  --help-manual <man> [<f>]    = Print one help manual and exit.        
  --help-manual-list [<f>]     = List help manuals available and exit.  
  --help-command <cmd> [<f>]   = Print help for one command and exit.   
  --help-command-list [<f>]    = List commands with help available and ex
  --help-commands [<f>]        = Print cmake-commands manual and exit.  
  --help-module <mod> [<f>]    = Print help for one module and exit.    
  --help-module-list [<f>]     = List modules with help available and exi
  --help-modules [<f>]         = Print cmake-modules manual and exit.   
  --help-policy <cmp> [<f>]    = Print help for one policy and exit.    
  --help-policy-list [<f>]     = List policies with help available and ex
  --help-policies [<f>]        = Print cmake-policies manual and exit.  
  --help-property <prop> [<f>] = Print help for one property and exit.  
  --help-property-list [<f>]   = List properties with help available and
                                 exit.                                  
  --help-properties [<f>]      = Print cmake-properties manual and exit.
  --help-variable var [<f>]    = Print help for one variable and exit.  
  --help-variable-list [<f>]   = List variables with help available and e
  --help-variables [<f>]       = Print cmake-variables manual and exit. 
                                                                        
Generators                                                              
                                                                        
The following generators are available on this platform:                
  Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project fil
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project fil
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project fil
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project fil
                                 Optional [arch] can be "Win64" or "IA64"
  Visual Studio 9 2008 [arch]  = Generates Visual Studio 2008 project fil
                                 Optional [arch] can be "Win64" or "IA64"
  Visual Studio 8 2005 [arch]  = Generates Visual Studio 2005 project fil
                                 Optional [arch] can be "Win64".        
  Visual Studio 7 .NET 2003    = Generates Visual Studio .NET 2003 projec
                                 files.                                 
  Visual Studio 7              = Deprecated.  Generates Visual Studio .NE
                                 2002 project files.                    
  Visual Studio 6              = Deprecated.  Generates Visual Studio 6 
                                 project files.                         
  Borland Makefiles            = Generates Borland makefiles.           
  NMake Makefiles              = Generates NMake makefiles.             
  NMake Makefiles JOM          = Generates JOM makefiles.               
  Green Hills MULTI            = Generates Green Hills MULTI files      
                                 (experimental, work-in-progress).      
  MSYS Makefiles               = Generates MSYS makefiles.              
  MinGW Makefiles              = Generates a make file for use with     
                                 mingw32-make.                          
  Unix Makefiles               = Generates standard UNIX makefiles.     
  Ninja                        = Generates build.ninja files.           
  Watcom WMake                 = Generates Watcom WMake makefiles.      
  CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.    
  CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.    
  CodeBlocks - Ninja           = Generates CodeBlocks project files.    
  CodeBlocks - Unix Makefiles  = Generates CodeBlocks project files.    
  CodeLite - MinGW Makefiles   = Generates CodeLite project files.      
  CodeLite - NMake Makefiles   = Generates CodeLite project files.      
  CodeLite - Ninja             = Generates CodeLite project files.      
  CodeLite - Unix Makefiles    = Generates CodeLite project files.      
  Eclipse CDT4 - MinGW Makefiles                                        
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - NMake Makefiles                                        
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Ninja         = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
  Kate - MinGW Makefiles       = Generates Kate project files.          
  Kate - NMake Makefiles       = Generates Kate project files.          
  Kate - Ninja                 = Generates Kate project files.          
  Kate - Unix Makefiles        = Generates Kate project files.          
  Sublime Text 2 - MinGW Makefiles                                      
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - NMake Makefiles                                      
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles                                       
                               = Generates Sublime Text 2 project files. 
                                                                       

我们想生成Visual Studio 14 2015 Win64项目文件,所以运行

cmake -G "Visual Studio 14 2015 Win64" ..

> cmake -G "Visual Studio 14 2015 Win64" ..
-- The C compiler identification is MSVC 19.0.23918.0
-- The CXX compiler identification is MSVC 19.0.23918.0
-- Check for working C compiler using: Visual Studio 14 2015 Win64
-- Check for working C compiler using: Visual Studio 14 2015 Win64 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 14 2015 Win64
-- Check for working CXX compiler using: Visual Studio 14 2015 Win64 -- 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: E:/Playground/CMakeExamples/ProjectDate/build

 

msbuild testDate.vcxproj

编译通过,运行

> Debug\testDate.exe
Year:2016 Month:5 day:5

windows下CMake使用图文手册 Part 2的更多相关文章

  1. windows下CMake使用图文手册 Part 3

    例子3: 构建动态库(.dll) 静态库(.lib) 采用和例子2一样的文件,但删除了main.cpp E:.               │  CMakeLists.txt │            ...

  2. windows下CMake使用图文手册 Part 1

    维基百科介绍“CMake是个开源的跨平台自动化建构系统,它用配置文件控制建构过程(build process)的方式和Unix的Make相似,只是CMake的配置文件取名为CMakeLists.txt ...

  3. windows下CMake使用图文手册 Part 4

    例子4:链接静态库(.lib) 例子3里面我们构建了date.lib, 这个例子里我们调用这个库. 前提: date.h的头文件在 E:\Playground\CMakeExamples\DateLi ...

  4. Windows下CMake编译安装OpenCV

    Windows下CMake编译安装OpenCV 这是一个面向新手的在windows上运进opencv, helloword的教程. 在这里我们使用vs2019来编译opencv, 并运行一个hello ...

  5. 在Windows下编译ffmpeg完全手册

    本文的内容几乎全部来自于FFmpeg on Windows,但是由于国内的网络封锁,很难访问这个域名下的内容,因此我一方面按照我自己的理解和实践做了翻译,另一方面也是为了能提供一个方便的参考方法. 注 ...

  6. [转]在Windows下编译ffmpeg完全手册

    本文的内容几乎全部来自于FFmpeg on Windows,但是由于国内的网络封锁,很难访问这个域名下的内容,因此我一方面按照我自己的理解和实践做了翻译,另一方面也是为了能提供一个方便的参考方法. 注 ...

  7. windows下安装Mysql—图文详解

    mysql安装过程及注意事项: 1.1. 下载: 我下载的是64位系统的zip包: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压 ...

  8. windows下安装Mysql(图文详解)

      博客园 | 首页 | 新随笔 | 联系 | 订阅 | 管理 mysql安装过程及注意事项: 1.1. 下载: 我下载的是64位系统的zip包: 下载地址:https://dev.mysql.com ...

  9. Windows下C++/Fortran调用.exe可执行文件

    目录 软件环境 Windows下CMake编译配置 设置项目的generator Command Line CMake GUI PreLoad.cmake 设置make 示例程序 CMake 设置Fo ...

随机推荐

  1. C语言基础回顾

    第一章 C语言基础 1.  C语言编译过程 预处理:宏替换.条件编译.头文件包含.特殊符号 编译.优化:翻译并优化成等价的中间代码表示或汇编代码 汇编:生成目标文件,及与源程序等效的目标的机器语言代码 ...

  2. iOS 崩溃日志 Backtrace的符号化

    iOS的崩溃日志配合dsym文件可以找到崩溃时的backtrace,这是解决崩溃的最重要的信息. 如果是在同一台mac上打包, 导入crash log时候会自动将backtrace符号化,可以看到方法 ...

  3. sublime text 2 ubuntu安装及插件管理

    参考 dudumao 1.下载Sublime Text2官网下载地址:http://www.sublimetext.com 2.安装Sublime Text2解压即可使用 $ sudo tar -jx ...

  4. 网页闯关游戏(riddle webgame)--H5刮刮卡的原理和实践

    前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. 对于刮刮卡, 想必大家都很熟悉, 也很喜 ...

  5. 【转】构建C1000K的服务器(1) – 基础

    原文来自 ideawu 构建C1000K的服务器(1) – 基础 著名的 C10K 问题提出的时候, 正是 2001 年, 到如今 12 年后的 2013 年, C10K 已经不是问题了, 任何一个普 ...

  6. 深入理解kmp中的next数组

    next数组 1. 如果对于值k,已有p0 p1, ..., pk-1 = pj-k pj-k+1, ..., pj-1,相当于next[j] = k. 此意味着什么呢?究其本质,next[j] = ...

  7. android 底层入门开发(二)

    LED将为我闪烁:控制发光二极管 对于大多数Linux驱动来说,需要直接与硬件交互,本章主要介绍用Linux驱动来控制二极管的明暗,即通过Linux驱动发送数据控制开发板上LED灯的开关. 第一节介绍 ...

  8. 在AndroidStudio不能找到so文件问题:couldn't find libweibosdkcore.so

    解决步骤已经写到我的公众号,二维码在下面. 欢迎观看我的CSDN学院课程,地址:http://edu.csdn.net/course/detail/2877 本人联系方式: 更多精彩分享,可关注我的微 ...

  9. CE 进程间通信

    WINCE下进程间通信常用的方式有:剪贴板(Clipboard),网络套接字(Socket),WM_COPYDATA消息,共享内存,管道(消息队列),注册表等 剪贴板 //////////////// ...

  10. jQuery中的map()方法

    jQuery中map()方法的使用格式为:$(selector).map(callback(index,domElement)). 将在每一个被选元素上执行map()方法中设置的回调函数,在回调函数中 ...