6.3.6.3.2 User Makefile Project – Create Code from User Makefile

To compile the code, the simple user makefile Makefile takes as arguments production, debug or clean. If production is passed then t_production.hex is built. If debug is passed then t_debug.elf is built. If clean is passed then the .elf and .hex files are erased.

Example: User Makefile Code

# Assumes xc32gcc and xc32bin2hex are on the path
COMPILER=xc32gcc
BIN2HEX=xc32bin2hex
PROCESSOR=32MX795F512L
production: t_production.hex
debug: t_debug.elf
t_debug.elf: t.c
$(COMPILER) mprocessor=$(PROCESSOR) -g -D_DEBUG -o t_debug.elf
t.c
t_production.hex: t_production.elf
$(BIN2HEX) t_production.elf
t_production.elf: t.c
$(COMPILER) mprocessor=$(PROCESSOR) o t_production.elf t.c
clean:
del /F *.hex
del /F *.elf

To create a new user makefile project in MPLAB X IDE, follow steps 1-5 from 6.3.6.3.1 User Makefile Project – Create Code from a Batch File. For Step 6, set up as per figure below.

The Working Directory is where the user Makefile build, debug build and clean commands will be run. The Working Directory, Image name and the Debug image name are entered as relative paths with respect to the MPLAB X IDE project directory.

MPLAB X IDE uses GNU Make to build, debug build and clean. As per 6.3.6.2 User Makefile Project Folder and Working Folder, MPLAB X IDE will run the Makefile in the project folder, which will change directories to the working folder. There, make will look for a makefile and find the user-defined Makefile, which will define how to build, debug build, and clean.

Note: Another way to define commands, for example the Build command, would be to use the -f option to specify the user makefile, as in:
 make -f Makefile production.

Click Finish to create the project.

Figure 6-28. User Makefile Project – Create from Makefile