MRC/Tutorials/Setting up and building your project

From Control Systems Technology Group
Jump to navigation Jump to search

Of course, we not only want to use software during this course, but we want to create some! Let's start off with a simple example project. Go inside the ~/mrc directory, and create a new folder with the name my_project:

cd ~/mrc mkdir my_project

Often, the code files are not put directly in the root of a folder, but in a directory called *src*. This stands for "source", and is called this way because the files in there are the source of the compilation process, and are converted into binaries (files that are no longer human-readable, but are understandable for the machine). So, let's go. Remember that when using cd (and many other commands in linux) you can use tab-completion to type quicker, i.e, try:

cd m<<< now push the TAB key >>>

You will see that the terminal fills out the rest, because my_project is the only directory in the current directory that starts with an m. Ok, create the src directory, and go inside:

mkdir src

cd src

Finally, let's do some programming! You should already be familiar with the C++ language, so you know how to create a basic C++ program. Let us do it now.

Open your favourite editor to create a file called example.cpp (e.g. `gedit example.cpp`) and put some code inside:

#include <iostream>

int main()

{

std::cout << "Hello world!" << std::endl;

return 0;

}

Once you have saved your C++ program it can be compiled from a terminal using:

g++ -o example example.cpp

This will create a file called example which you can run. To run the file use:

./example

You computer should now greet the world. Now, actually, our nice src is already not as clean as it should be. Check with `ls`. It should contain only source files, not binaries! No worries, let's go one directory up:

cd ..

In case you are wondering `..` means one directory up. Create a bin folder for our libraries:

mkdir bin

And run compilation as follows:

g++ -o bin/example src/example.cpp

Now our binary is create in the bin directory, while the source is in src: nicely seperated! You can run the program using:

bin/example

By the way, just remove the example binary we created 'wrongly' in the src directory using:

rm src/example

And we are good to go.