4

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>`
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rubberduccky
  • 107
  • 3
  • 11
  • Funny, your "script" looks exactly like C source code. Or do you mean the command hidden in your text? – too honest for this site Sep 08 '15 at 23:44
  • 1
    I doubt that you actually want to link gcc with some unrelated library. However, if you want to link your application with the library and want to know how to tell gcc to do that: (this is for after all the source files cleanly compile) `gcc -o -L -l (lower case 'L') short library name. Note: a short library name would be when the full library name is 'libmath.so' then the short library name would be 'math' to get short library name:. drop the leading 'lib' and everything from '.' onward – user3629249 Sep 09 '15 at 00:55
  • Thank you for the quick responses. To Olaf: I am talking about the command hidden in my text. That command is in a script file. I just find it easier to execute this file once instead of typing it all out on the command line. – Rubberduccky Sep 09 '15 at 01:57
  • To user3629249: I try the following command: "gcc test.c -o a.out -L /Library/Frameworks -l SDL2" and "gcc test.c -o a.out -L /Library/Frameworks -l SDL2.framework" and does not work. Maybe I am doing this wrong? Thank you for the quick response. – Rubberduccky Sep 09 '15 at 01:59
  • Why are you explicitly adding a framework header directory to the list of includes? – Jason Sep 09 '15 at 17:38

1 Answers1

0

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.

blakelead
  • 1,780
  • 2
  • 17
  • 28
  • Thanks for the quick reply. For the -L option would my command be "gcc -L/Library -l/Library/Frameworks/SDL2.framework/Headers -lSDL2 test.c -o a.out"? I tried both of these commands with and "SDL.h". Now I can't get gcc to compile with just the SDL include. I think I need to specify my headers with the "-H" option and the frameworks with the "-F" option as well as passing "-framework SDL2" to gcc. Unless I am using the -L and -l options wrong. I am not sure if this is important or not but I am programming on a Mac. – Rubberduccky Sep 10 '15 at 22:54
  • 1
    I'm sorry I assumed you were working on Linux.On a Mac you have to put SDL2main.m and SDL2main.h in your project directory and do something like `gcc -I/Library/Frameworks/SDL2.framework/Headers test.c -o test SDL2main.m -framework SDL2`. And of course the same thing goes for SDL2_image. – blakelead Sep 10 '15 at 23:51
  • Thank you for responding. I tried your suggestion and I am confused because I can't find a SDL2main.m. The only files I see are header files such as SDL.h and SDL_main.h. Am I not looking in the right directories? I found these files under the framework found in /Library/Frameworks/SDL2.framework/Headers. I was messing around with the options and found a certain combination that got me a little further but now there's a different error. The combination I tried is: – Rubberduccky Sep 11 '15 at 00:37
  • 1
    "gcc -I/Library/Frameworks/SDL2.framework/Headers -I/Library/Frameworks/SDL_image.framework/Headers -F/Library/Frameworks -framework SDL2 -framework SDL_image test.c -o test". Now I get an error saying: "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 " This error is generated from the file SDL_image.h. When I removed the SDL in every line that said "SDL/blah.h" – Rubberduccky Sep 11 '15 at 00:39
  • the compiler compiles the files but now when I run the resulting executable I get a weird error: "dyld: Library not loaded: @rpath/SDL.framework/Versions/A/SDL Referenced from: /Library/Frameworks/SDL_image.framework/Versions/A/SDL_image Reason: image not found". Any ideas? – Rubberduccky Sep 11 '15 at 00:41
  • When using SDL2 you should also use SDL2_image, not SDL_image. I think you are mixing SDL libs with SDL2 libs. – blakelead Sep 11 '15 at 00:41
  • Thank you so much! As it turns out I was using SDL_image libraries instead of SDL2_image libraries. – Rubberduccky Sep 11 '15 at 00:46
  • Ok then I hope fixing that will resolve your issues! – blakelead Sep 11 '15 at 00:47