-1

I have 3 header files and 4 .c files in my c program,which are code1.h code1.c,code2.h code2.c,code3.h code3.c,and main.c

The main function was defined in main.c

The relationships between files are: main.c includes code1.h, code1.h includes code2.h code2.h includes code3.h

I implemented a make file to compile and link them together.

But when the make file executed,it shows an error:

"_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

here is my make file:

objects= main.o code1.o middle.o
executable:$(objects)
     gcc-o  $(objects) -o executable

main.o: main.c
     gcc -c main.c 

code1.o:code1.h code1.c
     gcc -c code1.c

middle.o:code2.o code3.o
     gcc -o code2.o code3.o  -o middle.o

code2.o:code2.c code2.h
     gcc -c code2.c
code3.o:code3.h code3.c
     gcc -c code3.c
user3788871
  • 25
  • 1
  • 4
  • 10
  • If the error is related to your makefile, please post your gcc invocation. –  Sep 11 '14 at 16:46
  • what's the question? – thang Sep 11 '14 at 16:48
  • 1
    shouldn't that be `main.o` instead of `main1.o`? – Aniket Inge Sep 11 '14 at 16:54
  • That rule for `middle.o` is wildly incorrect. The rule for `executable` appears to be trying to run `gcc-o` (which strikes me as odd). You also didn't tell us which of those bizarre/broken rules is the one that is running and causing the error (though I expect that it is the `middle.o` rule). Why do you even have `middle.o`? – Etan Reisner Sep 11 '14 at 16:56
  • @Aniket If he wants it to be used given that makefile, yes. That being said it isn't necessary at all since that is just a poorer version of a built-in rule that make already has (and the rule that make will be using currently to build `main.o` given that makefile above). – Etan Reisner Sep 11 '14 at 16:58
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Niall Sep 12 '14 at 07:06

2 Answers2

1

your makefile has some errors:
corrections :
#incorrectly placed main.o instead of main1.o

objects : main1.o code1.o middle.o    

#rule for middle.o is incorrect.
correction -

middle.o : code2.o code3.o    
        gcc -o middle.o code2.o code3.o    

#error in rule for executable : correction -

  executable : $(objects)
        gcc $(objects) -o executable

Reference for more detail on gnu Makefiles

http://www.gnu.org/software/make/manual/make.html
Abhishek Choubey
  • 883
  • 6
  • 16
  • I fixed the second problem, and actually the first one you said above is correct in my original code. But nothing changed after fixing it. – user3788871 Sep 11 '14 at 17:14
  • @user3788871 - error is still there in rule for "executable" correction : executable:$(objects) gcc $(objects) -o executable – Abhishek Choubey Sep 11 '14 at 17:20
  • If you fixed your makefile locally then fix it in your post to match. We cannot help you if we don't have the actual file. – Etan Reisner Sep 11 '14 at 17:26
  • @user3788871 - check out this link on SO, hope it helps http://stackoverflow.com/questions/8439664/what-does-the-the-compile-time-error-undefined-symbols-for-architecture-x86-64 – Abhishek Choubey Sep 11 '14 at 17:33
  • @user3788871 - To my knowledge, its a linker error. It means that you are calling a function for which you have not given any definition. – Abhishek Choubey Sep 11 '14 at 17:37
0

The variable objects have the .o files like main.o code1.o middle.o. But the 1st target is wrongly named as main1.o. So the main.c file will not be compiled.

mahendiran.b
  • 1,315
  • 7
  • 13
  • 3
    No, it will be compiled by the built-in `%.c: %.o` rule. You don't even need a makefile for make to be able to do that. Create a directory with just an empty file `foo.c` in it and then run `make foo.o` from that directory to see what I mean. – Etan Reisner Sep 11 '14 at 16:59
  • I don't think that is the problem. The files names I posted are modified,and my original make file doesn't have this kind of error. – user3788871 Sep 11 '14 at 17:08