我正在使用Windows上的SDL2在CLion中开展一个小项目。
当我在CLion中编译并运行(Shift + F10)我的程序时,它不会显示任何控制台输出。但是,当我使用调试器(Shift + F9)运行它时,它确实显示控制台输出..
我不知道造成这种情况的原因。
为了确保我的项目或CLion没有破坏某些内容,我已将源代码复制到新项目并使用相同的CMakeList.txt
文件进行设置,但仍然没有工作
我的CMakeList.txt
:
CMakeList.txt
cmake_minimum_required(VERSION 3.6)
project(SDL_Project)
set(CMAKE_CXX_STANDARD 14)
# FindSDL2.cmake
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
#find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
include_directories(${SDL2_IMAGE_INCLUDE_DIR})
#include_directories(${SDL2_TTF_INCLUDE_DIR})
add_executable(SDL_Project src/main.cpp src/Game.cpp src/Game.h)
set_target_properties(SDL_Project PROPERTIES WIN32_EXECUTABLE FALSE)
target_link_libraries(SDL_Project ${SDL2_LIBRARY})
target_link_libraries(SDL_Project ${SDL2_IMAGE_LIBRARIES})
#target_link_libraries(SDL_Project ${SDL2_TTF_LIBRARIES})
我也尝试编译以下代码(没有SDL2,但使用相同的CMakeList.txt
):
#include <iostream>
int main(int argv, char* args[]) {
std::cout << "Hello World!" << std::endl;
return 0;
}
这有同样的问题;没有控制台输出。
在find_package
中没有include_directories
,target_link_libraries
和CMakeList.txt
的情况下编译上述代码会显示控制台输出!所以它与SDL2有关,我认为......?
有谁知道造成这种情况的原因,以及如何解决?
虽然我认为问题出在CMakeList.txt
,但这是剩下的代码:
main.cpp
#include <SDL.h>
#include <iostream>
#include "Game.h"
int main(int argv, char* args[]) {
const int FPS = 60;
const uint32_t frameDelay = 1000 / FPS;
const WindowSettings settings = {SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720};
Game game;
Uint32 frameStart;
Uint32 frameTime;
game.init("Game", settings);
while (game.isRunning()) {
frameStart = SDL_GetTicks();
game.handleEvents();
game.tick();
game.render();
frameTime = SDL_GetTicks() - frameStart;
std::cout << "Frame time: " << frameTime << " / " << frameDelay << std::endl;
if (frameTime < frameDelay) {
std::cout << "Frame delay: " << (frameDelay - frameTime) << std::endl;
SDL_Delay(frameDelay - frameTime);
}
}
game.cleanup();
return 0;
}
Game.h
#ifndef SDL_PROJECT_GAME_H
#define SDL_PROJECT_GAME_H
#include <SDL.h>
struct WindowSettings {
int x;
int y;
int width;
int height;
};
class Game {
private:
bool running;
SDL_Window* window;
SDL_Renderer* renderer;
public:
Game();
~Game();
void init(const char* title, const WindowSettings &settings);
void tick();
void render();
void cleanup();
void handleEvents();
bool isRunning() { return running; }
};
#endif //SDL_PROJECT_GAME_H
Game.cpp
#include <iostream>
#include "Game.h"
Game::Game():
running(false),
window(nullptr),
renderer(nullptr) {
}
Game::~Game() = default;
void Game::init(const char *title, const WindowSettings &settings) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "SDL initialization failed: " << SDL_GetError() << std::endl;
return;
};
window = SDL_CreateWindow(title, settings.x, settings.y, settings.width, settings.height, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cout << "Unable to create window: " << SDL_GetError() << std::endl;
return;
}
renderer = SDL_CreateRenderer(window, -1 , 0);
if (renderer == nullptr) {
std::cout << "Unable to create renderer: " << SDL_GetError() << std::endl;
return;
}
running = true;
}
void Game::tick() {
}
void Game::render() {
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
void Game::cleanup() {
if (renderer) {
SDL_DestroyRenderer(renderer);
}
if (window) {
SDL_DestroyWindow(window);
}
SDL_Quit();
std::cout << "Game clean exit" << std::endl;
}
void Game::handleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
}
答案 0 :(得分:1)
我能想到1个问题。你确定你的运行配置是正确的CLION吗?调试版本可能正在运行与您想象的不同的可执行文件。 CLION在幕后做了很多事情。单击运行箭头下拉列表,查看编辑配置或其他相应内容。
答案 1 :(得分:0)
好吧,我想通了......
显然,compiling on Windows redirects stdout and stderr to null(?)。我无法找到/删除影响它的编译器标志,但是,SDL provides SDL_Log,实际上输出到控制台(并且可能是stdout?)。
由于我无论如何都在使用SDL2,我不妨使用记录器。它支持printf风格的格式化,这是一个很好的奖励。如果我想重定向日志消息,它允许我设置自定义日志记录功能。