0

I have arch linux installed as my operating system. I have codelite installed as my c++ IDE. I have SDL2 downloaded.

I'm trying to get SDL to work with c++, but everything I do, it doesn't work.

I've searched for hours trying to get this to work and it just doesn't want to work. I can't find any .dll or .a or .so files for the library linker. When I was on windows I had to locate them and put the directories in the linker settings, but I can't find any .dll's. Now it recognizes SDL_Window, and SDL_Surface. But I'm getting errors when trying to build my project saying undefined reference to...

Here is my source code:

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

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

bool init();
bool loadMedia();
void close();

SDL_Window* window = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;

bool init() {
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
        success = false;
    } else {
        window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (window == NULL) {
            printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
            success = false;
        } else {
            gScreenSurface = SDL_GetWindowSurface(window);
        }
    }

    return success;
}

bool loadMedia() {
    bool success = true;
    gHelloWorld = SDL_LoadBMP("hello_world.bmp");
    if (gHelloWorld == NULL) {
        printf("Unable to load image %s! SDL Error: %s\n", "hello_world.bmp", SDL_GetError());
        success = false;
    }

    return success;
}

void close() {
    SDL_FreeSurface(gHelloWorld);
    gHelloWorld = NULL;

    SDL_DestroyWindow(window);
    window = NULL;

    SDL_Quit();
}

int main(int argc, char **argv)
{
    if (!init()) {
        printf("Failed to initialize!\n");
    } else {
        if (!loadMedia()) {
            printf("Failed to load media!\n");
        } else {
            SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
            SDL_UpdateWindowSurface(window);
            SDL_Delay(3000);
        }
    }

    close();

    return 0;
}

Here is my output:

/bin/sh -c '/usr/bin/make -j4 -e -f  Makefile'
----------Building project:[ Test - Debug ]----------
make[1]: Entering directory '/home/vinny/Documents/Projects/Test'
/usr/bin/g++  -c  "/home/vinny/Documents/Projects/Test/main.cpp" -g -O0 -Wall  -o ./Debug/main.cpp.o -I. -I. -I/home/vinny/Downloads/SDL2-2.0.7/include
/usr/bin/g++ -o ./Debug/Test @"Test.txt" -L.
./Debug/main.cpp.o: In function `init()':
/home/vinny/Documents/Projects/Test/main.cpp:17: undefined reference to `SDL_Init'
/home/vinny/Documents/Projects/Test/main.cpp:18: undefined reference to `SDL_GetError'
/home/vinny/Documents/Projects/Test/main.cpp:21: undefined reference to `SDL_CreateWindow'
/home/vinny/Documents/Projects/Test/main.cpp:23: undefined reference to `SDL_GetError'
/home/vinny/Documents/Projects/Test/main.cpp:26: undefined reference to `SDL_GetWindowSurface'
./Debug/main.cpp.o: In function `loadMedia()':
/home/vinny/Documents/Projects/Test/main.cpp:35: undefined reference to `SDL_RWFromFile'
/home/vinny/Documents/Projects/Test/main.cpp:35: undefined reference to `SDL_LoadBMP_RW'
/home/vinny/Documents/Projects/Test/main.cpp:37: undefined reference to `SDL_GetError'
./Debug/main.cpp.o: In function `close()':
/home/vinny/Documents/Projects/Test/main.cpp:45: undefined reference to `SDL_FreeSurface'
/home/vinny/Documents/Projects/Test/main.cpp:48: undefined reference to `SDL_DestroyWindow'
/home/vinny/Documents/Projects/Test/main.cpp:51: undefined reference to `SDL_Quit'
./Debug/main.cpp.o: In function `main':
/home/vinny/Documents/Projects/Test/main.cpp:62: undefined reference to `SDL_UpperBlit'
/home/vinny/Documents/Projects/Test/main.cpp:63: undefined reference to `SDL_UpdateWindowSurface'
/home/vinny/Documents/Projects/Test/main.cpp:64: undefined reference to `SDL_Delay'
collect2: error: ld returned 1 exit status
make[1]: *** [Test.mk:79: Debug/Test] Error 1
make[1]: Leaving directory '/home/vinny/Documents/Projects/Test'
make: *** [Makefile:5: All] Error 2
====14 errors, 0 warnings====

I am new to the linux OS so I may not have tried everything. Please help. I haven't been able to program since I switched over to linux. :(

Vince
  • 2,596
  • 11
  • 43
  • 76
  • 1
    Possible duplicate of [How do i add libraries to CodeLite projects?](https://stackoverflow.com/questions/34121370/how-do-i-add-libraries-to-codelite-projects) – aram Feb 09 '18 at 12:17
  • 2
    For clarification in this step `/usr/bin/g++ -o ./Debug/Test @"Test.txt" -L.` you're not linking against SDL (-lSDL). You should see how to properly add the libraries to your IDE/Build file. – aram Feb 09 '18 at 12:19
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – genpfault Feb 09 '18 at 15:04
  • Thanks guys. I got it working. I forgot about the linking. I was just thinking about the includes and libs. – Vince Mar 02 '18 at 06:46

1 Answers1

1

What @Aram said in his comment - except it needs to be -lSDL2 instead of -lSDL, since it is SDL2 which you are including and programming against, not simply SDL.

nondeterministic
  • 501
  • 5
  • 17