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.cpp in your file explorer. Create-file

    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 Button

    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 and g++ in your terminal.

    Installing libraries

    In order to install your favorite libraries, you have to install a package manger. 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 Linux Brew since it does not require root.

    In case you can't install a library from Linuxbrew, we would be more than happy to help you personally sending us an email at support@sourcelair.com.

    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

    Input from User

    Include your input in your code by adding in your code (e.g. "age" as input):

    int age;         /* Declare variable */
    cin >> age;      /* Use it as an input */


    C++11 is supported, since you can pass your own arguments to compile C++ files. To do so, you can use the Linux Terminal below your editor and type: `

    g++ -std=c++11 your_file.cpp -o your_program

    given that you have a file named your_file.cpp. Then, you can run your file using ./your_program. The "Run File" button does not support such command line arguments, since it is meant to be as simple as possible.

    If you would like to have more complex builds, you can either use "make" linux command, or create a bash script containing any commands you want to use for compiling and running your program, i.e. the commands above. In such case, you can use the "Run File" button on the bash script and execute it without using the terminal.

    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.cpp_ 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.cpp -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.cpp_:

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

    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.cpp__

    gcc -L. -Wall -o test main.cpp -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