1
votes

I am using Ubuntu 14.04LTS. I have installed the SDL2 libraries both by compiling from source (method1 https://askubuntu.com/questions/344512/what-is-the-general-procedure-to-install-development-libraries-in-ubuntu) and using sudo apt-get install libsdl2-dev. As I understand, the former installed the libraries and headers in /usr/local/(lib and include), while the latter installs them system wide in /usr/(lib and include).

When I tried to compile a simple code to test the functionality:

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

int main(int argc, char* argv[]) {SDL_Window *window;                       
// Declare a pointer

SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

// Create an application window with the following settings:
window = SDL_CreateWindow(
    "An SDL2 window",                  // window title
    SDL_WINDOWPOS_UNDEFINED,           // initial x position
    SDL_WINDOWPOS_UNDEFINED,           // initial y position
    640,                               // width, in pixels
    480,                               // height, in pixels
    SDL_WINDOW_OPENGL                  // flags - see below
);

// Check that the window was successfully created
if (window == NULL) {
    // In the case that the window could not be made...
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
}

// The window is open: could enter program loop here (see SDL_PollEvent())

SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

// Close and destroy the window
SDL_DestroyWindow(window);

// Clean up
SDL_Quit();
return 0;

using: g++ sdl_test.cpp -o sdlout

the compiler outputs:

sdltest.cpp:2:17: fatal error: SDL.h: No such file or directory
#include <SDL.h>
                ^
compilation terminated.

if I change to #include <SDL2/SDL.h> I get the following error:

/tmp/cc05JSKn.o: In function `main':
sdltest.cpp:(.text+0x15): undefined reference to `SDL_Init'
sdltest.cpp:(.text+0x3a): undefined reference to `SDL_CreateWindow'
sdltest.cpp:(.text+0x4a): undefined reference to `SDL_GetError'
sdltest.cpp:(.text+0x6d): undefined reference to `SDL_Delay'
sdltest.cpp:(.text+0x79): undefined reference to `SDL_DestroyWindow'
sdltest.cpp:(.text+0x7e): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

Which are the basic functions, so I assume that the shared object libraries are not linked correctly.

I also tried: g++ -Wall sdltest.cpp -o outsdl -I /usr/local/include -L /usr/local/lib to specify the paths, but again I get:

sdltest.cpp:2:17: fatal error: SDL.h: No such file or directory
#include <SDL.h>
                ^
compilation terminated.

The only command that worked and successfully compiled, is when using pkg-config g++ sdltest.cpp -o outsdl $(pkg-config --cflags --libs sdl2)

Therefore, I have the following questions:

1) Why is pkg-config necessary and how do compilation and linking flags work?

2) Is it possible to do something else in order to make the compilation command simpler?

3) (if not explained previously) What is the difference between pkg-config and using -I and -L which do not work?

4) what does $(...) actually do in the command line and is it completely the same as `...` ?

Thank you.

1

1 Answers

2
votes

The pkg-config command is a more-or-less cross-platform or cross-distro way to provide the correct flags to your compiler to allow it to find header and library files. That way, your system can store files in different locations and everyone can use the same commands to compile your code. It also helps resolve any special requirements of the library you're trying to use.

Using $() is the same as using backticks, so you can execute what is inside the parentheses in order to see what extra arguments are being passed to your compiler to make it work. Here is what I get on my machine when I run pkg-config --cflags --libs sdl2:

-D_REENTRANT -I/usr/include/SDL2 -lSDL2

The reason you're getting SDL.h: No such file or directory is because pkg-config adds -I/usr/include/SDL2 to the include search paths so you can include SDL.h in your code (without the SDL2 subdirectory).

The reason you get undefined reference errors is because you don't have -lSDL2 (which tells the compiler to link libSDL2).