例子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. C#中as用法

    在程序中,进行类型转换时常见的事,C#支持基本的强制类型转换方法,例如 Object obj1 = new NewType();NewType newValue = (NewType)obj1;这样强 ...

  2. serialVersionUID的作用

    Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的.在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的seri ...

  3. angular2 笔记

    动态添加一个component: import { ViewContarinerRef, Component, ComponentFactoryResolver, ViewChild } from ' ...

  4. log4g

    org.apache.log4j.Logger 详解 1. 概述 1.1. 背景 在应用程序中添加日志记录总的来说基于三个目的 :监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工 ...

  5. input 边框颜色

    border 的属性 有三个 border:5px solid red; 如果上述值缺少一个没有关系,例如border:#FF0000;是允许的. 分别对应:border-width, border- ...

  6. C语言标准库函数(网络上copy的)

    C语言标准库函数 标准io函数Standard C I/Oclearerr() clears errorsfclose() close a filefeof() true if at the end- ...

  7. java中的反射,知道类名创建类,还可以设置私有属性的值

    刚刚学到了反射,感觉反射的功能好强大,所以想写篇博客记录下自己的学习成果. 利用反射来创建对象. Class c1=Class.forName("test.Person");//通 ...

  8. halcon运行版设置

  9. LeetCode----Array

    Remove Duplicates from Sorted Array 思路:两个指针,头指针在0,尾指针从1开始寻找,找到第一个不等于头指针值的数,覆盖掉头指针后面那个数,然后尾指针往后移. pub ...

  10. putty+xming远程登录Ubuntu16.04图形界面

    前面我写过一篇<Ubuntu16.04 安装ftp服务器传输文件>的文章.文章当中已经可以远程访问linux服务器并且传输文件,然而要在putty中使用开启图形界面的命令到目前为止还是不够 ...