Search code examples
c++linuxsublimetext3sdl

I cannot seem to get SDL to work with sublime text


I am making a simple game and wanted to use SDL for the graphics. I run linux Ubuntu, and use sublime text editor, g++ compiler, and I am coding in c++. I downloaded SDL and followed these steps.

After I followed those steps, all of the SDL error stopped appearing. However, the flag variables aren't working.

This is the code:

#include <SDL2/SDL.h>
Risk() {
    SDL_Init(SDL_INIT_HAPTIC);
    window = SDL_CreateWindow("Board",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500,500, SDL_WINDOW_RESIZABLE);
    SDL_GetError();
}

The error appearing in my compiler is:

tom@TBT-XPS-13-9360:~/Documents/Subjects/CS/Fun/Risk$ g++ -std=c++14 
Game.cpp -W/tmp/ccLwSxiL.o: In function `Risk::Risk()':
Game.cpp:(.text._ZN4RiskC2Ev[_ZN4RiskC5Ev]+0x1f): undefined reference 
to `SDL_Init'
Game.cpp:(.text._ZN4RiskC2Ev[_ZN4RiskC5Ev]+0x44): undefined reference 
to `SDL_CreateWindow'
Game.cpp:(.text._ZN4RiskC2Ev[_ZN4RiskC5Ev]+0x54): undefined reference 
to `SDL_GetError'
collect2: error: ld returned 1 exit status

I think the error is the SDL libraries are in the wrong place, or Sublime doesn't know where they are.


Solution

  • The problem is that Sublime is not adding the required instruction to indicate that the executable should be linked to the SDL2 library.

    You can indicate it with the parameter -lSDL2 on the command line, or use the sdl2-config program with sdl2-config --libs, something like this:

    $ g++ -o executable-name source.cpp -lSDL2

    or

    $ g++ -o executable-name source.cpp $(sdl2-config --libs)

    sdl2-config is just an utility that outputs appropriate configuration options for compiling and linking. You can see it by running it alone:

    $ sdl2-config --libs

    it should output something like:

    -L/usr/lib -lSDL2

    You can see the same -lSDL2 mentioned before, and also a -L/usr/lib instruction that indicates the linker to include libraries in the /usr/lib/ directory in its search path. In general, you should use the sdl2-config for getting the required configuration options to pass to the compiler instead of indicating them by yourself, unless you know what you're doing, of course.

    Instead of going to the command line to compile, you can use the Make build system in Sublime Text. Add a file named Makefile to your project directory with the following content:

    # OBJS place here every file to compile as part of the project
    OBJS = Game.cpp
    
    # CC compiler to use
    CC = g++
    
    # COMPILER_FLAGS compilation options
    COMPILER_FLAGS = -std=c++14 -Wall `sdl2-config --cflags`
    
    # LINKER_FLAGS libraries to link
    LINKER_FLAGS = `sdl2-config --libs`
    
    # OBJ_NAME executable
    OBJ_NAME = mygame
    
    all : $(OBJS)
        $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) 
    

    Then go to menu Tools -> Build System and select Make. Now try building your project, if successful, a mygame executable should have been created.