0. C and CPP Programming Basics - RIT-Launch-Initiative/Liftoff-Project GitHub Wiki

Intro

Most of our software is written in C and C++ for several reasons. Higher level languages like Java and Python are nice and convenient due to the automatic memory management and libraries they offer. The downside is that there is processing overhead compared to lower level languages like C and C++. This is especially important when dealing with hardware constrained systems that have less processing power and memory. For slightly less hardware constrained systems such as our ground station (which we use some Python for), it allows us to interact better with the operating system through system calls (a topic which will not be covered). If you truly feel comfortable writing C and C++ code, and have an understanding of build automation you may skip this part.

Current Software Stack

Ground Software

  • C
  • C++
  • Python

Flight Software

  • C
  • C++

Exercises (Estimated Time: 30 minutes)

Part One

To start, go to the IntroTopics/Include folder and read the top comment I left in IntroToC.h After doing so, you will go to IntroToC.c in the Source folder and implement the functions based on the specifications given to you in the header file. I will do my best to write comments that guide you well along the way. It is best to follow the source file from top to bottom, although you may need to reference code found in main.

Once you are finished, run the following in your terminal once you have setup your compiler. Run this inside your source directory gcc -o intro IntroToC.c -I..

  • gcc is the compiler used
  • Every letter following a dash is known as a flag. Flags modify the operation of a command you enter into the terminal.
  • The -o flag specifies the name of the output file which will be "intro" in this case
  • IntroToC.c is the file you are compiling
  • The -I flag handles including other dependency files for compiling your code. ".." specifies one folder up from Source so your IntroTopics folder. By doing this, your IntroToC.c file will know where to look for Include/IntroToC.h from.

If you managed to not get any compile errors, congratulations and you may continue! If not, don't worry. There's no expectation of getting software right on the first try. Just fix any errors the compiler is complaining about

To run your program you can do ./intro since that was specified by the -o flag

You may notice that all your print statements are in one line. This is because no newline character was specified in the print statements. This would be a "\n" and is considered one character not two. The backslash is used as an escape to treat the following character as special. You should add a \n to the end of your strings until you feel comfortable with how to write print statements.

Part Two

We will now be introducing C++ and Object Oriented Programming (OOP for short). OOP is one of several programming paradigms commonly used in industry and is useful for designing modular, reusable code. Although C++ is derived from C, they are still largely different and C++ offers a breadth of features such as OOP. In this portion, you will be designing a very basic calculator while also learning about good, basic design practices such as dependency injection and polymorphism. You will see many of these practices spread across our software.

Header files and the main code have been provided for you. I would be worried if you couldn't find them. Observe how they are implemented. Just implement the functions for Add/Sub/Mul/DivCalculation.cpp in Source/Calculator/

Once you are finished, you may have realized there's quite a bit of files to compile. Lucky for you, there are these things called a Makefile which is a way of automating the build process. I have already written one for you and if you have setup the make command, you can just run make in the same directory as the Makefile. Understanding the basics of Makefiles is important such as defining recipes and variables. There are several Makefiles for you to reference including IntroTopics/Makefile Memory/Makefile and EmbeddedSystems/Makefile. The EmbeddedSystems Makefile is the most complex because it is generated through a tool, but can show more advanced things you can do with Makefiles. More modern practices include using tools such as CMake to create a more readable file that generates Makefiles for you. More on this later.

Additional Resources

Command Line Guide

C Quick Guide

C++ Quick Guide

Object Oriented Programming Guide

GNU Make Utility Guide