I am working with microchip microcontrollers for a quite a while, and I fully dislike very much Netbeans. So since Mplab migrated to Netbeans I am trying to do all the work by my own, and here is the help needed:

I want to compile all .c files in the project folder and make the object file of every .c file. And the last command is the link of all object files.

I paste one of my makefile. The thing is, I have to do this to every project, and I am trying to make it automatic, without writing any .c file name in the makefile. I tryed to use wildcard, but generates me something wrong to compile. Generates me one object file, when I have more than one.

If somebody can help me, I would really appreciate it.

This is one of my makefiles:

CFLAGS = -c -MP -MMD -g -omf=elf -DXPRJ_default=default -legacy-libc -O2 -msmart-io=1 -msfr-warn=off -mdfp="/opt/microchip/mplabx/v6.15/packs/Microchip/dsPIC30F_DFP/1.4.104/xc16"
MOREFLAGS = -pedantic -msmall-code -msmall-data -msmall-scalar -Wall -std=c99 -ansi
MCPU = -mcpu=30F4013
DSPFLAGS = --library=q --library=dsp
COMLINK = -omf=elf -mdfp="/opt/microchip/mplabx/v6.15/packs/Microchip/dsPIC30F_DFP/1.4.104/xc16"

build: build1 build2 build3 build4 build5 build6 build7 build8 build9

    xc16-gcc ds18b20.c      -o ds18b20.o    ${MCPU} ${CFLAGS} ${MOREFLAGS}

    xc16-gcc ADC.c          -o ADC.o        ${MCPU} ${CFLAGS} ${MOREFLAGS}

    xc16-gcc config.c       -o config.o     ${MCPU} ${CFLAGS} ${MOREFLAGS}

    xc16-gcc intConfig.c    -o intConfig.o  ${MCPU} ${CFLAGS} ${MOREFLAGS}

    xc16-gcc uartConfig.c   -o uartConfig.o ${MCPU} ${CFLAGS} ${MOREFLAGS}

    xc16-gcc I2C_Lib.c      -o I2C_Lib.o    ${MCPU} ${CFLAGS} ${MOREFLAGS}

    xc16-gcc LCD_I2C.c      -o LCD_I2C.o    ${MCPU} ${CFLAGS} ${MOREFLAGS} 

    xc16-gcc -o ds18b20.elf ds18b20.o ADC.o config.o intConfig.o uartConfig.o I2C_Lib.o LCD_I2C.o ${MCPU} -DXPRJ_default=default -legacy-libc -Wl,,,--defsym=__MPLAB_BUILD=1,,--script=p30F4013.gld,--stack=16,--check-sections,--data-init,--pack-data,--handles,--isr,--no-gc-sections,--fill-upper=0,--stackguard=16,--no-force-link,--smart-io,--report-mem,--memorysummary,memoryfile.xml ${COMLINK}

    xc16-bin2hex ds18b20.elf -a ${COMLINK}
    rm -f *.d *.o *.elf *.xml *.hex *.map
# https://gcc.gnu.org/onlinedocs/
# https://onlinedocs.microchip.com/pr/GUID-C4E60FF5-3DAB-44F1-BA61-4BD962D8F469-en-US-2/index.html?GUID-A3D1ECDB-7A9F-42AF-A832-DB5288E4EA04

1 Answer 1


With GNU Make, you can build a list of object files based on existing C files as follows:

OBJS := $(patsubst %.c,%.o,$(wildcard *.c))

Then you need a static pattern rule to build the object files individually:

$(OBJS): %.o: %.c
        xc16-gcc -c $(CFLAGS) $< -o $@ $(MCPU) $(MOREFLAGS)

(drop the -c from CFLAGS), or set your variables up so that you can use Make’s own builtin rules:

CC = xc16-gcc $(MCPU)

Next, define a rule for your linked binary, using an actual target (it’s always better to use real targets rather than phony ones):

ds18b20.elf: $(OBJS)
        xc16-gcc -o $@ $^ $(MCPU) -DXPRJ_default=default -legacy-libc -Wl,,,--defsym=__MPLAB_BUILD=1,,--script=p30F4013.gld,--stack=16,--check-sections,--data-init,--pack-data,--handles,--isr,--no-gc-sections,--fill-upper=0,--stackguard=16,--no-force-link,--smart-io,--report-mem,--memorysummary,memoryfile.xml $(COMLINK)

Last, the .hex target (I’m not sure about this one since your target doesn’t appear explicitly):

ds18b20.hex: ds18b20.elf
        xc16-bin2hex $< -a $(COMLINK)

Replace the first phony target with

build: ds18b20.hex

and you’re done.

  • I don't think you even need to create the implicit $(OBJS): %.o: %.c rule as there's one already in the default set of rules for make (see make -p -f /dev/null and search for %.o: %.c). You'd just need to set $CC, $CFLAGS, and maybe $TARGET_ARCH appropriately. Commented Oct 26, 2023 at 12:52
  • Yeah, I know, but I get pushback every time I mention the builtin rules here so … Commented Oct 26, 2023 at 12:58
  • Really? Oh :-( (Personally, I find being able to make lunch when I've got a lunch.c but no Makefile really convenient.) Commented Oct 26, 2023 at 13:04
  • 1
    Oh I love them, I use Make without Makefiles all the time! (Well, when I’m writing C.) Commented Oct 26, 2023 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .