Why? Isn’t this gross?
Some programmers consider header files to be annoying and over-complicated. For the most part they are right! Header Files are a relic of the C era, largely implemented due to memory constraints on 1970’s computers
- (Stack Overflow, Accessed Mar. 13, 2026).
Header files allow for use of ready-made functions. For example, the C++ Standard Libraries <cmath> include adds std::abs(x), so a user wouldn’t have to program their own implementation. Note: This was mostly stolen from here.
Header files can be split up into 5 types:
- C Standard Library - Largely outdated, using the C++ alternative is suggested.
- C++ Standard Library - Generally adds safety to the C standard library
- Other libraries (Boost, Eigen) - Provide extra functionalities. Eigen provides Matricies for example.
- PROS Robot Operating System (PROS) - Provides your interface with Vex
- User Defined Header Files - You make these :D
Common Standard Library Files
Some commonly used standard header files are:
<iostream>: Contains declarations for input and output operations using streams, such asstd::cout,std::cin, andstd::endl. Note: Probably the most used include in vex. Leaving<iostream>in your code can significantly increase binary size.<cmath>: Used to perform mathematical operations likestd::sqrt(),std::log2(),std::pow(), etc. Note: Believe this is part of C++ Library, not C like the name would imply.<vector>: Used to work with container class for dynamic arrays (vectors) functions likebegin(),end(). Note: C arrays are built into the language and are not included by a library.<chrono>: Provides a duration class and other useful time operations. Note: C++ Alternative to<ctime>.<string>: Provides thestd::stringclass and functions for string manipulation. Note: C++ alternative to<cstring>.<cstring>: Used to perform various functionalities related to string manipulation likestrlen(),strcmp(),strcpy(),size(), etc. Note: C Library.<cstdlib>: Declares functions involving memory allocation and system-related functions, such asmalloc(),exit(), andrand(). Note: C Library.<cerrno>: Used to perform error handling operations likeerrno(),strerror(),perror(), etc. Note: Believe this adds C Error handling.<ctime>: Used to perform functions related todate()andtime()likesetdate()andgetdate(). It is also used to modify the system date and get the CPU time respectively.
Example Implementation of Common Library Functions
#include <cmath> // Supplies math functions#include <cstdlib> // Supplies machine interface in C#include <cstring> // Supplies C string helper functions#include <iostream> // Supplies Input/Output for C++#include <string> // Supplies C++ string class and functions#include <vector> // Supplies Dynamically Sized Arrays (C++)int main(){ // Using <iostream>, prints text to terminal std::cout << "Hello, Geek!" << std::endl; // Using <cmath>, returns square root of 25 (5). // Note: using <cmath> over <math.h> will supply type safe alternatives double squareRoot = std::sqrt(25); // Returns 5.0 to the terminal std::cout << "Square root of 25: " << squareRoot << std::endl; // Using <cstdlib>, believe rand() has issues. Look into C++ alternatives. int randomNum = rand() % 100; // Random number between 0 and 99 std::cout << "Random number: " << randomNum << std::endl; // Returns random number to the terminal // Using <cstring> to edit strings char str1[20] = "Hello"; char str2[] = " World"; strcat(str1, str2); // Concatenates (Combines) two strings std::cout << "Concatenated string: " << str1 << std::endl; // Returns "Concatenated string: Hello World" to terminal // Using <vector>. Stores array of dynamic size (aka vector) vector<int> numbers = {1, 2, 3, 4, 5}; numbers.push_back(6); // Adds a 6th number to the variable std::cout << "Vector elements: "; for (int num : numbers) { std::cout << num << " "; } std::cout << endl; // Returns "1 2 3 4 5 6 " to the terminal // Using <string>. Much safer way of handling strings, slight overhead cost. std::string greeting = "Hello, "; std::string name = "Programmer"; std::string fullGreeting = greeting + name; // Simple Concatination std::cout << "Greeting message: " << fullGreeting << std::endl; // Returns "Greating message: Hello, Programmer" return 0; // Exit Main Function with Success code of 0.}
Creating Header Files
Creating header files is useful for organizing code but can be tricky. Consider looking for multiple explanations as this is a complicated topic.
Create a Directory For Custom Header Files
Create a Folder to separate custom headers from PROS, LVGL, and other installed libraries.
- Inside your PROS Project, navigate to
include/and create a new directory (mkdir <directory name>, or do it graphically by right clicking on theinclude/folder and selectingNew Folder). - Name this something correlated to your team:
<Team #>- Something Davis Related
<Team Name>$(Author Name)Lib
Create a Header File
Create a header file with an appropriate name. Follow a convention when naming files, camelCase and snake_case are common styles. Give the file the correct file extension: if the header file features any C++ features use .hpp otherwise, use .h. The newly created file will be completely empty.
New File by Right Clicking on Folder.
New File by using the Linux touch Command
Safe Guarding the Header File
What the Header File is Doing:
- A header file is a way declare to the rest of the code outside a source file that
myCustomFunction(),MyCustomClass, and/orMyVariableexists. It doesn’t define how the function or class works (This is what the source file is used for).
What #include Does:
#include <library>or#include "path"is one of many Pre-Processor Macros. This specific macro tells the compiler to go to a specified file and copy and paste the contents where the#includeline is. It doesn’t do anything more than that. There is no checks for what is being included or if it has been included.- This is a potential problem. Consider if you call the same
#includetwice in one file? You’ve just declared and then re-declared all the contents of that header file (this will cause a error).- You might assume this is simple enough to avoid by just not being dumb. However, consider you have a
include/math.hppfile that you use in bothsrc/main.cppandinclude/RobotConfig.hpp. If you then includeinclude/RobotConfig.hpptosrc/main.cppyou will have accidentally called#include "math.hpp"in the same file twice! - This is called a multiple inclusion.
- You might assume this is simple enough to avoid by just not being dumb. However, consider you have a
Solving Multiple Inclusion
To get around multiple inclusion, something in the code needs to mark if a file has been used already. There are two approaches to this.
- (Suggested) Using
#pragma oncebefore any code.
#pragma once // All code below this line is exempt from multiple inclusion problems.
- Using
#ifndef <symbol>.
#ifndef ROBOT_CONFIG_HEADER // If symbol is not defined#define ROBOT_CONFIG_HEADER // Define symbol and code// Code here is exempt from multiple inclusion problems.#endif // End of if statement. Note: next time this file is included, the // symbol will be defined and the code will not be included again.
Syntax for Declaration
Note: These will make more sense after finishing the tutorial for variables, functions, and classes.
Functions:
// NOTICE: there is no definition to this function ({/* code */} is missing). // Commentation for this function and what it does.[returnType] FunctionName([arg1Type] arg1, [arg2Type] arg2, ...);
Variables:
// NOTICE: the variable is not being defined. There is no = or constructor call.extern [varType] variableName;
Classes:
class ClassName : public Inheritence { private: int var1 = 0; // Default initialization 0 public: const [var2Type] var2; // Will be declared in constructor public: // NOTICE: The constructor is not being defined // Commentation for what this function does and what the arguments mean ClassName([arg1Type] arg1, [arg2Type] arg2, ...);};
Not an AD: This article is confusing. It should be reworked to be more comprehensive. I did my best please don’t hate . Please contribute and make this better!