6

What is the general approach to installing c libraries on Linux?

In particular, I would like to install this library, with the objective to compile this:

#include <SDL_image.h> // it errs that it does not recognize this

int main()
{
}

What I have tried:

  • get a binary from here
  • put it in /etc/usr/clibs
  • added this path to /etc/ld.so.conf

=> FAILED. still failing to compile my file.

I installed the rpm from here

=> FAILED, still failing to compile

I tried building from source: Upon running configure, it said this:

checking for sdl-config... no
checking for SDL - version >= 1.2.10... no
*** The sdl-config script installed by SDL could not be found
*** If SDL was installed in PREFIX, make sure PREFIX/bin is in
*** your path, or set the SDL_CONFIG environment variable to the
*** full path to sdl-config.

I don't understand, it expects a script for installing whose presence there is the result of an installation...

I don't know what to try anymore... I've spent a lot of time trying to figure this out, so if someone could just give me a solution with do this and that, it would be great. An answer to the general question would be a great bonus.

Thanks.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
dandroid
  • 395
  • 1
  • 3
  • 12

3 Answers3

8

In most Linux distributions, there are prepared packages available with the SDL libraries. On Debian and Ubuntu, you can simply sudo apt-get install libsdl-image1.2-dev. In Red Hat, Fedora, and CentOS, you can do sudo yum install SDL_image-devel.

You can get the correct flags for the compiler using sdl-config. The configure script is the “automated” tool for discovering the correct flags, but it is pretty compilicated. If you haven't used C libraries before, it isn't terribly obvious. The -I flag adds a directory to the search path for #include directives. The -L flag adds a directory to the search path for libraries and -l tries to add a library to the program. Compiling C happens in two steps, compiling and linking. Compiling only looks at header files (.h files) and only cares about -I directives; it output object code (.o files). Linking only cares about -L and -l options and attempts to resolve the symbols in the object code. Typically, libraries live in /lib and /usr/lib and headers live in /usr/include. However, headers are often broken out into separate subdirectories and thus require more specific -I directives. Some programs started to include foo-config programs that included the proper directives to compile against the library. pkg-config is a generic version used by many libraries, especially ones related to GNOME.

This is all very different from other languages which typically a) just use sources for libraries (e.g., PERL, Python) or b) have an executable format that contains all the information needed for compilation (e.g., Java, C#).

apmasell
  • 7,033
  • 19
  • 28
  • I installed the packages as mentioned, an when i try to compile that simple example with just including the header it complains about it not existing. Is there any solution to this? Am i completely stupid or is the process of installing and using a c library extremely cumbersome? are there no automated tools for this? – dandroid Aug 25 '12 at 22:27
2

sdl-config would be present as the result of installing libsdl, which you must do before you install libsdl-image.

Why don't you install the packages provided with your distribution?

Alan Curry
  • 14,255
  • 3
  • 32
  • 33
  • i did follow your instructions and installed the package. however, when compiling i still get errors. it is not aware of the header file. see my reply above. – dandroid Aug 25 '12 at 22:32
  • From the story you've told so far, you made one attempt to install an rpm from a third-party source, and one attempt to compile a library yourself, both of which failed. Remnants of those failed attempts may be preventing things from working properly. I suggest you take a deep breath and rewrite your question from scratch, and include more details. Don't just say "I get errors". **show them!** – Alan Curry Aug 25 '12 at 22:40
  • i have cleared the remnants of the old packages and installed the proper ones. I have managed to compile my simple code and run it( see my answer below). (thanks for the help) – dandroid Aug 25 '12 at 22:52
1

Solution: my currently installed packages are:

libsdl1.2debian

libsdl-image1.2

libsdl1.2-dev

libsdl-image1.2-dev

i can compile and run the following code:

#include <SDL_image.h>
#include <math.h>
#include <stdio.h>

int main()
{
  int x = sqrt(4);
  SDL_Surface* s = IMG_Load("foo");
  printf("%d %d", x, s);
  return 0;
}

~

with the command:

gcc libTest.c `sdl-config --cflags` -Wall -O6 -g -O2   -lSDL_image

(I nicked it from somewhere, I don't really understand it)

dandroid
  • 395
  • 1
  • 3
  • 12