How to compile an example SDL program written in C?

末鹿安然 提交于 2019-12-07 04:58:37

问题


I'm getting started with SDL and C programming. I have experience with other programming languages, but linking/compiling libraries in C is new to me. I am using Mac 10.8 and have installed latest stable 2.0 using the instructions in the read me (./configure; make; make install). Here is the sample code that I am trying to compile:

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

int main(void)
{
  if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
    fprintf(stderr, "\nUnable to initialize SDL:  %s\n", SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);

  return 0;
}

When I try to compile my script using gcc example.c, I get an error:

example.c:3:17: error: SDL.h: No such file or directory
example.c: In function ‘main’:
example.c:7: error: ‘SDL_INIT_VIDEO’ undeclared (first use in this function)
example.c:7: error: (Each undeclared identifier is reported only once
example.c:7: error: for each function it appears in.)
example.c:7: error: ‘SDL_INIT_TIMER’ undeclared (first use in this function)
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:11: error: ‘SDL_Quit’ undeclared (first use in this function)

I tried searching the wiki, and tutorials, and any kind of documentation that I could find, but I could not find any example anywhere that showed how to properly compile a C program that uses SDL.

What do I need to do to compile this program?


回答1:


A general hint for C beginners: read error logs top-down: often fixing first error will resolve all other. In your case first error is:

example.c:3:17: error: SDL.h: No such file or directory

As others have said, you need to instruct gcc where to find SDL.h. You can do this by providing -I option.

To check where SDL.h is installed by default I would issue

./configure --help

in the directory where you did build libsdl. Then look for --prefix, under Linux default prefix is often /usr/local. To compile your example I would issue (on Linux):

gcc example.c -I/usr/local/include

But the above command compiles and links the code. After successful compilation, gcc would throw another bunch of errors, one of them being undefined reference.

To prevent that, full command line to build your example (on Linux at least) would be:

gcc example.c -I/usr/local/include -L/usr/local/lib -lSDL

Where:

  • -I points compiler to directory with SDL.h,
  • -L points linker to directory with libSDL.a (or libSDL.so),
  • -l instructs linker to link with library, in our case libSDL.a or libSDL.so. Note that the lib prefix and .a/.so suffix is missing.

Please note that I didn't check this instruction, even on Linux machine (on the other hand I have no access to Mac OS machine).

One more thing: by default binary with the compiled and linked example will be called a.out. To change that you can provide -o option to gcc.




回答2:


I found out you can use a tool called pkg-config to find out the compiler flags expected for a specific library.

$ pkg-config --cflags --libs sdl2
-D_THREAD_SAFE -I/usr/local/include/SDL2 -I/usr/X11R6/include -L/usr/local/lib -lSDL2

$ gcc example.c $(pkg-config --cflags --libs sdl2)

If you are using a Makefile, you need to prefix the command with shell:

all:
    gcc example.c $(shell pkg-config --cflags --libs sdl2)


来源:https://stackoverflow.com/questions/19366064/how-to-compile-an-example-sdl-program-written-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!