我正在探索SDL并编写以下简单代码
#include <SDL2/SDL.h>
int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0){
return 0;
}
SDL_Window* window = SDL_CreateWindow("Hello World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
600,
400,
SDL_WINDOW_SHOWN);
if (window == nullptr) {
return 0;
}
SDL_Surface* surface = SDL_GetWindowSurface(window);
SDL_FillRect(surface,
nullptr,
SDL_MapRGB(
surface->format,
0xFF,
0xFF,
0xFF));
SDL_UpdateWindowSurface(window);
SDL_Delay(9000);
SDL_DestroyWindow(window);
return 0;
}
并为Clion提供以下cmake文件
cmake_minimum_required(VERSION 3.9)
project(LazyFoo)
set(CMAKE_CXX_STANDARD 14)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(LazyFoo main.cpp)
target_link_libraries(LazyFoo ${SDL2_LIBRARY})
问题是,当我直接从命令行构建main.cpp而不使用cmake时,代码编译并显示一个漂亮的窗口,但是当我使用clion构建代码时,它会抛出未定义的引用我上面使用的所有SDL函数。
我知道问题出在我的cmake文件上并且用google搜索相同但都是徒劳的。
如何更改我的cmake文件以便能够从CLion本身构建项目。
修改 我的clion构建消息
/home/tarptaeya/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/173.4674.29/bin/cmake/bin/cmake --build /home/tarptaeya/DevGames/LazyFoo/cmake-build-debug --target LazyFoo -- -j 2
Scanning dependencies of target LazyFoo
[ 50%] Building CXX object CMakeFiles/LazyFoo.dir/main.cpp.o
[100%] Linking CXX executable LazyFoo
CMakeFiles/LazyFoo.dir/main.cpp.o: In function `main':
/home/tarptaeya/DevGames/LazyFoo/main.cpp:4: undefined reference to `SDL_Init'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:12: undefined reference to `SDL_CreateWindow'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:16: undefined reference to `SDL_GetWindowSurface'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:17: undefined reference to `SDL_MapRGB'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:23: undefined reference to `SDL_FillRect'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:24: undefined reference to `SDL_UpdateWindowSurface'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:25: undefined reference to `SDL_Delay'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:26: undefined reference to `SDL_DestroyWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/LazyFoo.dir/build.make:94: recipe for target 'LazyFoo' failed
make[3]: *** [LazyFoo] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/LazyFoo.dir/all' failed
make[2]: *** [CMakeFiles/LazyFoo.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/LazyFoo.dir/rule' failed
make[1]: *** [CMakeFiles/LazyFoo.dir/rule] Error 2
Makefile:118: recipe for target 'LazyFoo' failed
make: *** [LazyFoo] Error 2