导航

windows下CMake使用图文手册 Part 3

Posted on 2016-05-06 16:11  cuiocean  阅读(1527)  评论(0编辑  收藏  举报

例子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.