Sean Macfoy
Sean Macfoy

Reputation: 33

C++ development in Windows with Cygwin, gcc and SDL2

I installed Cygwin then compiled and installed SDL2 from source with the following bash command:

./configure && make && make install

as suggested at http://www.libsdl.org/extras/win32/cygwin/README.txt. Subsequently, I attempted to compile the following minimal SDL2 project (detailed source code is believed to be unimportant, but included for completeness):

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;
}

using the following invocation of g++:

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

and received the following error from 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

Upvotes: 1

Views: 2243

Answers (1)

WeRelic
WeRelic

Reputation: 290

I wrestled with the same issue for a while...

I ended up using the following:

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

The main issue I ran into was that -lSDL2main had to be linked before -lSDL2. However, I'm pretty sure that your issue is the lack of the -L/usr/local/lib bit. Add those two fixes, and you should be good to go.

Upvotes: 1

Related Questions