I need help to create the makefile

0
# Baseando-se no link: https://www.embarcados.com.br/introducao-ao-makefile/

all: bancoBPE

bancoBPE: main.o
    gcc -o bancoBPE main.o -Wall -Wextra -Werror -Wpedantic

# Não sei direito como compilar esse structs.h sozinho sem ter structs.c

structs.o: structs.h
    gcc -o filacirc.o filacirc.c -Wall -Wextra -Werror -Wpedantic

filacirc.o: filacirc.c filacirc.h
    gcc -o filacirc.o filacirc.c -Wall -Wextra -Werror -Wpedantic

pilhaseq.o: pilhaseq.c pilhaseq.h
    gcc -o pilhaseq.o pilhaseq.c -Wall -Wextra -Werror -Wpedantic

lista.o: lista.c lista.h
    gcc -o lista.o lista.c -Wall -Wextra -Werror -Wpedantic

clean:
    rm -rf *.o *~ bancoBPE

The following error occurred in the terminal:

$ make
gcc -o bancoBPE main.o -Wall -Wextra -Werror -Wpedantic
/usr/bin/ld: main.o: relocation R_X86_64_32 against '.rodata' can not be         used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
makefile:5: recipe for target 'bancoBPE' failed
make: *** [bancoBPE] Error 1

If someone can pass on material so that I can study better, I would appreciate it. I'm trying to understand right how the makefile works, but still not much success despite reading some references.

    
asked by anonymous 05.12.2017 / 19:24

1 answer

3

Makefiles have a very simple structure:

alvo: ingredientes
    regra

Where alvo is the name of the file that will be created by regra and depends on ingrediantes (files).

A Makefile always has a main recipe (the first rule), as in:

bancoBPE: main.o structs.o pilhaseq.o lista.o filecirc.o
    $(CC) -o $@ $^

Here, $ @ is replaced by alvo and $ ^ by all ingredientes . The CC variable is the C compiler (here used as a linker).

Based on the ingredients, make decides which other recipes should be avail- able ... If main.o does not exist, it will use the revenue to construct it, for example, or if main.c , in the recipe where alvo is main.o has a timestamp newer than file main.o (the target), then the rule is executed:

main.o: main.c structs.h lista.h pilhaseq.h filecirc.h
    $(CC) $(CFLAGS) -c -o $@ $<

Here, $ < is the first item in the ingredientes list and the CFLAGS variable is used to inform the compiler options.

Fortunately make provides some shortcuts ... It knows how to compile .c files and the rule can be omitted. Above, we would only need:

main.o: main.c structs.h lista.h pilhaseq.h filecirc.h

make still allows "fake" targets, such as:

all: bancoBPE

clean:
    rm -f *.o *~ bancoBPE

But it is prudent that if we are using GNU make , let's also use the pseudo-recipe:

.PHONY: all clean

To tell make that neither or clean are files. So you can call make with:

$ make all  # all é assumido, se não informado
$ make clean

I find the use of all useful only in cases where there are two or more "primary" targets.

Make also defines the CC and CFLAGS variables, respectively, "cc" and "" (empty string), but you can change them at the beginning of the Makefile file:

CC=x86_64-w64-mingw32-gcc
CFLAGS=-O2 -march=native -msse4.2
    
05.12.2017 / 22:57