Working With Other Libraries

Linux Distribution (obviously)
A text editor :
Vim, Emacs, gedit, kate, atom
A Compiler :
gcc or clang
A Shell:
bash, xterm, sh

My setup for this tutorial:
Debian GNU/Linux 9.4
Atom (Text editor)
gcc version 6.3.0-18+deb9u1 (Debian’s Release version for Debian 9.1)

For this example i am gonna teach you how to install libraries to use in your
projects. For this example i will be installing a new version of SDL2. If you havent
yet, open up your shell, and download mercurial. If you are on debian, im sure
you can install it with :

      clim@clim:~/Desktop$ sudo apt-get install mercurial

Next, i found the website on google, and found their mercurial source.

      clim@clim:~/Desktop$ mkdir sdl2
      clim@clim:~/Desktop$ cd sdl2
      clim@clim:~/Desktop/sdl2$ hg clone
      clim@clim:~/Desktop/sdl2$ cd SDL

Its always good practice to read the README and INSTALL files of the project.
Its a typical install, the process is call the configure script, make it then
make install. this is quite easy.


This creates a makefile generated from an AutoMake program. It is part of the
autotools that alot of *nix programmers tend to use to make building things easier.
It isn’t one of my favorite build-tools, but its simple for the end user.

    clim@clim:~/Desktop/sdl2/SDL$ make

Next is the make command, this is a program that parses a Makefile (Thats the
filename) and then builds accordingly. I will go over how to create your own
makefiles in another lesson, but to touch on it, there are targets you can make,
these targets could be an executable name, an object file name, or even a command
you will write a custom script for. You will see a bunch of lines that start with
CC. It is showing you the objects it is currently compiling.The next command
I use will be a custom command that doesnt build anything.

  clim@clim:~/Desktop/sdl2/SDL$ sudo make install

This command will install the newly compiled objects into your Linux
Distribution. Notice how i used the command sudo for that specific make target?
This is because it is accessing your hard drive and manipulating files. Since
your file system is not accessible for writing, for security reasons. Once you
type that in, you need to enter your password (hopefully your system has sudo on
it, some do not, if not execute this in su mode).

Now that it installed you can go into your project and include them. For this
tutorial, i will prove it is installed by looking at the preprocessed code.


    int main(int argc, char *argv[]){

    	printf("Hello World!\n");

    	return 0;

I enter that in main.c.

    clim@clim:~/Desktop/sdl2/sdltest$ cpp main.c > main.i
    clim@clim:~/Desktop/sdl2/sdltest$ gedit main.i

Ah! And perfect. The first page of the code looks like this:

      # 1 "main.c"
      # 1 ""
      # 1 ""
      # 31 ""
      # 1 "/usr/include/stdc-predef.h" 1 3 4
      # 32 "" 2
      # 1 "main.c"
      # 1 "/usr/local/include/SDL2/SDL.h" 1 3
      # 32 "/usr/local/include/SDL2/SDL.h" 3
      # 1 "/usr/local/include/SDL2/SDL_main.h" 1 3
      # 25 "/usr/local/include/SDL2/SDL_main.h" 3
      # 1 "/usr/local/include/SDL2/SDL_stdinc.h" 1 3
      # 31 "/usr/local/include/SDL2/SDL_stdinc.h" 3
      # 1 "/usr/local/include/SDL2/SDL_config.h" 1 3
      # 33 "/usr/local/include/SDL2/SDL_config.h" 3
      # 1 "/usr/local/include/SDL2/SDL_platform.h" 1 3
      # 179 "/usr/local/include/SDL2/SDL_platform.h" 3
      # 1 "/usr/local/include/SDL2/begin_code.h" 1 3
      # 180 "/usr/local/include/SDL2/SDL_platform.h" 2 3
      # 188 "/usr/local/include/SDL2/SDL_platform.h" 3

      # 188 "/usr/local/include/SDL2/SDL_platform.h" 3
      extern __attribute__ ((visibility("default"))) const char * SDL_GetPlatform (void);

Since the code outputted that format, it tells you that SDL2 is located within
your local includes files, and you can now properly use the code by linking it
using the -lSDL2 compile flag of in gcc

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s