How do I create a C Makefile

Asked

Viewed 2,311 times

2

I’m having problems running 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 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

2 answers

4


This is all wrong

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

because "-c" and "-o" at the same doesn’t make sense: "-c" says to only compile and not create the executable, however "-o" says to create the executable.

That’s the way it is:

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

without the "-c".

  • Now the error that appears to me is this : gcc ex09.o funcao1.o -o main funcao1.o: In Function sort_without_resp': funcao1.c:(.text+0xa8): Multiple Definition of sort_without_resp' ex09.o:ex09.c:(.text+0xa): first defined here collect2: error: Ld 1 Exit status make: **[main] Error 1 - @Joseph X.

  • 2

    @ José X: To be precise, the -o serves to name the generated file. You can use the -c to only compile a source file and at the same time -o to indicate the name of the object file. But I don’t know any practical use for this.

  • 1

    @C. E. Gesser is, I "simplified" a little bit.

1

Your problem is not exactly in the Makefile. The file is with the correct syntax, indicating the dependencies, 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, responsible for linking the final executable, you are passing the flag -c, that serves to ask gcc to just Compile the source file, without linking. In this case you want just the opposite.

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

  • Now the error that appears to me is this : gcc ex09 function1.o -o main function1.o: In Function sort_without_resp':
funcao1.c:(.text+0xa8): multiple definition of sort_without_resp' ex09.c:(.text+0xa): first defined here collect2: error: Ld returned 1 Exit status make: *** [main] Error 1 - @ C.E.Gesser

  • @Programmer Now seems to be another problem, unrelated to this issue. It seems that a function of yours is defined in more than one compilation unit. This can happen if, for example, you write the body of the function in the file . h.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.