简体   繁体   中英

How to use CMake's pkg_search_module() when compiling under Windows?

I'm programming a game which uses SDL2 and CMake. In order to link and include SDL2 I use the following CMake code:

include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
target_link_libraries(MYLIB SDL2)

When compiling under Linux (Fedora) this works perfectly.

But what about Windows? There I don't have standard system locations for DLL/a files and include folders. There isn't even pkg-config.

This is the error I get:

Could NOT find PkgConfig (missing:  PKG_CONFIG_EXECUTABLE) 
checking for one of the modules 'sdl2'
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.0/Modules/FindPkgConfig.cmake:425 (message):
  None of the required 'sdl2' found
Call Stack (most recent call first):
  .../CMakeLists.txt:4 (pkg_search_module)

I used the VS 2013 generator.

As you may already know, pkg_search_module() relies on the pkg-config program in order to locate dependencies. As you point out, Windows doesn't come with pkg-config . You might be able to install it and get things working that way, but probably a better alternative for crossplatform builds is to use CMake's find_package() function instead. This way you can eliminate the dependency on having pkg-config installed on the developer's machine.

cmake_minimum_required(VERSION 3.1)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")

find_package(SDL2 REQUIRED)

add_executable(sdl2demo sdl2demo.cpp)
target_include_directories(sdl2demo SYSTEM PRIVATE "${SDL2_INCLUDE_DIR}")
target_link_libraries(sdl2demo "${SDL2_LIBRARY}")

CMake doesn't come with a module for finding SDL2 so you'll need to add that to your source directory. That's why the above sets the CMAKE_MODULE_PATH ; you add a cmake script that works cross platform for finding SDL2 to ${CMAKE_SOURCE_DIR}/cmake/Modules and then you can deliver that script with your source code.

The FindSDL2.cmake module I've used is: http://freerct.googlecode.com/svn/trunk/CMake/FindSDL2.cmake

This script should be able to locate SDL2 if it's installed in a standard location. If SDL2 isn't installed in a standard location or otherwise can't be located by this script, the developer has to configure the appropriate CMake variables to tell CMake the appropriate location, after which the configure and build will be able to procede normally.

cmake <source dir> -G "Visual Studio 12 2013 Win64" -DSDL2_INCLUDE_DIR=<sdl dir>/include -DSDL2MAIN_LIBRARY=<sdl dir>/lib/x64/SDL2main.lib -DSDL2_LIBRARY=<sdl dir>/lib/x64/SDL2.lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM