Multiple Files

Since the default code is made of multiple files, it requires a specific linking script, correct? I am a programmer that can make simple VB, C, C++, and Java programs work but I don’t understand much about coding theory. Can anyone explain what the benefit of multiple files is?

Along that same line, the linker provides a way for a method in one file call a method in another file, right? How are these linking scripts made?

Any answer would be greatly appreciated.
Thanks in advance,

The main benefit of multiple files is organization!
Technically… every function and call you make slows your program down since current commands are put on hold to the stack and the new function is addressed, things are put to the stack and then taken care of in order.

The multiple files also allow one to split up code for different people to work on as well as allowing you or somebody else to change code rapidly if you can go to a specific file instead of one monster file that you must use search to find areas in.

I cant tell you exactly how linker scripts are done… I know the linker is run and thats about where my knowledge ends.
The linker does have all functions and callings done so they can be called from different files.

Linkers do just as they are named, they link together different files and create an file able to be executed.
They take libraries that you included with the linker script (or the command line), and connect the code so that calls that you make to functions outside of each file actually call the right function and do not call nothing.
They also take each of the object files that was created from the compiler, and connects them in a similar fashion to what was done for the libraries.
And like ‘dm0ney’ said, more files = more organization, and easier to spread the code to multiple people.
And you do not need a linker script for most linkers… you can generally just place the files you want to link into the command line… I believe that is what the Microchip IDE does… although I would have to check when I have that available to me.

In a word organization…this was mentioned before, but I’ll expand a bit…

There are 14185 lines written for the Wildstang robot (including comments, headers and some of the default ifi provided files) If we were to put this into a single file, it would quickly become unmanageable (not to mention uncompilable due to mcc18 limitations).

To make this more manageable, we have broken this into 33 C files and 26 H files. Each file contains functions and/or definitions for a specific portion of our robot.

For example, ws_drive_input.c handles joystick input as it relates to the drive train (i.e. left speed, right speed, shifter positions) as well as acceleration.

ws_lift.c handles joystick input as it relates to the lift (i.e. height, tilt, top/bottom spear positions).

As you can see, we have a naming convention for our files such that any custom code is prepended with a ws_. The names are also well named such that as long as you know what it is you’re trying to accomplish, you should be able to determine which file to modify. For example, if you want to use tank steering instead of single stick, you could easilly tell you wouldn’t want to modify ws_lift.c, but rather ws_drive_input.c.

The modularization of your code (i.e. multiple files) also allows more programmers to be involved. With this type of system, one person can go off and write lift code while another works on drive code. Since these modules are completely independent, there is no risk of duplicating effort.

All in all, making your programs modular will make your code more readable and maintainable.

In the case of MCC18, the linker script just defines memory blocks. Which files to link are passed on the command line.