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 IDE, 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.

Some of these files are edited in the remainder of this lab.


Edit Source Code


This project is a bit different from the others you’ve worked on so far. You are dealing with five different files, all of which will be interacting with each other. As before, Lab09.c contains the main() function and therefore the program begins executing from that point. From within the main() function, you need to call 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, several variables and a couple functions are defined just as they are 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, you will be editing the header files, which provide the connection between Lab09.c and the other two C files.

This lab does the exact same thing as Lab 8, but the two function definitions and their associated variable definitions have been placed in separate files.

STEP 1a:
Open 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 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 builds and sends the program to the simulator.

Set up the Watches window:

Lab 9 uses the same variables as Lab 8; however, in this lab, they have been defined as static variables. MPLAB X IDE refers to these variables as Global Symbols. To view the Lab 9 variables you must use and configure the Watches window.

  • Open the Watches window by going to Windows > Debugging > Watches or pressing Alt + shift + 2.
  • Right-click within 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 stops execution so that you can examine the variables and their values.


The results for Lab 9 are very similar to those of Lab 8. In this lab, all the variables were defined outside of a function ( i.e., static variables). The only way they can be displayed by the IDE is to use the Watches window.



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.


Line numbers correspond to those in the provided solution file.

File1_09.h Lines 7-9
STEP 1a:
Writes external declarations for the variables defined in file1_09.c. In that file, there are three variables:

int intVariable1 = 0;
int intVariable2 = 0;
int product = 0;

In order to make them available to other source files, they must be declared as extern where they are used. The easiest way to provide this is to put the extern declarations into a header file that may be included in any source file that uses these variables. All you need to do is duplicate the variable declarations from file1_09.c and put the extern keyword in front of them (NOTE: you cannot initialize an extern variable declaration — only the actual definition may use an initializer):

extern int intVariable1;
extern int intVariable2;
extern int product;

File1_09.h Line 20
STEP 1b:
Provides a function prototype to make multiply_function() available to other files. This function prototype is identical to the one you created in Lab 8:

int multiply_function(int x, int y);

File2_09.h Lines 7-10
STEP 2a:
Similar to step 1a, but this time makes the variables in file2_09.c available to other files. The variables in file2_09.c are:

float floatVariable1 = 0;
float floatVariable2 = 0;
float quotient = 0;
int intQuotient = 0;

And just like step 1a, you only need to add the extern keyword in front of them when they are added to the header file:

extern float floatVariable1;
extern float floatVariable2;
extern float quotient;
extern int intQuotient;

File2_09.h Line 21
STEP 2b:
Makes divide_function() available to other files by adding a function prototype here. Again, this is just like the one you created in Lab 8.

float divide_function(float x, float y );


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.

© 2024 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.