无法运行SDL2项目,“无法执行”“:%1不是有效的Win32应用程序”

时间:2017-04-05 18:36:03

标签: c++ qt-creator sdl-2

我正在尝试自学如何将SDL2与Qt Creator一起使用,但不断进入这些“让我编码”的情况。目前,我只是想打开一个带有绿色图片的窗口(做一个教程),但是我收到了这个错误:

Cannot execute "": %1 not a valid Win32-application

我正在使用MinGw 5.3.0 32bit2作为编译器,我已经设法运行其他没有此错误的项目。我试着修改Build and Run配置,但没有运气。目前我的(必要)运行配置如下所示:

Executable C:\...\sdl_test\debug\sdl_test.exe
Working directory: C:\...\sdl_test

另外参考,这里是main.cpp:

#include <iostream>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

using namespace std;



int main()
{
    bool quit = false;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = nullptr;
    window = SDL_CreateWindow("MVP", 100, 100, 600, 400,
                              SDL_WINDOW_SHOWN);

    if(window == nullptr){
        cout << "Window could not be created." << endl;
        return 0;
    }

    SDL_Renderer* renderer = nullptr;
    renderer = SDL_CreateRenderer(window, -1,
                                  SDL_RENDERER_ACCELERATED);
    SDL_Event* mainEvent = new SDL_Event();

    SDL_Texture* grass_image = NULL;
    grass_image = IMG_LoadTexture(renderer, "grass.bmp");

    SDL_Rect grass_rect;
    grass_rect.x = 10;
    grass_rect.y = 50;
    grass_rect.w = 250;
    grass_rect.h = 250;

    while(!quit && mainEvent->type != SDL_QUIT){
        SDL_PollEvent(mainEvent);
        SDL_RenderClear(renderer);

        SDL_RenderCopy(renderer, grass_image, NULL, &grass_rect);

        SDL_RenderPresent(renderer);
    }
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    delete mainEvent;

    return 0;

}

感觉绝望,因为我只想打开一个带有绿色矩形的窗口......这太难以问了吗?!

编辑: 这是我的.pro文件

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp


win32: LIBS += -L$$PWD/../../../../SDL2-2.0.5/i686-w64-mingw32/lib/ -lmingw32 -lSDL2main -lSDL2

INCLUDEPATH += $$PWD/../../../../SDL2-2.0.5/i686-w64-mingw32/include
DEPENDPATH += $$PWD/../../../../SDL2-2.0.5/i686-w64-mingw32/include

win32: LIBS += -L$$PWD/../../../../SDL2_image-2.0.1/i686-w64-mingw32/lib/ -lmingw32 -lSDL2_image

INCLUDEPATH += $$PWD/../../../../SDL2_image-2.0.1/i686-w64-mingw32/include
DEPENDPATH += $$PWD/../../../../SDL2_image-2.0.1/i686-w64-mingw32/include

win32:QMAKE_LFLAGS += -shared

1 个答案:

答案 0 :(得分:0)

您向我们展示的代码适用于Linux和任何理智的系统:)

您的问题可能存在一些问题:

1 - 您正尝试在32位系统中运行64位应用程序

2 - 你的dll可能已经腐败

3 - 你的SDL dll可以用Microsoft Visual Studio编译(你需要用MinGW生成dll)

另外,你应该向编译器传递一些标志,因为你告诉我们你使用的是Qt Creator,可能你有一个projectname.pro文件或一个CMakeLists.txt

发现你拥有的东西,并将这些标志传递给编译器:

-Wl,-subsystem,windows

并链接这些库

-lmingw32 -lSDL2main -lSDL2