You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于C++和SDL2的CLion小型项目崩溃,退出码468求助

退出码468的含义与问题排查

退出码468的含义

退出码468对应Windows系统错误代码ERROR_INVALID_WINDOW_HANDLE(十六进制0x1D4),但结合你描述的“程序未执行首行代码”的情况,这个退出码并非你的程序主动返回,而是程序启动阶段因依赖动态库加载失败,由系统直接终止并返回的错误码。

核心问题分析

你在引入SDL2前程序正常运行,说明问题出在SDL2/SDL_image的运行时依赖配置上,而非代码逻辑:

  • 动态库缺失:SDL2和SDL_image的运行时DLL未放置在可执行文件的同级目录,也不在系统默认搜索路径中,导致程序启动时无法加载必要依赖,直接被系统终止。
  • CMake配置疏漏:编译时仅链接了库文件,但未将运行时DLL复制到输出目录,或链接的库版本(32/64位)与编译器环境不匹配。

你的项目代码

main.cpp

#include <iostream>
#include "type_test.h"
#include "sdl_include.h"
using namespace std;


int main(int argc, char *argv[]){
    cout << "Hello, World!" << endl;
    Bidule bidule = Bidule(5, 'e');
    cout << bidule.get_truc() << endl;
    cout << bidule.get_machin() << endl;
    if (SDL_Init(SDL_INIT_EVERYTHING)<0){
        cout << "Failed to initialise SDL. Error cause : " << SDL_GetError() << endl;
        return 1;
    }
    SDL_Window* win = SDL_CreateWindow("Titre", WIN_NOANCHOR, WIN_NOANCHOR, 160, 144, SDL_WINDOW_SHOWN);
    if (win == nullptr){
        cout << "Failed to create the window. Error cause : " << SDL_GetError() << endl;
        return 1;
    }
    SDL_Renderer* renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED| SDL_RENDERER_PRESENTVSYNC);
    if (renderer == nullptr){
        cout << "Failed to create a renderer for the window. Error cause : " << SDL_GetError() << endl;
        return 1;
    }
    SDL_SetRenderDrawColor(renderer, 0, 0, 0xFF, 0xFF);
    int init_img = IMG_INIT_PNG;
    if (!(IMG_Init(init_img) & init_img)){
        cout << "Failed to initialise SDL_image. Error cause : " << IMG_GetError() << endl;
    }
    bool running = true;
    SDL_Event evt;

    while (running){
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
        while (SDL_PollEvent(&evt)) {
            if (evt.type == SDL_QUIT) {
                running = false;
                break;
            }
        }
    }
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}

type_test.h

#ifndef TEST_CLION_TYPE_TEST_H
#define TEST_CLION_TYPE_TEST_H

class Bidule{
    public:
        Bidule(short truc, char machin);
        short get_truc();
        char get_machin();
    private:
        short truc;
        char machin;
};

#endif //TEST_CLION_TYPE_TEST_H

type_test.cpp

#include "type_test.h"

Bidule::Bidule(short truc, char machin){
    this->truc = truc;
    this->machin = machin;
}

short Bidule::get_truc(){
    return this->truc;
}

char Bidule::get_machin(){
    return this->machin;
}

sdl_include.h

#ifndef TEST_CLION_SDL_INCLUDE_H
#define TEST_CLION_SDL_INCLUDE_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#define WIN_NOANCHOR SDL_WINDOWPOS_UNDEFINED

#endif //TEST_CLION_SDL_INCLUDE_H

解决方案

  1. 手动复制运行时DLL
    找到SDL2和SDL_image安装目录下的SDL2.dll、SDL2_image.dll,以及SDL_image依赖的第三方DLL(如libpng.dll、zlib.dll等),将它们复制到CLion编译生成的可执行文件所在目录(通常是build/Debug或build/Release)。

  2. CMake自动复制DLL
    在你的CMakeLists.txt中添加以下逻辑,实现编译后自动复制DLL到输出目录:

    find_package(SDL2 REQUIRED)
    find_package(SDL2_image REQUIRED)
    
    add_executable(你的项目名 main.cpp type_test.cpp)
    target_link_libraries(你的项目名 PRIVATE SDL2::SDL2 SDL2_image::SDL2_image)
    
    # 复制SDL2 DLL
    add_custom_command(TARGET 你的项目名 POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:SDL2::SDL2>
        $<TARGET_FILE_DIR:你的项目名>)
    
    # 复制SDL2_image DLL
    add_custom_command(TARGET 你的项目名 POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:SDL2_image::SDL2_image>
        $<TARGET_FILE_DIR:你的项目名>)
    
  3. 验证库版本匹配
    确保你下载的SDL2/SDL_image库位数(32位/64位)与CLion当前使用的编译器位数一致(可通过File > Settings > Build, Execution, Deployment > Toolchains查看)。

内容的提问来源于stack exchange,提问作者fortwoone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 02:40:07