Pedro Fernandes
Pedro Fernandes

Reputation: 198

Makefile doesnt create final executable

I have this makefile

CFLAGS=-g
LDFLAGS=-g

exec: fluxo.o validar.o ficheiroParaMemoria.o

fluxo.o: fluxo.c

validar.o: validar.c validar.h

ficheiroParaMemoria.o: ficheiroParaMemoria.c ficheiroParaMemoria.h

clean:
    rm -f run *.o

It creates the 3 .o files, but it's not creating the executable. Fluxo.c has main and nothing else. INvisibles show nothing but endline at the end of each line and a tabe before the clean command

Upvotes: 0

Views: 1340

Answers (3)

Chris Turner
Chris Turner

Reputation: 8142

Your Makefile doesn't contain any rules to build an executable. This rule is the closest - it creates a dependency between those 3 object files and a target called "exec".

exec: fluxo.o validar.o ficheiroParaMemoria.o

If you want to build "exec" from those 3 object files you need to expand the rule to also include the command to do just that.

exec: fluxo.o validar.o ficheiroParaMemoria.o
    $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

In the command $@ is used to indicate the target so would be "exec" in this case. $^ are the dependencies.

The reason why you don't need to add in commands to build the .o files is that make includes a builtin rule for building .o files from a .c file of the same name. There is also a builtin rule for building an executable from a .o or .c of the same name, but that isn't much use in this case as it wouldn't include the the other 2 .o files

Also, LDFLAGS is typically used to specify linking options not compilation options so you don't need the "-g" in it

Upvotes: 0

Pedro Fernandes
Pedro Fernandes

Reputation: 198

Fixed it. Needed to change the name "exec" to "fluxo" because that contains the main function

Upvotes: 0

Igor Galczak
Igor Galczak

Reputation: 142

Sorry i dont understand your Makefile, but here is works Makefile from my project:

CC = gcc
FLAGS = -std=c99
DEST_PATH = "./bin/main"


all: clean main.o Smtp.o Base64.o
    $(CC) build/*.o -o $(DEST_PATH) $(FLAGS)

main.o: src/main.cpp src/Service/Smtp.hpp
    $(CC) src/main.cpp -c -o build/main.o $(FLAGS)

Smtp.o: src/Service/Smtp.cpp src/Service/Smtp.hpp
    $(CC) src/Service/Smtp.cpp -c -o build/Smtp.o $(FLAGS)

Base64.o: src/Service/Base64.cpp src/Service/Base64.hpp
    $(CC) src/Service/Base64.cpp -c -o build/Base64.o $(FLAGS)

clean:
    rm -f build/*

-c flag create object file(.o).
In you example i dont see any real command except rm.
Hope this help.

Upvotes: 1

Related Questions