例子3: 构建动态库(.dll) 静态库(.lib)

采用和例子2一样的文件,但删除了main.cpp

E:.              
│  CMakeLists.txt
│                
├─include        
│      Date.h    
│                
└─src            
        date.cpp 

修改CMakeLists.txt

cmake_minimum_required(VERSION 3.5.2)
project(dateLib)
 
#Bring the headers, such as Date.h into the project
include_directories(include)
 
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
 
#Generate the shared library from the sources
add_library(date SHARED ${SOURCES})

 

创建build文件夹,进入,运行

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

这次我们要build成release模式,运行

msbuild /p:Configuration=Release date.vcxproj

查看Release文件夹下是否生成了date.dll文件。

 

 

ADD_LIBRARY指令的语法是:

ADD_LIBRARY(libname [SHARED|STATIC|MODULE]
[EXCLUDE_FROM_ALL] source1 source2 ... sourceN)

  • 参数1, 库名称
  • 参数2,库类型,可以为SHARED动态库,STATIC静态库,MODULE,使用dyld的系统有效,如果不支持dyld则被当作SHARED.
  • 参数3,EXCLUDE_FROM_ALL参数的意思是这个库不会被默认构建,除非有其他的组件依赖或者手工构建。
  • 参数4,源文件列表

同样的文件,如果需要生成静态库,只需要CMakeLists.txt文件最后一句改成

add_library(date STATIC ${SOURCES})

重新运行cmake,msbuild 后在Release文件下生成了

date.lib

 

如果我们想把lib文件编译好自动复制到某固定文件夹呢,我们需要添加一个ADD_CUSTOM_COMMAND

SET(COPY_TO_PATH "E:/Playground/CMakeExamples/lib")
ADD_CUSTOM_COMMAND(TARGET date POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy
                       $<TARGET_FILE:date>
                       ${COPY_TO_PATH}
                   COMMENT "Copying 'date' library to '${COPY_TO_PATH}'")

首先定义一个COPY_TO_PATH 变量,要保持所有lib的文件夹,注意需要把windows path里的 \ 替换成 /

ADD_CUSTOM_COMMAND有两个函数签名,这里我们使用它的第二个使用方法

Build Events

The second signature adds a custom command to a target such as a library or executable. This is useful for performing an operation before or after building the target. The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.

add_custom_command(TARGET target
PRE_BUILD | PRE_LINK | POST_BUILD
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[BYPRODUCTS [files...]]
[WORKING_DIRECTORY dir]
[COMMENT comment]
[VERBATIM] [USES_TERMINAL])

This defines a new command that will be associated with building the specified target. When the command will happen is determined by which of the following is specified:

PRE_BUILD
Run before any other rules are executed within the target. This is supported only on Visual Studio 7 or later. For all other generators PRE_BUILD will be treated as PRE_LINK.
PRE_LINK
Run after sources have been compiled but before linking the binary or running the librarian or archiver tool of a static library. This is not defined for targets created by the add_custom_target()command.
POST_BUILD
Run after all other rules within the target have been executed.
  • TARGET指定关联目标,这里我们指定为date,CMake会自动识别会和date.lib或者date.dll或是date.exe关联。当此关联目标变化时,触发此命令
  • POST_BUILD指定此命令在关联目标所有其他规则都执行后再执行此命令
  • COMMAND 指定你要运行的命令。这里我们使用${CMAKE_COMMAND} 指定cmake.exe命令的绝对地址,使用 cmake –E copy使用跨平台命令。
  • $<TARGET_FILE:date>指定date(.exe,.dll,.lib)的绝对地址
  • COMMENT添加注释

重新运行cmake,msbuild,看看指定文件下有没有date.lib.

 

CMAKE_COMMAND

The full path to the cmake(1) executable.

This is the full path to the CMake executable cmake(1) which is useful from custom commands that want to use the cmake -E option for portable system commands. (e.g. /usr/local/bin/cmake)

 

$<TARGET_FILE:tgt>

Full path to main file (.exe, .so.1.2, .a) where tgt is the name of a target.

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

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

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

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

    例子2:有目录的项目 我现在有个文件夹ProjectDate,有如下文件结构 E:. │  CMakeLists.txt │ ├─include │      Date.h │ └─src       ...

  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. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

  2. C++ 一些笔记

    1.指针.引用和const ; int &a = i; int *p ; int *&r=p;//r是对指针的引用 r=&i;//实质上是将p指向i *r=;//实质上是将i的 ...

  3. HomeBrew安装和卸载

    HomeBrew——OS X 不可或缺的套件管理器 1.安装,一行代码即可. /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercon ...

  4. Obiee11g变量

    原文地址:http://xee123.blog.163.com/blog/static/277119942012612104438332/ Obiee 版本 11.1.1.5 库变量 库变量在同一时刻 ...

  5. NLP概述

    1,词法分析 待续 2,文本分类 文本表示: 重点是贝叶斯模型:二项表示法和多项表示法.(向量维度为词库大小,一个是01,一个是频次).模型重点在于化后验为先验. 还有其他模型:机器学习模型,分布式模 ...

  6. HDU1532 网络流:最大流之福德福克森算法

    问题描述:约翰是个农民,每次下雨的时候他的庄家总是会被淹没,这就意味着当庄家被水淹后需要很长时间才能重新生长出来,因此,约翰已经建立了一系列排水管道为了使他的庄家尽可能被淹没的最少,也就是说管道的排水 ...

  7. hdu1087 dp

    题意:给定一串数字,要求选取一个严格递增的子序列,使序列和最大. dp[i] 表示以 i 为结尾的子序列的最大和,dp[i] = max{dp[j]+a[i]}(j 从 0 到 i-1),dp[0]是 ...

  8. CSS布局奇技淫巧:各种居中

    居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...

  9. 多系统PE win UBUNTU OSX

    WIN8 PE来自黑果圈中著名的地平线大神- 内置wim安装工具.EFI修复工具 OSX PE来自 FireWolf OS X PE V7.0 UBUNTU 直接安装盘就能当PE用 需要一个FAT32 ...

  10. Spring+SpringMVC+Mybatis大整合(SpringMVC采用REST风格、mybatis采用Mapper代理)

    整体目录结构: 其中包下全部是采用mybatis自动生成工具生成. mybatis自动生成文件 <?xml version="1.0" encoding="UTF- ...