How do I create a Makefile in C

3

I'm having trouble running the makefile.

main:   ex09.o  funcao1.o
    gcc -c ex09.o   funcao1.o -o main

ex09.o: ex09.c
    gcc -c ex09.c

funcao1.o:      funcao1.c   funcao1.h
    gcc -c funcao1.c

clean:  
    rm *.o

One of the linker errors that is generated

gcc -c ex09.o   funcao1.o -o main
gcc: warning: ex09.o: linker input file unused because linking not done
gcc: warning: funcao1.o: linker input file unused because linking not done
    
asked by anonymous 01.10.2016 / 20:41

2 answers

4

This is wrong here

main:   ex09.o  funcao1.o
    gcc -c ex09.o   funcao1.o -o main ### BAD

Because "-c" and "-o" do not make sense: "-c" says to only compile and not create the executable, however "-o" says to create the executable.

The right thing to do is:

main:   ex09.o  funcao1.o
    gcc ex09.o   funcao1.o -o main ### GOOD

without the "-c".

    
01.10.2016 / 22:27
1

Your problem is not exactly in the Makefile. The file has the correct syntax, indicating the dependencies, the targets, and how to generate them. The problem is in the command to generate the final executable, relative to the flag you are passing to gcc.

In the first line, which is responsible for linking the final executable, you are passing the -c flag, which is to ask gcc to only compile the source file, without linking. In that case you just want the opposite.

So just remove the -c from the first line of your Makefile so it should work as you expect.

    
01.10.2016 / 22:19