使用Cygwin,gcc和SDL2在Windows中进行C ++开发

时间:2014-01-31 22:38:30

标签: linker g++ cygwin sdl sdl-2

我安装了Cygwin,然后使用以下bash命令从源代码编译和安装SDL2:

./configure && make && make install

正如http://www.libsdl.org/extras/win32/cygwin/README.txt所建议的那样。随后,我尝试编译以下最小的SDL2项目(详细的源代码被认为是不重要的,但包括完整性):

main.cpp中:

/* ----- HEADERS ------ */

#include <SDL2/SDL.h>
#include <iostream>
#include <string>
#include <cstdlib>

/* ----- GLOBALS ----- */

SDL_Window* g_pWindow = nullptr;
SDL_Renderer* g_pRenderer = nullptr;
bool g_bRunning = true;
SDL_Event g_event;

/* ----- CONSTANTS ----- */

// window parameters
const std::string   WIN_TITLE   = "Hello, SDL!";
const int           WIN_XPOS    = SDL_WINDOWPOS_CENTERED;
const int           WIN_YPOS    = SDL_WINDOWPOS_CENTERED;
const int           WIN_WIDTH   = 640;
const int           WIN_HEIGHT  = 480;
const Uint32        WIN_FLAGS   = SDL_WINDOW_SHOWN;

// renderer parameters
const int       REN_INDEX = -1;
const Uint32    REN_FLAGS = 0;

// clear color
const Uint8 CLEAR_RED   = 0x00;
const Uint8 CLEAR_GREEN = 0x00;
const Uint8 CLEAR_BLUE  = 0x00;
const Uint8 CLEAR_ALPHA = 0xFF;

/* ----- MAIN FUNCTION ----- */

int main()
{
    // initialize SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) > 0)
    {
            atexit(SDL_Quit);
    }
    else
    {
        std::cout << SDL_GetError() << std::endl;
        exit(1);
    }

    // create window
    g_pWindow = SDL_CreateWindow(WIN_TITLE.c_str(), WIN_XPOS, WIN_YPOS, WIN_WIDTH, WIN_HEIGHT, WIN_FLAGS);

    if (g_pWindow == nullptr)
    {
            std::cout << SDL_GetError() << std::endl;
        exit(1);
    }

    // create renderer
    g_pRenderer = SDL_CreateRenderer(g_pWindow, REN_INDEX, REN_FLAGS);

    if (g_pRenderer == nullptr)
    {
        std::cout << SDL_GetError() << std::endl;
        exit(1);
    }

    // set clear color
    SDL_SetRenderDrawColor(g_pRenderer, CLEAR_RED, CLEAR_GREEN, CLEAR_BLUE, CLEAR_ALPHA);

    // loop
    while (g_bRunning)
    {
        // events
        while (SDL_PollEvent(&g_event))
        {
            switch (g_event.type)
            {
                // user quit
                case SDL_QUIT:
                    g_bRunning = false;
                    break;

                default:
                    break;
            }
        }

        // render
        SDL_RenderClear(g_pRenderer);
        SDL_RenderPresent(g_pRenderer);
    }

    // cleanup
    SDL_DestroyWindow(g_pWindow);
    SDL_DestroyRenderer(g_pRenderer);

    return 0;
}

使用以下g ++调用:

g++ -W -Wall -std=c++11 main.cpp -o game -lSDL2 -lSDL2main

并从g ++收到以下错误:

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

1 个答案:

答案 0 :(得分:1)

我有一段时间与同样的问题搏斗......

我最终使用了以下内容:

g++ -W -Wall main.cpp -o game -std=c++11 -L/usr/local/lib -lSDL2main -SDL2

我遇到的主要问题是-lSDL2main必须在-lSDL2之前关联。但是,我很确定您的问题是缺少-L/usr/local/lib位。添加这两个修复程序,你应该很高兴。