josef.van.niekerk
josef.van.niekerk

Reputation: 12121

Build individual .o files from .c files in Makefile

I'm trying to write a Makefile that will build a list of .o files from source, and then in a separate make target, setup linking etc. My Makefile currently looks like this:

CC=arm-none-eabi-gcc

vpath %.c src src/peripherals
vpath %.o out

OUT_DIR = out

CFLAGS  = -DUSE_STDPERIPH_DRIVER
CFLAGS += -c -fmessage-length=0 -g3 -gdwarf-2 -O0 -Wall -Wa,-adhlns="[email protected]"
CFLAGS += -mthumb -mcpu=cortex-m4
CFLAGS += -MMD -MP -MF"[email protected]" -MT"[email protected]"
CFLAGS += -Iinc -Iinc/cmsis -Iinc/peripherals -Iinc/stm32f4xx

SRC = misc.c stm32f4xx_adc.c stm32f4xx_can.c stm32f4xx_crc.c stm32f4xx_cryp.c stm32f4xx_cryp_aes.c \
    stm32f4xx_cryp_des.c stm32f4xx_cryp_tdes.c stm32f4xx_dac.c stm32f4xx_dbgmcu.c stm32f4xx_dcmi.c stm32f4xx_dma.c \
    stm32f4xx_exti.c stm32f4xx_flash.c stm32f4xx_fsmc.c stm32f4xx_gpio.c stm32f4xx_hash.c stm32f4xx_hash_md5.c \
    stm32f4xx_hash_sha1.c stm32f4xx_i2c.c stm32f4xx_iwdg.c stm32f4xx_pwr.c stm32f4xx_rcc.c stm32f4xx_rng.c \
    stm32f4xx_rtc.c stm32f4xx_sdio.c stm32f4xx_spi.c stm32f4xx_syscfg.c stm32f4xx_tim.c stm32f4xx_usart.c \
    stm32f4xx_wwdg.c

OBJ = $(SRC:.c=.o)

%.o : %.c 
    $(CC) -c -o $@ $< $(CFLAGS)

all: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS)

I know this is terribly wrong, I'm not an expert with working in Makefile's, but I want to get this right, as it helps to understand the process a bit better.

What's basically required is to take the list of .c files in $(SRC) and build them into a list of .o files, which are output in lib/out

I know that my target and %.o... rule is horribly messed up. How do I get the all: target to build the individual .o files.

Here's the project structure just for reference, the Makefile I'm working on is in the ./lib folder.

.
├── Makefile
├── inc
│   └── main.h
├── lib
│   ├── Makefile
│   ├── inc
│   │   ├── cmsis
│   │   │   ├── arm_common_tables.h
│   │   │   ├── ...
│   │   ├── peripherals
│   │   │   ├── misc.h
│   │   │   ├── stm32f4xx_adc.h
│   │   │   ├── ...
│   │   └── stm32f4xx
│   │       ├── stm32f4xx.h
│   │       ├── stm32f4xx_conf.h
│   │       └── system_stm32f4xx.h
│   ├── src
│   │   └── peripherals
│   │       ├── misc.c
│   │       ├── stm32f4xx_adc.c
│   │       ├── ...
│   ├── startup_stm32f4xx.s
│   └── ~Makefile
├── readme.md
├── src
│   └── main.c
├── stm32f4.ld
├── stm32f4discovery.cfg
├── system_stm32f4xx.c
└── ~Makefile

Upvotes: 1

Views: 927

Answers (1)

Kamath
Kamath

Reputation: 4664

typo OBJ = $(SRCS:.c=.o) change to OBJ = $(SRC:.c=.o), also looks like there is something messy with your dependency file generation.

Upvotes: 1

Related Questions