Ruslan
Ruslan

Reputation: 11

Makefile for IAR. List of src

I have variable in my Makefile:

SRCP = \
 main.c \
 lib1/src/file1.c \
 lib2/src/file2.c

Folders lib1/src and lib2/src may consist other sources, but I need compil only files in SRCP var.

I know one variant:

OBJ_DIR = ./Release/Obj
OBJ = $(SRC:.c=.o)
$(OBJ): $(SRCP)
    for source in $(SRCP); do \
        $(CC) $(CLAGS) -c $$source -o $(OBJ_DIR)/$@; \
    done

But not informative. In console I see $source instead main.c (or any name).

My makefile with errors:

.PHONY: all clean

PROJECT = hello

IAR_TARGET = ./Release
EXE_DIR = $(IAR_TARGET)/Exe
OBJ_DIR = $(IAR_TARGET)/Obj

COMPILE_OPTS = -mcpu=cortex-m3 -mthumb -Wall -g -O0
INCLUDE_DIRS = -I . \
 -I lib/inc

SRCP = \
 main.c \
 lib/src/stm32f10x_tim.c \
 lib/src/stm32f10x_adc.c

LIB = -L ./lib/src

SRC := $(notdir $(SRCP))

OBJ_FILES := $(addprefix $(OBJ_DIR)/,$(notdir $(SRCP:.c=.o)))

CC = arm-none-eabi-gcc
CFLAGS = $(COMPILE_OPTS) $(INCLUDE_DIRS)

# mkdir -p $(EXE_DIR) $(OBJ_DIR)
all: $(EXE_DIR)/$(PROJECT).elf

# Linker invocation
$(EXE_DIR)/$(PROJECT).elf: $(OBJ_FILES)
    $(CC) $(CFLAGS) $(OBJ_FILES) -o $(EXE_DIR)/$(PROJECT).elf

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

clean:
    rm -rf $(IAR_TARGET)

Error: make: *** No rule to make target 'Release/Obj/main.o', needed by 'Release/Exe/hello.elf'. Stop.

What is the correct version you know?

Upvotes: 1

Views: 1681

Answers (2)

Ruslan
Ruslan

Reputation: 11

I know!

I have list of files with path:

SRCP := \
  main.c \
  lib/src/stm32f10x_tim.c \
  lib/src/stm32f10x_adc.c

How to get .c file with path by object name? Look:

OBJ_DIR=Release/Obj
# Rules
%.o:
    @echo creating $@ ...
    $(CC) $(CFLAGS) -c $(filter %$(subst .o,.c,$@), $(SRCP)) -o $(OBJ_DIR)/$@

How to work this $(filter %$(subst .o,.c,$@), $(SRCP))?

  • For example, $@ have stm32f10x_adc.o
  • $(subst .o,.c,$@) change .o to .c, so we have stm32f10x_adc.c. Very important! This string do not have spaces (like I like:) ). I do mistake, when write $(subst .o, .c, $@). It is not clear for me (:
  • %$(subst .o,.c,$@). % work with filter.
  • $(filter %$(subst .o,.c,$@), $(SRCP)) take %stm32f10x_adc.c from $(SRCP) with path. And... We have lib/src/stm32f10x_adc.c!

Thankyou for the help!

Upvotes: 0

Codo
Codo

Reputation: 78835

I don't have any specific experience with IAR projects. But I guess I can help you with the Makefile.

The main changes I propose are:

  • Change the definition of OBJ_FILES such that it contains a list of the object files with the proper path name, e.g. ./Release/Obj/file1.o. notdir removes the directory, addprefix adds the new directory.

  • Add a pattern rule how to convert a .c file into a .o file. As object files are a dependency of the final file (a .elf file in this example) and as there are no explicit rules to build the these object file, make will use the pattern rule to build them.

Most likely, GNU make is required for this makefile.

SRCP = \
  main.c \
  lib/src/stm32f10x_tim.c \
  lib/src/stm32f10x_adc.c

OBJ_DIR = ./Release/Obj
OBJ_FILES := $(addprefix $(OBJ_DIR)/,$(notdir $(SRCP:.c=.o)))

# Linker invocation
$(OUTPUT_DIR)/$(PROJECT).elf: $(OBJ_FILES)
    $(CC) $(LDFLAGS) $(OBJ_FILES) $(STD_LIBS) -o $(OUTPUT_DIR)/$(PROJECT).elf

# Rules
$(OBJDIR)/%.o: ./%.c
    $(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: lib/src/%.c
    $(CC) $(CFLAGS) -c $< -o $@

Upvotes: 2

Related Questions