例子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. 想一想social VR might just work

    昨天玩了Oculus上的Casino VR(其实之前就知道这个瑞士公司,也下过standalone的PC client). 几把下来,居然觉得fun,总结起来: 1.是在一个immersive的环境中 ...

  2. 遍历 Input检测是否有重复的值

    在项目中需要遍历某个Table中的Input输入是否有重复的值,为此基于Jquery写了两种实现方式(关键在于取值方式): 方法1: function CheckGoodsNo() { var ish ...

  3. 2016年4月面试题(Unity)

    一. C#中值类型和引用类型的区别? A: 值类型的数据存储在内存的栈中:引用类型的数据存储在内存的堆中,而内存单元中只存放堆中对象的地址. 值类型存取速度快,引用类型存取速度慢 值类型表示实际数据, ...

  4. JavaScript模块化编程

    为什么模块化很重要?因为有了模块,我们就可以更方便的使用别人的代码,想要什么功能,就加载什么模块. 万事都有规矩,就是规范化! 目前,通行的Javascript模块规范有两种: CommonJS和AM ...

  5. JUnit3的作用

    简要说JUnit的4大功能 1. 管理测试用例.修改了哪些代码,这些代码的修改会对哪些部分有影响,通过JUnit将这次的修改做个完整测试.这也就JUnit中所谓的TestSuite. 2. 定义测试代 ...

  6. 获取Ip地址

    public static string GetClientIPv4Address() { string ipv4 = String.Empty; foreach (IPAddress ip in D ...

  7. JUnit报错需导入两个jar包

    <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</a ...

  8. thinkphp + 美图秀秀api 实现图片裁切上传,带数据库

    思路: 1.数据库 创建test2 创建表img,字段id,url,addtime 2.前台页: 1>我用的是bootstrap 引入必要的js,css 2>引入美图秀秀的js 3.后台: ...

  9. 如何把一个用户加入sodu组

    在一个命令前加sudo,可以使用超级用户的权限执行该命令.但并不是任何用户都可以使用sudo,只有用户属于sudo组时才能使用这个命令. 如 果希望把一个用户加入sudo组,可以用root登录系统,然 ...

  10. Apache+PHP+MySQL

    Apache:是一种web服务器(与IIS类同)PHP:全称为Hypertext Preprocessor.PHP是一种HTML 内嵌式的语言,是一种网站(网页)开发语言(与ASP.JSP..NET等 ...