Using Multiple Compilation Units Guide
Using Multiple Compilation Units Guide
Files in project:
main_mcu.c Primary file for the first compilation unit
filter_mcu.c Primary file for the second compilation unit
report_mcu.c Primary file for the third compilation unit
project_mcu.h Include file with project wide definitions, should be included by all units
filter_mcu.h External definitions for filter, should be included by all units that use the
filter unit
report_mcu.h External definitions for report, should be included by all units that use
report
buildall_mcu.bat Batch file that compiles and links all units
build_mcu.bat Batch file that recompiles files needing compiling and links
project_mcu.pjt Used by build_mcu.bat to list project units
report_data_line()
report_line_number
Each unit:
*.o (relocatable object)
*.err (error file) project_mcu.hex (final load image)
*.osym (unit symbols) project_mcu.lst (C and ASM listing)
project_mcu.sym (project symbols)
project_mcu.cof (debugger file)
Building the project from the command line:
Note that after a project is linked if no .pjt file exists the linker will create
one that may be used with the BUILD= option in the future.
● The above screen is from OPTIONS > PROJECT OPTIONS after loading the
project_mcu.pjt file. If the file does not exist create the project manually and
make a screen like the above.
● Right Click in the FILES slide out and select BUILD ALL to rebuild and link the
whole project. Performing a BUILD ALL creates all the “.h” files that get linked to
the units and it also links the units together to create the output files that are
loaded onto the devices. Once a BUILD ALL is completed with no errors, the
code is ready to be run.
● This pane is helpful in managing each unit in the project. Review the right click
options for the full range of options.
Using MPLAB X IDE to work with Multiple Compilation Units
● Create a new project by selecting “File -> New Project” from the toolbar. Follow the
dialog boxes to specify the project type.
● Make sure MPLAB is configured for the proper chip, as the CCS C compiler uses this
selection to determine which compiler to use (PCB, PCM, PCH, PCD, etc). The chip is
selected when creating a new project in the “Select Device” window.
● Many project build options (such as output directory, include directories, output files
generated, etc) can be changed by selecting “Run -> Set Project Configuration“ from
the MPLAB toolbar.
● If the compile fails with an error that says something like “Target chip not supported” or
“Compiler not found” make sure that a.) you have the proper PIC selected (use “Run ->
Set Project Configuration -> Conf:” from the MPLAB toolbar), b.) the CCS C Toolsuite
has been selected for this project (use “Run -> Set Project Configuration -> Conf:”
under “Compiler Toolchain” from the MPLAB toolbar) and c.) the path for CCSC.EXE is
configured correctly for your installation of the CCS C Compiler (use “Tools-> Options
-> Embedded -> Build Tools” on the MPLAB toolbar)
Notes
● By default variables declared at the unit level (outside a function) are visible to all other
units. To make a variable private to the unit use the keyword static. Notice
report_mcu.c defines the variable report_line_number. If the definition were changed
to look as the following line then there would be a link time error since main_mcu.c
attempts to use the variable.
static long report_line_number;
● This same rule applies to functions. Use static to make a function local to the unit.
● Should two units have a function or unit level variable with the same name an error is
generated unless one of the following is true:
● The identifier is qualified with static.
● The argument list is different and two instances of the function can co-exist in
the project in accordance with the normal overload rules.
● The contents of the functions are absolutely identical. In this case the CCS
linker simply deletes the duplicate function.
● The standard C libraries (like stdlib.h) are supplied with source code in the .h file.
Because of the above rule these files may be #include'd in multiple units without taking
up extra ROM and with no need to include these in the link command since they are
not units.
● #define's are never exported to other units. If a #define needs to be shared between
units put them in an include file that is #include'd by both units. Project wide defines in
our example could go into project_mcu.h.
● It is best to have an include file like project_mcu.h that all units #include. This file
should define the chip, speed, fuses and any other compiler settings that should be the
same for all units in the project.
● In this example project a #USE RS232 is in the project_mcu.h file. This creates an
RS232 library in each unit. The linker is able to determine the libraries are the same
and the duplicates removed in the final link.
● Each unit has its own error file (like filter_mcu.err). When the compilations are done in
a batch file it may be useful to terminate the batch run on the first error. The +CC
command line option will cause the compiler to return a windows error code if the
compilation fails. This can be tested in the batch file like this: