问题描述
我正在寻找最简单的方法来使用 SDL2 和 SDL_image 与cmake编译c ++程序。
这是我最好的尝试,经过几个小时的搜索:
CMakeLists.txt
project(shooter-cmake2)
cmake_minimum_required(VERSION 2.8)
设定(SOURCES
shooter.cpp
classes.cpp
utils.cpp
)
set(CMAKE_CXX_FLAGS$ {CMAKE_CXX_FLAGS} -std = c ++ 0x)
add_executable($ {PROJECT_NAME} $ {SOURCES})
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2_image REQUIRED sdl2_image)
INCLUDE_DIRECTORIES $ {SDL2_INCLUDE_DIRS} $ {SDL2IMAGE_INCLUDE_DIR})
TARGET_LINK_LIBRARIES($ {PROJECT_NAME} $ {SDL2_LIBRARIES} $ {SDL2IMAGE_LIBRARY})
我得到这些错误:
在函数`loadTexture(std :: basic_string& :char_traits< char>,std :: allocator< char> > const& SDL_Renderer *)':
未定义引用`IMG_LoadTexture'
collect2:ld返回1退出状态
这是函数调用:
#includeSDL.h
# includeSDL_image.h
SDL_Texture * loadTexture(const std :: string& file,SDL_Renderer * ren){
SDL_Texture * texture = IMG_LoadTexture(ren,file.c_str ;
texture!= nullptr或die(LoadTexture);
return texture;
}
我绝望。请帮帮我!
谢谢! :)
我觉得下面的工作,因为它发现我的Ubuntu系统中的磁带库,并且提供的示例功能可以链接:
项目(shooter-cmake2)
cmake_minimum_required(VERSION 2.8)
组(CMAKE_CXX_FLAGS$ {} CMAKE_CXX_FLAGS -std =的C ++ 0x)
add_executable($ {} PROJECT_NAME的src / TEST.CPP)
INCLUDE (FindPkgConfig)
PKG_SEARCH_MODULE(SDL2必填SDL2)
PKG_SEARCH_MODULE(SDL2IMAGE必填SDL2_image> = 2.0.0)
INCLUDE_DIRECTORIES($ {SDL2_INCLUDE_DIRS} $ {SDL2IMAGE_INCLUDE_DIRS} )
TARGET_LINK_LIBRARIES($ {PROJECT_NAME} $ {SDL2_LIBRARIES} $ {SDL2IMAGE_LIBRARIES})
如果cmake使用--debug-output执行输出:
- 找到PkgConfig:/ usr / bin / pkg-config发现版0.26)
从名为:[2] /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake
[1] $ USER /堆栈溢出/ cmake的-SDL2图像/的CMakeLists.txt
- 检查来自调用模块'SDL2'
中的一个:[1] $ USER /堆栈溢出/ cmake的-SDL2图像/的CMakeLists.txt
- 检查一个模块SDL2_image> = 2.0.0'
从名为:[1] $ USER /堆栈溢出/ cmake的-SDL2图像/的CMakeLists.txt
这让我检查
的内容
/usr/lib/x86_64-linux-gnu/pkgconfig/sdl2.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/SDL2_image.pc
我注意到,SDL2_image.pc包含
名称:SDL2_image
我认为应该匹配第三个参数PKG_SEARCH_MODULE该库I'm looking for the simplest way to compile a c++ program using SDL2 and SDL_image with cmake.
Here is my best attempt, after hours of searching:
CMakeLists.txt
project(shooter-cmake2) cmake_minimum_required(VERSION 2.8) set(SOURCES shooter.cpp classes.cpp utils.cpp ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") add_executable(${PROJECT_NAME} ${SOURCES}) INCLUDE(FindPkgConfig) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) PKG_SEARCH_MODULE(SDL2_image REQUIRED sdl2_image) INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIR}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY})
I get these errors:
In function `loadTexture(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, SDL_Renderer*)': undefined reference to `IMG_LoadTexture' collect2: ld returned 1 exit status
Here is the function call:
#include "SDL.h" #include "SDL_image.h" SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){ SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str()); texture != nullptr or die("LoadTexture"); return texture; }
I am desperate. Please help me!Thanks! :)
解决方案I think that the following will work, as it finds the libraries on my ubuntu system and the example function you provided can link:
project(shooter-cmake2) cmake_minimum_required(VERSION 2.8) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") add_executable(${PROJECT_NAME} src/test.cpp) INCLUDE(FindPkgConfig) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0) INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES})
If cmake is executed with --debug-output it outputs:
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26") Called from: [2] /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt -- checking for one of the modules 'sdl2' Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt -- checking for one of the modules 'SDL2_image>=2.0.0' Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
This made me check the contents of
/usr/lib/x86_64-linux-gnu/pkgconfig/sdl2.pc /usr/lib/x86_64-linux-gnu/pkgconfig/SDL2_image.pc
I noticed that SDL2_image.pc contains Name: SDL2_imagewhich I assumed should match the third parameter to PKG_SEARCH_MODULE for this library.
这篇关于如何使用SDL2和SDL_image与cmake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!