I was messing around on VEXcode and I found that you can add new files and under that, I found “header file” and “source file”. I was just wondering the difference between the two types of files and some example code of what you would put in each file. Sorry if this is not descriptive because I know what vague forum questions are hard to deal with.
Header files, using .h and .hpp as extensions are for containing function declarations (void function ();), includes (#include "file.cpp") , macros, template codes etc. that are shared between multiple source files. For example, a common use for header files is to include parts of your library (ex. movement functions, arm control function, global variables etc) , while the source file is used to run and define those functions. Source files (.c, .cpp) are for function definitions ( void function ( ) { //code } ), and implementation of your functions ( function(); )
Headers are keys to access functionality, sources implement the functionality.
PS. They don’t have any technical difference (as in, header files and do everything source files can do and vice-versa), but this is the conventional usage.
Header Files are where you define anything thing that needs global access (things like your motors/certain values/etc), and you must put your method headers in at least one header file but it is recommended that you organize them with respect to the part of the robot that is using that method. I recommend that you look up what a method header is if this terminology sounds unclear. Your source files contain all method definitions. You also need to initiate the things in the global access header file (which you would create). Header files makes it much easier to organize a project instead of having everything in one giant source file.
So for example in configure.h I would put in
—————————————— #include “vex.h”
using namespace vex;
brain Brain;
controller Controller =
motor front = motor(PORT1);
——————————————
And then in main.cpp I could do like
—————————————— #include “vex.h” #include “configuration.h”
using namespace vex;
int main()
{
while(true)
{
if(controller.ButtonR1.pressing)
{
Front.spin(directionType::fwd, 100, velocityUnits::pct);
}
}
}
——————————————
Or am I doing it the wrong way around? Where the functions go in the .h file and the configuration goes in the .cpp file
This is technically not quite the conventional usage, but you seem to get the idea.
Make sure to get your spelling correct though, and make sure to use a header guard.
There is a fair bit more to header files than meets the eye, and I don’t think a single forum post could ever do the topic justice. The website I linked does cover all this though, with lots of sample code.