例子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. dede channel 增加limit(属性)功能

    dede channel 标签添加 limit(属性)功能 在用dede建站的时候,往往会需要用到一些需要自定义的普通需求,今天在这里介绍channel的limit功能,有时候我们需要调用中间部分数据 ...

  2. PP 创建BOM

    转自 http://blog.csdn.net/u012369651/article/details/19190939 一.最终结果预览. 二.创建过程. 使用到的事务码 CS01 创建BOM CS0 ...

  3. Date Range Picker时间插件非常不错,主要体现在选择一个时间区间

    地址:http://www.daterangepicker.com/ demo地址:http://tamble.github.io/jquery-ui-daterangepicker/#event a ...

  4. groupspecWidhoutAuthorizations与groupspecWidthAuthorizations的区别

    GroupSpecifier是一个用来定义group所有参数的类.首先,将它命名为“myGroup/g1”.然后设置 serverChannel与Stratus进行沟通.最后发布.这样,我们就完成了P ...

  5. 我关注的一些关于前端的文章(copy)

    本文的核心是侧重于HTML/CSS的框架,JS框架或以JS为核心的框架不讨论(比如YUI):多屏已是既定事实,虽然不是所有开发都要考虑自适应,但有自适应功能至少说明了这框架短期内不会被淘汰,所以不带自 ...

  6. C语言中内存的申请函数

    C语言跟内存申请相关的函数主要有 alloca,calloc,malloc,free,realloc,sbrk等. alloca是向栈申请内存,因此无需释放. malloc分配的内存是位于堆中的,并且 ...

  7. CE 操作串口

    WinCE里面都是通过标准的系统API对串口进行操作的,但是串口不同于其它文件,它是是独占式地操作的. 下面是一个操作的串口类: #pragma once typedef void (*LPDataA ...

  8. 3数字cn域名延续数字域名火爆行情! 珍品域名 593.cn 出售

      近日, 域名投资者小维放出珍藏多年的珍品域名593.cn, 据悉该域名将参加易名中国举办的数字域名专场拍卖活动,将以1元标价起拍.   域名593.cn,数字“593”谐音“我就上.吾就上.我就商 ...

  9. 00 alv抬头等

    *&---------------------------------------------------------------------* *& Report ZHJ_TEST0 ...

  10. logrotate机制与原理[转载]

    http://blog.lightxue.com/how-logrotate-works/ 日志实在是太有用了,它记录了程序运行时各种信息.通过日志可以分析用户行为,记录运行轨迹,查找程序问题.可惜磁 ...