Starting with C


    This guide is going to help you start coding your C programs at SourceLair and make the most out of SourceLair's capabilities.

    This guide assumes that you already have a SourceLair account .


    In order to start coding in C all you have to do is start a Generic project. Generic projects provide you with a fully featured Linux terminal and pre-installed the gcc compiler.

    If you need some help in starting a Generic project you can read this help article.

    Code your first "Hello World"

    Firstly, create a new file hello.c in your file explorer.


    Secondly, write the relevant code in your editor.


    Run Files

    In order to run your file you press "Run File" button, after saving your last changes.

    Run Hello

    If you desire more advanced options to compile and run your C files, you can compile and execute them in your terminal, just like you would do locally with gcc in your terminal.

    Installing libraries

    You have to install a package manager in order to install your favorite libraries. We propose Linuxbrew. You can install Linuxbrew if you paste at your terminal prompt:

    $ ruby -e "$(wget -O- https://raw.github.com/Homebrew/linuxbrew/go/install)"
    $ echo -e 'export PATH="$HOME/.linuxbrew/bin:$PATH"\nexport MANPATH="$HOME/.linuxb
    rew/share/man:$MANPATH"\nexport INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"' > ~/.bashrc && source ~/.bashrc
    $ brew update
    $ brew doctor

    and you are ready!

    You can find more info about Linuxbrew here. Of course you can also use other package managers but we propose Linuxbrew since it does not require root.

    Using Git Submodules

    For source files that are not included in any package manager you can use git.

    With Git, you can download libraries and keep the version that goes with your project. If you're curious now on how you can have nested Git projects, then you should look into Git Submodules.

    Git submodules allow you to add a certain version of a Git repository inside your repository. Just follow the steps below:

    1. Create a new project in SourceLair
    2. Open the terminal
    3. Type:
      $ git submodule add -b dev-branch git@github.com:Lirabry/Awesome lib/awesome
      to add a library named Awesome from Github, at the branch dev-branch in lib/awesome subdirectory.

    Then, you can easily update all your submodules by typing:

    $ git submodule foreach git pull

    Dynamic linking

    Dynamic linking allows you to include libraries that lie at your computer at runtime. One of the most common libraries, i.e. is _math.h_. In order to include math.h in your program, you'll need to dynamically link it.

    Including existing dynamic libraries

    In order to include an existing dynamic library, such as _math.h_, you just need to compile your program using -lm flags.

    As an example, let's say you have a simple file called _sqrt.c_ with the following code:

    #include <math.h>
    #include <stdio.h>
    int main(void)
        double x = 4.0, result;
        result = sqrt(x);
        printf("The square root of %lf is %lf\n", x, result);
        return 0;

    All you need to compile and run it using dynamic linking is:

    gcc sqrt.c -o sqrt -lm && ./sqrt

    Creating your own dynamic library

    Apart from using standard dynamic libraries, like _math.h_, you can create your own.

    Creating an object file

    First, you'll need to compile an object file from your library, let's say that your library is called _fun.c_:

    gcc -c -Wall -Werror -fpic fun.c

    Creating a shared library

    Making your object file a shared library (it should start with lib and end with .so)

    gcc -shared -o libfun.so fun.o

    Compile your file

    Now you can dynamically link _fun_library at compile time. Let's call your main file __main.c__

    gcc -L. -Wall -o test main.c -lfun -lm

    Adding /mnt/project to the dynamic library search path

    Trying to run your file now will raise an exception. This is because the linker does not know where to search for the dynamic library you just created. All we need to do now is add the directory of the dynamic library we just created - assuming it is _/mnt/project_ - to the dynamic library search path. We do so using the _LD\_LIBRARY\_PATH_environment variable, before running the executable.

    LD_LIBRARY_PATH=/mnt/project:$LD_LIBRARY_PATH ./test

    Statically exporting LD_LIBRARY_PATH

    In order to avoid defining LD_LIBRARY_PATH every time you want to run your executable, you could just export it statically for every Terminal session. To do so, just paste the following command in your Terminal.

    echo "export LD_LIBRARY_PATH=/mnt/project:$LD_LIBRARY_PATH" >> ~/.bashrc

    Tips to code faster

    SourceLair provides you with some great features in order to help you develop faster your website being focused on the creative part of programming.

    Command Palette

    Command Palette is a free text search tool which helps you use most of SourceLair's features really fast. You can open it by pressing Ctrl/Cmd + Shift + P.


    Quickopen is a fast and time saving way to navigate through your filesystem in order to find and open any file you want using your keyboard.

    There are 2 ways to open the Quickopen prompt:

    • using its shortcut Ctrl/Cmd + Shift + O
    • finding the Quickopen: Open a file command from Command Palette (Ctrl/Cmd + Shift + P)

    Find in files

    Find in files is a simple and easy way to search for something in your entire project at once, with quick file navigation from the results. There are 2 ways to trigger this feature:

    • finding the Project:Find in files command from Command Palette
    • using its shortcut Ctrl/Cmd + Shift + F