如何在Linux上使用SDL 2进行交叉编译以供Windows使用

4

我在 Arch Linux (64位) 上使用 mingw-w64-g++ 编译器尝试编译一个使用 SDL 2 的简单 C++ 程序。

为此,我从 这里 下载了 SDL2-devel-2.0.4-mingw.tar.gz。

prog.cpp:

#include <SDL.h>

int main ()
{
    SDL_Init (SDL_INIT_VIDEO);

    SDL_Window *sdlWnd = SDL_CreateWindow ("Test", SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);

    SDL_Event event;
    bool running = true;

    while (running) {
        while (SDL_PollEvent (&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
                break;
            }
        }
    }

    return 0;
}

Makefile:

GPP = x86_64-w64-mingw32-g++
prog.exe: prog.o
    $(GPP) -o prog.exe prog.o -LSDL2-2.0.4/lib/x64 -lSDL2main -lSDL2
prog.o: prog.cpp
    $(GPP) -o prog.o -c -ISDL2-2.0.4/include prog.cpp

现在,制作时出现了错误:

x86_64-w64-mingw32-g++ -o prog.exe prog.o -LSDL2-2.0.4/lib/x64 -lSDL2main -lSDL2
Warning: corrupt .drectve at end of def file
SDL2-2.0.4/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[main]+0x1c): undefined reference to `SDL_main'

为什么会出现“undefined reference to `SDL_main'”的错误?尽管我已经指定了“-lSDL2main”?我做错了什么? :(

可能是重复的问题:无法找到sdl2main - Pierre
1个回答

1

好的,这是因为主要函数签名必须声明为:

int main(int argc, char *argv[])

根据官方SDL常见问题解答

Make sure that you are declaring main() as:

#include "SDL.h"

int main(int argc, char *argv[])

You should be using main() instead of WinMain() even though you are creating a Windows application, because SDL provides a version of WinMain() which performs some SDL initialization before calling your main code. If for some reason you need to use WinMain(), take a look at the SDL source code in src/main/win32/SDL_main.c to see what kind of initialization you need to do in your WinMain() function so that SDL works properly.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接