I’m having trouble with my CMake configuration for a project that uses SDL2 and SDL2_net. When I compile manually with g++, everything works fine. However, with CMake, the compiler cannot find the SDLNet_TCP_Accept method and other methods implemented in SDLnetTCP.c.
I have two source files, client.cpp and server.cpp, that need to be compiled into separate executables. Both use SDL2 and SDL2_net. Manually, I compile them with these commands, and they work without issues:
g++ client.cpp -o client -lSDL2 -lSDL2_net
g++ server.cpp -o server -lSDL2 -lSDL2_net
However, when I use CMake for my bigger project, I encounter issues with finding SDL2_net methods. Below is my CMakeLists.txt :
cmake_minimum_required(VERSION 3.27.0)
project(MCC)
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
pkg_check_modules(SDL2NET REQUIRED SDL2_net)
if(SDL2_FOUND)
message(STATUS "SDL2 FOUND")
set(SDL2_TTF "SDL2_ttf")
endif()
if(SDL2NET_FOUND)
message(STATUS "SDL2_net FOUND")
endif()
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--allow-multiple-definition -g -L../libs/Box2d/lib -L../libs/qrcodegen/lib")
set(BOX2D_LIB "box2d")
set(QRCODEGEN_LIB "qrcodegencpp")
file(GLOB SRC_FILES
${PROJECT_SOURCE_DIR}/src/**/*.cpp
)
add_executable(
${PROJECT_NAME}
${PROJECT_SOURCE_DIR}/apps/main.cpp
${SRC_FILES}
)
include_directories(
${SDL2_INCLUDE_DIRS}
${SDL2NET_INCLUDE_DIRS}
${SDL2IMAGE_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
)
target_link_libraries(
${PROJECT_NAME}
${SDL2_LIBRARIES}
${SDL2NET_LIBRARIES}
${SDL2IMAGE_LIBRARIES}
${SDL2_TTF}
${SDL2MIXER_LIBRARIES}
${BOX2D_LIB}
${QRCODEGEN_LIB}
)
The specific error I encountered when running my program:
0x00007ffff7daecd5 in SDLNet_TCP_Accept (server=0x7ffff7e28d70) at /build/libsdl2-net-o1CM8w/libsdl2-net-2.2.0+dfsg/SDLnetTCP.c:173
Downloading source file /build/libsdl2-net-o1CM8w/libsdl2-net-2.2.0+dfsg/SDLnetTCP.c
173 /build/libsdl2-net-o1CM8w/libsdl2-net-2.2.0+dfsg/SDLnetTCP.c: No such file or directory.
Aïssa Clément is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.