Lab Exercise 9: Multi-File Projects


The purpose of this lab is to help you understand how to create projects that contain multiple source files. This kind of project structure has numerous advantages, the biggest of which is better organization of programs. It also promotes code reuse because functions that you want to use over and over again may be placed in their own file(s) and then included in any project where you want to use them.

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files



Open the Project

Start MPLAB® X, then click on the Open Project Main_Open_Project.png icon on the main toolbar

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab09.X folder.

Select Open Project OpenProjectButton.png.


Open C Source Files and Header Files


Open the Header Files and the Source Files from the project tree by double clicking them.

We will edit some of these files in the remainder of this lab.


Edit Source Code


This project is quite a bit different from the others we’ve worked on so far. We will be dealing with five different files, all of which will be interacting with each other. As before, Lab09.c will contain our main() function, and therefore the program will begin executing from that point. From within the main() function, we will be calling functions that reside in file1_09.c and file2_09.c. The header files associated with file1 and file2 contain the function prototypes that are needed in Lab09.c to be able to call the functions that reside in the separate files.
The files file1_09.c and file2_09.c don’t require any editing. In those files, we defined several variables and a couple functions just as we did in Lab8.c. Essentially these files look like any ordinary main file, but without a main() function (a project can only have one of those). Instead, we will be editing the header files, which provide the connection between Lab09.c and the other two C files.
Note that this lab does the exact same thing as Lab08, but the two function definitions and their associated variable definitions have been placed in separate files.

STEP 1a:
Open the file file1_09.h. Add external variable declarations to make the variables defined in file1_09.c available to any C source file that includes this header file. The variables you need to create external definitions for are:
intVariable1, intVariable2 and product.

STEP 1b:
Add a function prototype to make multiply_function() defined in file1_09.c available to any C source file that includes this header file.

STEP 2a:
Open the file file2_09.h. Add external variable declarations to make the variables defined in file2_09.c available to any C source file that includes this header file. The variables you need to create external definitions for are:
floatVariable1, floatVariable2, quotient and intQuotient.

STEP 2b:
Add a function prototype to make divide_function() defined in file2_09.c available to any C source file that includes this header file.



Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.

Set up the Watches window:

Lab 9 uses the same variables as Lab08; however, in this lab they have been defined as Static Variables. MPLAB® X refers to these variables as Global Symbols. To view the Lab09 variables we must use and configure the Watches Window

  • Open the Watches Window ( Windows -> Debugging -> Watches ) or (Alt + shift + 2 )
  • “Right Click” in the Watches Window and select “New Watch”
  • From the global symbol select intVariable1
  • Repeat the above 2 steps until intVariable2, floatVarivable1, floatVariable2, quotient, intQuotient, and product have all been added to the Watches Window
Click on the Continue Debug_Continue.png button. This begins the simulation.
Click on the Halt Debug_Pause.png button. This will stop execution so that we may examine the variables and their values.



End Debug Session

Erase the Watches Window by right clicking in the Watches Window and selecting Delete All.
End the Simulation Session by clicking the Finish Debugger Session Debug_Finish_Debugger_Session.png button.

Close the Project.


Multi-file projects take the concept of functions a step further, by allowing further organization and separation of functionality within a program. Separate files allow you to group related functions and variables in such a way that they may be used among several different programs. Using multiple files incurs no additional overhead, so you can feel free to use them whenever they make sense.

© 2017 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.