0

我已经像这样下载了 SDL2.0:

~/tmp>wget http://www.libsdl.org/tmp/release/SDL2-2.0.0.tar.gz

然后我解压它,进入目录,然后./configure && make && make install. 一切都很顺利。现在我制作了一个非常简单的示例(见下文)。我的问题是当我尝试编译时它不起作用,所以我将 SDL2.dll 复制到当前目录并尝试了所有我知道的组合但没有成功:

~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -l SDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -lSDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -L SDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -l SDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -lSDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -L SDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2

所有错误都是:

/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: cannot find -lSDL2.dll
collect2: error: ld returned 1 exit status

或者:

933012@F9ZW00118371 ~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2.dll
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xe): undefined reference to `SDL_Init'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x42): undefined reference to `SDL_CreateWindow'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x50): undefined reference to `SDL_GetWindowSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x67): undefined reference to `SDL_RWFromFile'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x77): undefined reference to `SDL_LoadBMP_RW'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x9c): undefined reference to `SDL_UpperBlit'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xa7): undefined reference to `SDL_UpdateWindowSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xb2): undefined reference to `SDL_FreeSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xda): undefined reference to `SDL_PollEvent'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xef): undefined reference to `SDL_DestroyWindow'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xf4): undefined reference to `SDL_Quit'
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o: bad reloc address 0x20 in section `.eh_frame'
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status

你会怎么做?

这是我尝试编译的源代码:

#include <SDL.h>

int main(int argc, char *argv[]) {
    int running;
    SDL_Window *window;
    SDL_Surface *windowsurface;
    SDL_Surface *image;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("Hello World",
                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                              592, 460, SDL_WINDOW_SHOWN);
    windowsurface = SDL_GetWindowSurface(window);

    image = SDL_LoadBMP("exampleimage.bmp");
    SDL_BlitSurface(image, NULL, windowsurface, NULL);

    SDL_UpdateWindowSurface(window);
    SDL_FreeSurface(image);

    running = 1;
    while (running) {
        while (SDL_PollEvent(&event) != 0) {
            if (event.type == SDL_QUIT) {
                running = 0;
                break;
            }
        }
    }
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
 }
4

2 回答 2

2

不确定这是否适用于 cygwin,但在我的 Mac 上使用 pkg-config 我能够找到编译器标志:

gcc example.c $(pkg-config --cflags --libs sdl2)
于 2013-10-14T19:16:23.850 回答
2

你应该链接这个标志:-Lwhere/sdldll/is -lSDL2-Iwhere/sdlincludes/are

-L标志是二进制文件(dll 和 lib)所在的位置,-l是 dll(或 lib)的文件名 -I标志是包含文件所在的位置(.h)

gcc -o program.exe main.cpp -LSDL2/bin -lSDL2 -ISDL2/include
于 2016-03-11T17:18:59.730 回答