linking Files
In the following tutorial, we will look at how to link methods that belong in a different file. The advantage of having methods in different files is to simplify code and allow for code to easily be added to other projects simply by copying the file. While it is possible to link code via header files like in C / C++, RobotC also allows for linkage of code similar to how one would do it in Java. We will teach the later because the former is more complex and does not offer any advantages in RobotC.
To start off, create a new file called link.c. In that file copy the following text:
void Forward()
{
motor[port1] = 127;
motor[port2] = 127;
}
This method will allow the robot to drive forward indefinitely. Now since the method is created we need to link it to the main file so that it can be called during the autonomous period. Create another file called main.c and MAKE SURE MAIN.C AND LINK.C ARE IN THE SAME DIRECTORY. We we will house our "task main()" line to start the code. Copy the following text into the main.c file:
#include "link.c"
task main()
{
Forward();
}
The way that files are linked is via the #include statement at the top of the file. This line of code follows the template of #include "NAME_OF_FILE". Basically the include statement pastes all code that was written in the other file into the current one when the code is compiled. Ergo, your files are now linked!!!! Thus, when the code hits the line "Forward();", it will look for this method in the link.c file.
Thanks for reading,
Cheers!
To start off, create a new file called link.c. In that file copy the following text:
void Forward()
{
motor[port1] = 127;
motor[port2] = 127;
}
This method will allow the robot to drive forward indefinitely. Now since the method is created we need to link it to the main file so that it can be called during the autonomous period. Create another file called main.c and MAKE SURE MAIN.C AND LINK.C ARE IN THE SAME DIRECTORY. We we will house our "task main()" line to start the code. Copy the following text into the main.c file:
#include "link.c"
task main()
{
Forward();
}
The way that files are linked is via the #include statement at the top of the file. This line of code follows the template of #include "NAME_OF_FILE". Basically the include statement pastes all code that was written in the other file into the current one when the code is compiled. Ergo, your files are now linked!!!! Thus, when the code hits the line "Forward();", it will look for this method in the link.c file.
Thanks for reading,
Cheers!