依赖外部库2(SDL2库):Findmake写法

除了采用include_directories和link_directories指定SDL位置,

还可以使用Findmake的方式进行库依赖

Windows下编译准备

  1. 将SDL2-2.0.9目录添加到环境变量 SDL2DIR 中
  2. 将SDL2-2.0.9\lib\x64目录添加到环境变量,以便程序可以找到 SDL2.dll
  3. 然后运行 build下的编译脚本 auto_build.bat
解压后目录如下:
cmake4/
|-- build
|   |-- airplane.bmp
|   |-- auto_build.bat
|   `-- nature.bmp
|-- cmake
|   `-- modules
|       |-- FindOpenVR.cmake
|       |-- FindSDL2.cmake
|       |-- FindSDL2_image.cmake
|       |-- FindSDL2_mixer.cmake
|       `-- FindSDL2_ttf.cmake
|-- cmake4.cpp
|-- CMakeLists.txt
`-- Readme.md
备注:这里先讨论Windows下的CMake的使用方法,Linux请参考类似的做法

Findmake存放在CMakeLists.txt的同一级目录下,一般这些Findmake文件都有现成的,有的cmake下自带

  • FindSDL2.cmake
  • FindSDL2_image.cmake
  • FindSDL2_mixer.cmake
  • FindSDL2_ttf.cmake

此时的CMakeLists.txt如下:

cmake_minimum_required(VERSION 2.8)
 
PROJECT(cmake4)

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules/ ${CMAKE_MODULE_PATH})

message(${CMAKE_MODULE_PATH})
message(${CMAKE_SOURCE_DIR})
message(${PROJECT_SOURCE_DIR})

find_package (SDL2 REQUIRED)

#add all source current dir
aux_source_directory(. DIR_SRCS)

include_directories(${SDL2_INCLUDE_DIR})
  
add_executable(cmake4 MACOSX_BUNDLE ${DIR_SRCS})

target_link_libraries(cmake4 ${SDL2_LIBRARY})

其通过 CMAKE_MODULE_PATH来指定,通常情况下官方推荐使用该方法,毕竟少写很多内容

参考:https://www.ibm.com/developerworks/cn/linux/l-cn-cmake/index.html

应用依赖QT5库

请参考 https://github.com/panyingyun/cmakedemo/tree/master/cmake5

应用依赖SDL2、OpenVR库

请参考 https://github.com/panyingyun/cmakedemo/tree/master/cmake6