将SDL2_net与CMake链接

时间:2019-02-25 10:33:04

标签: c++ cmake sdl sdl-2 sdl-net

我正在尝试通过CMake将SDL2_net(SDL_net 2.0)链接到我的项目,但是经过四处搜索之后,我还没有找到解决方案。我的CMakeLists.txt当前看起来像这样:

1 cmake_minimum_required (VERSION 3.7)
2 project (SDL_net_test)
3 include (FindPkgConfig)
4 include (FindSDL_net)
5 
6 pkg_search_module (SDL2 REQUIRED sdl2)
7 pkg_search_module (SDL_NET REQUIRED sdl2_net)
8 
9 include_directories (${SDL2_INCLUDE_DIRS} ${SDL_NET_INCLUDE_DIRS})
10 
11 add_executable (SDL_net_test main.cpp)
12 target_link_libraries (SDL_net_test ${SDL2_LIBRARIES} ${SDL_NET_LIBRARIES})

但是,当我尝试运行CMake时,出现以下错误:

-- Could NOT find SDL_net (missing: SDL_NET_LIBRARIES SDL_NET_INCLUDE_DIRS) 
-- Checking for one of the modules 'sdl2_net'
CMake Error at /usr/share/cmake/Modules/FindPkgConfig.cmake:659 (message):
None of the required 'sdl2_net' found
Call Stack (most recent call first):
CMakeLists.txt:7 (pkg_search_module)


CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
SDL_NET_INCLUDE_DIR (ADVANCED)
   used as include directory in directory /home/neboula/Programming/sandbox/sdl2_net
   used as include directory in directory /home/neboula/Programming/sandbox/sdl2_net
   used as include directory in directory /home/neboula/Programming/sandbox/sdl2_net
SDL_NET_LIBRARY (ADVANCED)
    linked by target "SDL_net_test" in directory /home/neboula/Programming/sandbox/sdl2_net

-- Configuring incomplete, errors occurred!
See also "/home/neboula/Programming/sandbox/sdl2_net/build/CMakeFiles/CMakeOutput.log".

我已经从软件包管理器(Fedora 29上的dnf)安装了SDL2_net-devel软件包,并且我已经成功地将SDL2和SDL2_image链接到了先前基于this answer的站点上,该站点运行良好。我还找到了this,但是我不确定如何使用它。我该怎么办?

2 个答案:

答案 0 :(得分:0)

由于提供答案的人仅对此发表了评论,所以我自己将其放在此处。

该解决方案似乎非常简单:我写了pkg_search_module (SDL_NET REQUIRED sdl2_net),而本来应该是pkg_search_module (SDL_NET REQUIRED SDL2_net)

答案 1 :(得分:0)

为了轻松集成SDL2库和其他相关库(SDL2_net,SDL2_mixer等),我开发了modern cross-platform CMake modules,可以按如下方式使用:

  1. 在我们的项目中克隆SDL2 CMake模块:
git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2
  1. 在您的主要CMakeLists.txt中添加以下行
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)
find_package(SDL2 REQUIRED)
find_package(SDL2_net REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2:Net)

您甚至可以指定一个自定义路径来查找在Windows上特别有用的SDL2,SDL2_net等。

cmake .. -DSDL2_PATH="/path/to/sdl2" -DSDL2_NET_PATH="/path/to/sdl2-net"

当然,您不应忘记安装特定的软件包:

# Fedora/RPM
sudo yum install SDL2-devel SDL2_net-devel

# Debian/Ubuntu
sudo apt install libsdl2-dev libsdl2-net-dev