Search code examples
cgccsdlsdl-image

How can I link with gcc and SDL_image library?


I have the following script:

gcc -I /Library/Frameworks/SDL2.framework/Headers \
    -F /Library/Frameworks/ -framework SDL2 test.c

that successfully compiles the following source code:

#include <SDL.h>
//#include "SDL_image.h"

int init();
int loadMedia();
void close();

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

int init()
{
    int success = 1;

    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    }
}

int main(int argc, char* args[])
{
    return 0;
}

My question is how do I compile this with the added library SDL_image? Would it be something like this?

gcc -I /Library/Frameworks/SDL2.framework/Headers \
    -I /Library/Frameworks/SDL_image.framework/Headers \
    -F /Library/Frameworks/ -framework SDL2 test.c

When I try this I get the following error:

In file included from test.c:2:
/Library/Frameworks/SDL_image.framework/Headers/SDL_image.h:27:10:      fatal error: 
      'SDL/SDL.h' file not found 
`#include <SDL/SDL.h>`

Solution

  • You could do:

    gcc -L/libs/path -I/headers/path -lSDL2 -lSDL2_image

    In this case, use #include "..." for your SDL2 headers inclusion.

    If you installed SDL2 with a package manager, the previous command is reduced to:

    gcc -lSDL2 -lSDL2_image

    since the path of SDL2 directory is then known to the compiler. In that case you should use #include <...>.

    You should consider writing a Makefile for your project. It would become easier to compile as your project grows.