What is a Source File?
A source file contains the actual code that runs. This is where functions, class methods, and variables are defined. If a header file says βthis thing exists,β a source file says βthis is how it works.β
Understanding the File Extension:
| C Source File | C++ Source File | |
|---|---|---|
| File Extension: | .c |
Note: DHRA suggests using |
| Use-Case | If your code is strictly coded in the C Programming Language. | If your code includes: classes, operator overloads, templates, namespaces, or other C++ Features. |
Where Source Files Go
Put source files inside the src/ (source) folder of your PROS project.
If your project starts getting larger, make subfolders inside src/ to stay organized.
Example Layout
include/ 2131H/ Drive.hpp Competition/ RobotConfig.hppsrc/ 2131H/ Drive.cpp Competition/ RobotConfig.cpp main.cpp
This keeps declarations in include/ and definitions in src/. Trying to follow the same names is advised.
Creating a Source File
- Create a new file in
src/with a clear name. - Use the same naming style across the whole project.
snake_caseandcamelCaseare both common. Pick one and stay consistent. - If the file implements C++ code, give it the
.cppextension.
Typical Workflow
- Create a header file in
include/ - Create the matching source file in
src/ - Put declarations in the header (Add multiple inclusion guards)
- Put definitions in the source file (No need for inclusion guard)
- Include the header where you want to use that code
Basic Example
Below is a simple header/source pair.
Header File
#pragma oncevoid driveForward(float motorDegrees, float maxVelocity = 600.0);
Source File
#include "2131H/drive.hpp"#include "Competition/RobotConfig.hpp" // For left and right motor declarations// NOTICE: Return Type and Parameters match declaration.void driveForward(float motorDegrees, float maxVelocity){ leftMotor.move_relative(motorDegrees, maxVelocity); rightMotor.move_relative(motorDegrees, maxVelocity); return; // Return type is void so we return nothing}
Using the Source File
The .cpp file is not included directly. The header is included instead.
#include "main.h"#include "2131H/Drive.hpp"void opcontrol(){ driveForward(90); // The maxVelocity will defualt to 600.0 driveForward(90, 300.0); // maxVelocity explicitly set to 300.0}
Why Split Code Into Source Files?
Putting everything in main.cpp works at first. It stops working well once your code grows.
Separate source files help you:
- Keep files shorter.
- Group related code together.
- Reuse code in multiple places.
- Debug faster.
- Make team projects less chaotic.
Common Mistakes
1. Including a Source File
- Never write:
#include "Drive.cpp". - Include the header instead:
#include "2131H/Drive.hpp".
2. Putting Everything in Headers
- Do not place every function definition in a header unless you have a good reason.
3. Forgetting to Match Names
Try to keep related files paired clearly:
drive.hppwithdrive.cppintake.hppwithintake.cppodom.hppwithodom.cpp
This makes large projects much easier to navigate.
Hint: 2131Hβs 2024-25 High Stakes Code is decently organized. You can find the repository on GitHub, or by clicking this link: https://github.com/DHRA-2131/2024-25-2131H/tree/main/Rewrite%203/include/2131H/Utilities.
Not an AD: This article was pretty much written with the inbuilt AI. If this upsets you and you think you can do better, please contribute. We would be glad to have you on board!