我正在从这里编写一个简单的教程:https://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php
我的初始程序未加载任何图像。它只是显示一个屏幕而消失了。一切正常。这是我的初始CMakeLists.txt
文件:
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})
一切工作正常,编译良好。但是,然后我想加载一个PNG图像,我认为这很容易。谷歌搜索使我进入了SDL2图像库和IMG_Load
方法。因此,我继续安装libsdl2-image-dev
,然后我的CMakeLists.txt
文件又增加了两行::
set(SDL2IMAGE_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
但是,仅通过使用IMG_Load
方法,make命令就抛出了很多库要求:libtiff-dev
,libpng-dev
,libjpeg-dev
,libwebp-dev
。所有这些只是为了加载png文件!因此,我继续安装所有这些文件,现在我的CMakeLists.txt
文件看起来像是可憎的(我在可以的地方使用了find_package
,并在不能的地方手动设置了变量):
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)
至此,我制作时出现此错误:
[ 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
我已经搜寻了几个小时。有人可以在黑暗中提供光明吗?
这也是使用sdl + cmake的正确方法吗?
set(SDL2IMAGE_LIBRARIES "/usr/lib/x86_64-linux-gnu/libSDL2_image.a")
似乎您没有将SDL2_image动态链接到您的程序。我不知道如何在CMake中进行更改,但是您可以使用此模式来编译程序:
g++ your_program.cpp -o your_program -lSDL2 -lSDL2_image
您也可以使用这样的Makefile:
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