Newbyte
Newbyte

Reputation: 3282

Link SDL2_net with CMake

I'm trying to link SDL2_net (SDL_net 2.0) to my project via CMake, but after searching around I've yet to find a solution. My CMakeLists.txt currently looks like this:

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})

However, when I attempt to run CMake it gives me the following error(s):

-- 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".

I have installed the SDL2_net-devel package from my package manager (dnf on Fedora 29), and I've successfully linked SDL2 and SDL2_image previously basing it on this answer, which worked brilliantly. I also found this, but I'm not entirely sure how to use it. How should I go about this?

Upvotes: 1

Views: 2266

Answers (2)

aminosbh
aminosbh

Reputation: 193

To easily integrate the SDL2 library and other related libraries (SDL2_net, SDL2_mixer, ...), I developed modern cross-platform CMake modules that can be used as follows:

  1. Clone SDL2 CMake modules inside our project:
git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2
  1. Add the following lines in your main 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)

You can even specify a custom path to find the SDL2, SDL2_net, ... useful specially on Windows.

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

For sure, you should not forgot to install the specific packages:

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

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

Upvotes: 0

Newbyte
Newbyte

Reputation: 3282

Since the person who provided an answer only posted a comment about it, I'll put it down here myself.

The solution was seemingly very simple: I had written pkg_search_module (SDL_NET REQUIRED sdl2_net), while it was supposed to be pkg_search_module (SDL_NET REQUIRED SDL2_net).

Upvotes: 0

Related Questions