简体   繁体   中英

Loading a PNG using SDL2 + SDL2Image using C++ and cmake on Ubuntu

I'm working on a simple tutorial from here: https://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php

My initial program did not do any image loading. It just showed a screen and went away. Everything worked fine. Here is my initial CMakeLists.txt file:

cmake_minimum_required(VERSION 3.7)
project(01_hello_world)


set(CXX_STANDARD 17)

find_package(SDL2 REQUIRED)

add_executable(hello 01_hello_SDL.cpp)
target_include_directories(hello PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(hello ${SDL2_LIBRARIES})

Everything worked and compiled just fine. However, I then wanted to load a PNG image, which I thought would be a very easy. Googling led me to the SDL2 Image library, and the IMG_Load method. So I went ahead and installed libsdl2-image-dev , and my CMakeLists.txt file grew by two more lines::

set(SDL2IMAGE_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")

However, just by using the IMG_Load method, the make command threw a whole bunch of library requirements: libtiff-dev , libpng-dev , libjpeg-dev , libwebp-dev . All this just to load a png file, So I went ahead and installed all those, and now my CMakeLists.txt file looks like this abomination (I used find_package where I could, and manually set variables where I couldn't):

cmake_minimum_required(VERSION 3.7)
project(01_hello_world)

set(CXX_STANDARD 17)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(SDL2 REQUIRED)
find_package(PNG REQUIRED)
find_package(JPEG REQUIRED)
find_package(TIFF REQUIRED)

set(SDL2IMAGE_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
set(WEBP_LIBRARIES "/usr/lib/x86_64-linux-gnu/libwebp.a")

add_executable(hello 01_hello_SDL.cpp)
target_include_directories(hello PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS})
target_link_libraries(hello ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES} ${PNG_LIBRARY} ${JPEG_LIBRARY} ${WEBP_LIBRARIES} ${TIFF_LIBRARIES} Threads::Threads)

At this point, when I make, I get this error:

[ 50%] Linking CXX executable hello
/usr/bin/ld: cannot find -l{TIFF_LIBRARIES}
collect2: error: ld returned 1 exit status
CMakeFiles/hello.dir/build.make:98: recipe for target 'hello' failed
make[2]: *** [hello] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/hello.dir/all' failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

I've been googling for hours. Can someone provide a light in this darkness?

Also is this the correct approach to working with sdl + cmake?

set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")

It seems like you aren't dynamically linking SDL2_image to your program. I don't know how to change it in CMake,but you could compile your program utilizing this pattern:

g++ your_program.cpp -o your_program -lSDL2 -lSDL2_image

You may also utilize a Makefile like this one:

OBJS = your_program.cpp

OBJ_NAME = your_program_output

all:
    g++ $(OBJS) -o $(OBJ_NAME) -lSDL2 -lSDL2_image # don't forget about replacing the four spaces by a tab character

One last thing, lazy foo has a tutorial teaching how to install and set up the SDL2_image library on linux utilizing an IDE (Code::Blocks) or not (command lines and Makefiles). http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/linux/index.php

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