K
K
Klaxons2015-09-23 18:33:55
linux
Klaxons, 2015-09-23 18:33:55

How to include files in Makefile?

Good afternoon, tell me, is there a directory with the project and libraries
directory structure

/---------------------
main.c
lib
   |
   |-------cmsis
   |-------mylib
                   |
                   |--------led
                                 |
                                 |-------- led.h
                                 |-------- led.c
   |-------STM32F4xx_StdPeriph_Driver
build
Makefile

// led.h

#include <stdio.h>
#include "stm32f4xx.h"

void turnOn();
void turnOff();

// led.c

#include "led.h"

void turnOn()
{
  GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
}

void turnOff()
{
  GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
}

// main.c

#include <stdio.h>
#include "stm32f4xx.h"
#include "led.h"
...
// init gpio
...
extern void turnOn();
extern void turnOff();
...
// init sys clock
...

int main(void) {
  setSysTick();

  while(1)
  {
    //check the push button
    turnOn();
    Delay(500);
    turnOff();
  }

  return 0;
}

Description of the Makefile for the linker and compiler
# Makefile

# STM32F4-Discovery Makefile for Mac OSX

SRCS=$(wildcard *.c) 
# Add assembly source files here or use $(wildcard *.s) for all .s files
S_SRCS = CortexM4asmOps.s


# Project name
PROJ_NAME = test
OUTPATH = build

OUTPATH := $(abspath $(OUTPATH))
BASEDIR := $(abspath ./)
MKDIR_P = mkdir -p


###################################################

# Check for valid float argument
# NOTE that you have to run make clan after
# changing these as hardfloat and softfloat are not
# binary compatible
ifneq ($(FLOAT_TYPE), hard)
ifneq ($(FLOAT_TYPE), soft)
#override FLOAT_TYPE = hard
override FLOAT_TYPE = soft
endif
endif

###################################################

AS=$(BINPATH)arm-none-eabi-as
CC=$(BINPATH)arm-none-eabi-gcc
LD=$(BINPATH)arm-none-eabi-gcc
OBJCOPY=$(BINPATH)arm-none-eabi-objcopy
OBJDUMP=$(BINPATH)arm-none-eabi-objdump
SIZE=$(BINPATH)arm-none-eabi-size

LINKER_SCRIPT = stm32_flash.ld

CPU = -mcpu=cortex-m4 -mthumb

CFLAGS  = $(CPU) -c -std=gnu99 -g -O2 -Wall
LDFLAGS  = $(CPU) -mlittle-endian -mthumb-interwork -nostartfiles -Wl,--gc-sections,-Map=$(OUTPATH)/$(PROJ_NAME).map,--cref --specs=nano.specs

ifeq ($(FLOAT_TYPE), hard)
CFLAGS += -fsingle-precision-constant -Wdouble-promotion
CFLAGS += -mfpu=fpv4-sp-d16 -mfloat-abi=hard
else
CFLAGS += -msoft-float
endif

# Default to STM32F40_41xxx if no device is passed
ifeq ($(DEVICE_DEF), )
DEVICE_DEF = STM32F40_41xxx
endif

CFLAGS += -D$(DEVICE_DEF)

vpath %.a %.c lib


# Includes
INCLUDE_PATHS = -I$(BASEDIR)/inc -I$(BASEDIR)/lib/cmsis/stm32f4xx -I$(BASEDIR)/lib/cmsis/include -I$(BASEDIR)
INCLUDE_PATHS += -I$(BASEDIR)/lib/Conf
INCLUDE_PATHS += -I$(BASEDIR)/lib/STM32F4xx_StdPeriph_Driver/inc

INCLUDE_PATHS += -I$(BASEDIR)/lib/mylib
INCLUDE_PATHS += -I$(BASEDIR)/lib/mylib/led

# Library paths
LIBPATHS = -L$(BASEDIR)/lib/STM32F4xx_StdPeriph_Driver

# Libraries to link
LIBS = -lstdperiph -lc -lgcc -lnosys

OBJS = $(SRCS:.c=.o)
OBJS += $(S_SRCS:.s=.o)

###################################################

.PHONY: lib proj

all: dir lib proj
  $(SIZE) $(OUTPATH)/$(PROJ_NAME).elf

lib:
  $(MAKE) -C lib FLOAT_TYPE=$(FLOAT_TYPE) BINPATH=$(BINPATH) DEVICE_DEF=$(DEVICE_DEF) BASEDIR=$(BASEDIR)

proj: $(OUTPATH)/$(PROJ_NAME).elf

.s.o:
  $(AS) $(CPU) -o $(addprefix $(OUTPATH)/, [email protected]) $<

.c.o:
  $(CC) $(CFLAGS) -std=gnu99 $(INCLUDE_PATHS) -o $(addprefix  $(OUTPATH)/, [email protected]) $<

$(OUTPATH)/$(PROJ_NAME).elf: $(OBJS)
  $(LD) $(LDFLAGS) -T$(LINKER_SCRIPT) $(LIBPATHS) -o [email protected] $(addprefix $(OUTPATH)/, $^) $(LIBS) $(LD_SYS_LIBS)
  $(OBJCOPY) -O ihex $(OUTPATH)/$(PROJ_NAME).elf $(OUTPATH)/$(PROJ_NAME).hex
  $(OBJCOPY) -O binary $(OUTPATH)/$(PROJ_NAME).elf $(OUTPATH)/$(PROJ_NAME).bin
  $(OBJDUMP) -S --disassemble $(OUTPATH)/$(PROJ_NAME).elf > $(OUTPATH)/$(PROJ_NAME).dis

dir:
  $(MKDIR_P) $(OUTPATH)

clean:
  rm -f $(OUTPATH)/*.o
  rm -f $(OUTPATH)/$(PROJ_NAME).elf
  rm -f $(OUTPATH)/$(PROJ_NAME).hex
  rm -f $(OUTPATH)/$(PROJ_NAME).bin
  rm -f $(OUTPATH)/$(PROJ_NAME).dis
  rm -f $(OUTPATH)/$(PROJ_NAME).map
  # Remove the following line if you don't want to clean the Libraries as well
  $(MAKE) clean -C lib

flash:
  st-flash write build/$(PROJ_NAME).bin 0x08000000

compiling libraries
make lib
make[1]: `mylib' is up to date.
I collect the binary
make
gives an error
build/main.o: In function `main':
main.c:37: undefined reference to `turnOn'
main.c:39: undefined reference to `turnOff'
collect2: error: ld returned 1 exit status
make: *** [build/test.elf] Error 1

If the files from lib/mylib/led are moved to the root (to the same level as main.c), then everything is going well

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aol-nnov, 2015-09-23
@aol-nnov

try SRCS=$(wildcard *.c) $(wildcard led/*.c) or just list through space with paths relative to Makefile?

D
Daniil Smirnov, 2015-09-26
@antonsosnitzkij

I, unfortunately, am a layman in this topic, but now I have a pair of dumb teachers on this topic and as a solution to the problem, I can add a similar line to the makefile:
$(SRC_DIR)/ led.c

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question