Forum: Compiler & IDEs Makefile Verständnisproblem


von Simon H. (simi)


Lesenswert?

Hallo zusammen,

ich habe hier ein Rudel Makefiles vor mir, die perfekt funktionieren, 
nur habe ich nicht die leiseste Ahnung, warum. Es geht im Wesentlichen 
um drei Files: Das makefile im Root, welches das Makefile im app Folder 
aufruft (und eines im lib Folder, das habe ich aber nicht angehängt). 
makefile.common definiert die Toolchain etc. und wird von den anderen 
included.


Wenn ich nun im app-folder "make app" starte, compiliert er mir alle 
sourcen im app folder. Und ich vertehe nicht, wie der das überhaupt 
kann.

Auszug aus main im app-folder:
1
app: app.a
2
3
app.a: $(OBJS)
4
  $(AR) cr app.a $(OBJS)

er will das Target app und benötigt dafür erst mal das Target app.a.

Für app.a benötigt er die object-files, definiert durch die Variable 
OBJS.

So. Und hier stehe ich nun an. Woher zum Geier weiss der, wie er die 
object files erzeugen soll? Er lässt auf jedes .c file ein gcc laufen. 
Woher weiss der das? Im makefile.common ist die Variable CC definiert. 
Wenn ich die Variable CC in "$(TC)-gccxxxx" verändere, versucht er, für 
jedes c file gccxxx aufzurufen. Aber wo ist hier der Bezug auf diese 
Variable? Wenn ich nach $(CC) suche, finde ich genau eine Stelle. Um da 
durchzukommen, sollte er aber die Object Files schon haben.

Ich stehe irgendwie total auf dem Schlauch. Kann mir jemand einen Tipp 
geben?

Gruäss
Simon




makefile.common (wird included):
1
#include Makefile
2
3
#This file is included in the general Makefile, the libs Makefile and the src Makefile
4
#Different optimize settings for library and source files can be realized by using arguments
5
#Compiler optimize settings:
6
# -O0 no optimize, reduce compilation time and make debugging produce the expected results (default).
7
# -O1 optimize, reduce code size and execution time, without much increase of compilation time.
8
# -O2 optimize, reduce code execution time compared to ‘O1’, increase of compilation time.
9
# -O3 optimize, turns on all optimizations, further increase of compilation time. 
10
# -Os optimize for size, enables all ‘-O2’ optimizations that do not typically increase code size and other code size optimizations.
11
#Recommended optimize settings for release version: -O3
12
#Recommended optimize settings for debug version: -O0
13
#Valid parameters :
14
# OptLIB=0 --> optimize library files using the -O0 setting
15
# OptLIB=1 --> optimize library files using the -O1 setting 
16
# OptLIB=2 --> optimize library files using the -O2 setting
17
# OptLIB=3 --> optimize library files using the -O3 setting
18
# OptLIB=s --> optimize library files using the -Os setting
19
# OptSRC=0 --> optimize source files using the -O0 setting
20
# OptSRC=1 --> optimize source files using the -O1 setting 
21
# OptSRC=2 --> optimize source files using the -O2 setting
22
# OptSRC=3 --> optimize source files using the -O3 setting
23
# OptSRC=s --> optimize source files using the -Os setting
24
# all --> build all
25
# libs --> build libs only
26
# src --> build src only
27
# clean --> clean project
28
# tshow --> show optimize settings
29
#Example:
30
# make OptLIB=3 OptSRC=0 all tshow
31
32
#TOP=$(shell readlink -f "$(dir $(lastword $(MAKEFILE_LIST)))")
33
TOP=E:/git/FigoShot/FigoShot
34
PROGRAM=main
35
LIBDIR=$(TOP)/Libs
36
37
#Adust the following line to the library in use
38
#STMLIB=$(LIBDIR)/STM32F10x_StdPeriph_Lib_V3.6.1/Libraries
39
STMLIB=$(LIBDIR)/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries
40
41
#Adjust TypeOfMCU in use, see CMSIS file "stm32f10x.h"
42
#STM32F103RBT (128KB FLASH, 20KB RAM) --> STM32F10X_MD
43
TypeOfMCU=STM32F10X_LD_VL
44
#STM32F103RET (512KB FLASH, 64KB RAM) --> STM32F10X_HD
45
#TypeOfMCU=STM32F10X_HD
46
47
TC=arm-none-eabi
48
CC=$(TC)-gcc
49
LD=$(TC)-ld -v
50
OBJCOPY=$(TC)-objcopy
51
AR=$(TC)-ar
52
GDB=$(TC)-gdb
53
54
INCLUDE=-I$(TOP)/inc
55
INCLUDE+=-I$(STMLIB)/CMSIS/CM3/CoreSupport
56
INCLUDE+=-I$(STMLIB)/CMSIS/CM3/DeviceSupport/ST/STM32F10x
57
INCLUDE+=-I$(STMLIB)/STM32F10x_StdPeriph_Driver/inc
58
INCLUDE+=-I$(TOP)/app
59
60
61
COMMONFLAGS=-g -mcpu=cortex-m3 -mthumb
62
COMMONFLAGSlib=$(COMMONFLAGS)
63
64
#Commands for general Makefile and src Makefile 
65
ifeq ($(OptSRC),0)
66
  COMMONFLAGS+=-O0
67
  InfoTextSrc=src (no optimize, -O0)
68
else ifeq ($(OptSRC),1)
69
  COMMONFLAGS+=-O1
70
  InfoTextSrc=src (optimize time+ size+, -O1)
71
else ifeq ($(OptSRC),2)
72
   COMMONFLAGS+=-O2
73
   InfoTextSrc=src (optimize time++ size+, -O2)
74
else ifeq ($(OptSRC),s)
75
   COMMONFLAGS+=-Os
76
   InfoTextSrc=src (optimize size++, -Os)
77
else
78
   COMMONFLAGS+=-O3
79
   InfoTextSrc=src (full optimize, -O3)
80
endif
81
CFLAGS+=$(COMMONFLAGS) -Wall -Werror $(INCLUDE)
82
CFLAGS+=-D $(TypeOfMCU)
83
CFLAGS+=-D VECT_TAB_FLASH
84
85
#Commands for libs Makefile
86
ifeq ($(OptLIB),0)
87
  COMMONFLAGSlib+=-O0
88
  InfoTextLib=libs (no optimize, -O0)
89
else ifeq ($(OptLIB),1)
90
  COMMONFLAGSlib+=-O1
91
  InfoTextLib=libs (optimize time+ size+, -O1)
92
else ifeq ($(OptLIB),2)
93
   COMMONFLAGSlib+=-O2 
94
   InfoTextLib=libs (optimize time++ size+, -O2)
95
else ifeq ($(OptLIB),s)
96
   COMMONFLAGSlib+=-Os 
97
   InfoTextLib=libs (optimize size++, -Os)
98
else
99
   COMMONFLAGSlib+=-O3
100
   InfoTextLib=libs (full optimize, -O3)
101
endif  
102
CFLAGSlib+=$(COMMONFLAGSlib) -Wall -Werror $(INCLUDE)
103
CFLAGSlib+=-D $(TypeOfMCU)
104
CFLAGSlib+=-D VECT_TAB_FLASH

makefile des Projekts:
1
#general Makefile
2
3
include Makefile.common
4
LDFLAGS=$(COMMONFLAGS) -fno-exceptions -ffunction-sections -fdata-sections -L$(LIBDIR) -nostartfiles -Wl,--gc-sections,-Tlink.ld
5
6
LDLIBS+=-lm
7
LDLIBS+=-lstm32
8
9
#STARTUP=startup.c
10
11
12
all: libs app
13
  $(CC) -o $(PROGRAM).elf $(LDFLAGS) \
14
    -Wl,--whole-archive \
15
      app/app.a \
16
    -Wl,--no-whole-archive \
17
      $(LDLIBS)
18
  $(OBJCOPY) -O ihex   $(PROGRAM).elf $(PROGRAM).hex
19
  $(OBJCOPY) -O binary $(PROGRAM).elf $(PROGRAM).bin
20
#Extract info contained in ELF to readable text-files:
21
  arm-none-eabi-readelf -a $(PROGRAM).elf > $(PROGRAM).info_elf
22
  arm-none-eabi-size -d -B -t $(PROGRAM).elf > $(PROGRAM).info_size
23
  arm-none-eabi-objdump -S $(PROGRAM).elf > $(PROGRAM).info_code
24
  arm-none-eabi-nm -t d -S --size-sort -s $(PROGRAM).elf > $(PROGRAM).info_symbol
25
26
.PHONY: libs app clean tshow
27
  
28
libs:
29
  $(MAKE) -C libs $@
30
31
app:
32
  $(MAKE) -C app $@
33
34
clean:
35
  $(MAKE) -C app $@
36
  $(MAKE) -C libs $@
37
  rm -f $(PROGRAM).elf $(PROGRAM).hex $(PROGRAM).bin $(PROGRAM).info_elf $(PROGRAM).info_size
38
  rm -f $(PROGRAM).info_code
39
  rm -f $(PROGRAM).info_symbol
40
41
tshow:
42
  @echo "######################################################################################################"
43
  @echo "################# optimize settings: $(InfoTextLib), $(InfoTextSrc)"
44
  @echo "######################################################################################################"

makefile des Ordners app:
1
#src Makefile
2
3
include ../Makefile.common
4
5
OBJS+=startup.o
6
OBJS+=main.o
7
OBJS+=button.o
8
OBJS+=game.o
9
OBJS+=player.o
10
OBJS+=sevenSegment.o
11
OBJS+=shotGlass.o
12
OBJS+=stm32f10x_it.o
13
14
15
16
all: src
17
18
app: app.a
19
20
app.a: $(OBJS)
21
  $(AR) cr app.a $(OBJS)
22
23
.PHONY: src clean tshow
24
25
clean:
26
  rm -f app.a $(OBJS)
27
  
28
tshow:
29
  @echo "######################################################################################################"
30
  @echo "################# optimize settings: $(InfoTextLib), $(InfoTextSrc)"
31
  @echo "######################################################################################################"

von Detlef K. (adenin)


Lesenswert?

Das ist das dritte Clarkesche Gesetz „Jede hinreichend fortschrittliche 
Technologie ist von Magie nicht zu unterscheiden.“ ;)

von 900ss (900ss)


Lesenswert?

Simon Huwyler schrieb:
> Woher zum Geier weiss der, wie er die
> object files erzeugen soll?

Es gibt in Make vordefinierte Regeln. Diese sorgen für das Compilieren.
Diese Regeln kann man sich auch ausgeben lassen mit einer bestimmten 
Kommandozeilenoption für Make. Habe gerade vergessen, wie die Option 
war. Mußt mal das Manual fragen.

von Nase (Gast)


Lesenswert?

1
make -p -f/dev/null

von Simon H. (simi)


Lesenswert?

Detlef Kunz schrieb:
> Das ist das dritte Clarkesche Gesetz „Jede hinreichend fortschrittliche
> Technologie ist von Magie nicht zu unterscheiden.“ ;)

:-D


@alle:

Vielen Dank! Jetzt bin ich schon wieder ein bisschen klüger geworden! 
:-)
Schön finde ich dieses implizite Default-Zeug aber nicht. Aber 
tatsächlich: Sobald ich eine Regel für Object-Files anfüge, nimmt er 
die. Cool!

Gruäss
Simon

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.