When I compile the following two programs one of them compiles and one of themse does not but I don't see a difference. The smaller program is the same only I've taken out a lot of the code so that I'm left with only the minimum that I need to display a window with SDL. I think it would be easier to learn with fewer lines of code and slowly learn the extra and more complicated aspects of using SDL.

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

SDL_Surface* g_pMainSurface = NULL;
SDL_Event g_Event;

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    g_pMainSurface = SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT);

    return 0;
}

In the code above I'm getting this error -> Window.cpp|10|undefined reference to SDL_Init'|
Window.cpp|12|undefined reference to SDL_SetVideoMode'|

The following program compiles fine.

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

SDL_Surface* g_pMainSurface = NULL;
SDL_Event g_Event;

int main(int argc, char* argv[])
{
    if(SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        fprintf(stderr, "Could not initialize SDL!\n");
        exit(1);
    }
    else
    {
        fprintf(stdout, "SDL initialized properly!\n");
        atexit(SDL_Quit);
    }

    g_pMainSurface = SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT);

    if(!g_pMainSurface)
    {
        fprintf(stderr, "Could not create main surface!\n");
        exit(1);
    }

    for(;;)
    {
        if(SDL_WaitEvent(&g_Event) == 0)
        {
            fprintf(stderr, "Error while waiting for an event!\n");
            exit(1);
        }

        // check the type of event
        if(g_Event.type == SDL_QUIT)
        {
            fprintf(stdout, "Quit event has occurred.\n");
            break;
        }
    }

    fprintf(stdout, "Terminating program normally.\n");

    return 0;
}

Both programs actually compile fine. The error you got for the first program is a linker error, meaning that it compiled fine but it couldn't link. The error in question is caused by forgetting to link with the SDL library. It is not sufficient to include the header file, you also have to link to the library. Just look at the FAQ for SDL.

If you are confused with the compilation and linking process, I recommend reading my tutorial on that subject.

Thanks mide_2000_17 I'm reading your artical now. I guess my question now is, do you now where in my system the sdl library I need would be at, on a Linux Mint system? I also have a few questions about some of the thing I read in your tutorial but all get with you later on those.

Libraries on Linux are usually located in /usr/lib and /usr/local/lib, or some 32-64 variants of that. Usually, you should be able to simply pass the option -lSDL to the compiler, and it will find it for you (at least, on my system, this works fine). It works if I take your code and run this:

$ g++ simple_sdl_test.cpp -lSDL -o simple_sdl_test

If not, you can also use a simple locate command to find what you are looking for, like $ locate libSDL, which should print out all files on your system that contain this name, for example, I get this on my system:

$ locate libSDL
/usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0
/usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0.11.4
/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
/usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0.2.0
/usr/lib/x86_64-linux-gnu/libSDL_image-1.2.so.0
/usr/lib/x86_64-linux-gnu/libSDL_image-1.2.so.0.8.4
$ 

But normally, with Linux projects, you would setup a cmake script that will automatically find the libraries and header files you need (by recursively looking for the best matches (and version numbers can be specified too) in the most likely folders). Something like this in a file called "CMakeLists.txt":

# Anounce your required cmake version:
cmake_minimum_required(VERSION 2.8)
# Name your project:
project(SimpleSDLTest)

# Ask cmake to locate the SDL library:
find_package(SDL REQUIRED)

# Add SDL's header file directory to the includes:
include_directories(${SDL_INCLUDE_DIR})

# Create an executable target from your source(s):
add_executable(simple_sdl_test simple_sdl_test.cpp)

# Tell cmake to link your target with the SDL libs:
target_link_libraries(simple_sdl_test ${SDL_LIBRARY})

Where the simple_sdl_test.cpp would be your source file, as so:

#include <SDL.h>
#include <cstdio>
#include <cstdlib>

SDL_Surface* g_pMainSurface = NULL;
SDL_Event g_Event;

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    g_pMainSurface = SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT);

    return 0;
}

(notice the SDL.h instead of SDL/SDL.h because you have specified, through cmake, the include directory already, and notice the cstdio and cstdlib because this is C++, not C)

Once you have the CMakeLists.txt and simple_sdl_test.cpp files in the same directory, you can go to that directory (in the terminal) and use the following procedure:

/path/to/test$ mkdir build
/path/to/test$ cd build
/path/to/test/build$ cmake ..
/path/to/test/build$ make
/path/to/test/build$ ./simple_sdl_test

Which create a build directory, configures the build by invoking cmake on the source directory, builds the project with "make", and then runs the program.

Of course, you have to have libsdl-dev packages installed (probably libsdl1.2-dev) via your software center or through an "apt-get" command.

Using a configuration tool like cmake is really the name of the game in Linux, because the location of libraries and headers are predictable enough (yet vary slightly from case to case) that an automatic system can find them easily, and then, you don't have to worry about where they are located, and your project can be built on virtually any Linux distribution (and usually works on Windows too, for both MinGW and MSVC!.. but it can get a bit tricky with finding libraries there).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.