Makefile Error in original 2 file

Source: Internet
Author: User

Never system to see Makefile documents, usually belong to the copy template, used to forget, the afternoon to try to write their own understanding of the simplest makefile, including 2. c files, 1. h files, for a couple of hours, refer to other people's articles to get out, special records.

MAIN.C:

1#include <stdio.h>2#include"command.h" 3 4 intMainintargcConst Char*argv[])5 {6printf"Run in main\n");7 command ();8         return 0;9}

COMMAD.C:

1 #include <stdio.h>2"command.h"34void command (void)5{6         printf ("run in command\n"); 7 }

Commad.h:

1 #ifndef __command_h__ 2 #define __command_h__34void COMMAND (void); 5 6 #endif

Makefile

1objects =MAIN.O COMMAND.O2 3 main:$ (objects)4Gcc$ (objects)-o main5 6 main.o:main.c command.h7     GCCMAIN.C-o main.o8 9 COMMAND.O:COMMAND.C command.hTen     GCCCOMMAND.C-o command.o One  A  - . Phony:clean - Clean : the     RMMain $ (objects)

After make, error:

GCC Main.c-o MAIN.O
/TMP/CCTBTS4T.O: In the function ' main ':
MAIN.C: (. text+0x16): Reference not defined for ' command '
Collect2:error:ld returned 1 exit status
Make: * * [MAIN.O] Error 1
So began my first misunderstanding, my makefile error in line 6-7, when executing the build target MAIN.O, I see the error, that is my target dependency should include COMMAD.O, because my main.c called command.c inside the function. So I added dependency commad.o for MAIN.O,

1objects =MAIN.O COMMAND.O2 3 main:$ (objects)4     cc$ (objects)-o main5 6 main.o:main.c COMMAND.O command.h7         GCCMAIN.C COMMAND.O-o main.o8 9 COMMAND.O:COMMAND.C command.hTen         GCCCOMMAND.C-o command.o One  A  - . Phony:clean - Clean : the     RMMain $ (objects)

See 6-7 lines, feel super wrong, leave no matter, make again, due to dependency, derivation execution to COMMAND.O:

gcc command.c-o command.o
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 0 has invalid symbol index 11
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 1 has invalid symbol index 12
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 2 has invalid symbol index 2
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 3 has invalid symbol index 2
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 4 has invalid symbol index 11
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 5 has invalid symbol index 13
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 6 has invalid symbol index 13
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 7 has invalid symbol index 13
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 8 has invalid symbol index 12
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation 9 has invalid symbol index 13
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 13< /c0>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation One has invalid symbol index 13< /c2>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 13< /c4>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation to invalid symbol index 13< /c6>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation + has invalid symbol index 13< /c8>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 13< /c0>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 13< /c2>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation, has invalid symbol index 13< /c4>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 13< /c6>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 13< /c8>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 13< /c0>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_info): Relocation has invalid symbol index 22< /c2>
/usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o (. debug_line): Relocation 0 has invalid symbol index 2
/usr/lib/gcc/i686-linux-gnu/4.8/. /.. /.. /I386-LINUX-GNU/CRT1.O: In the function ' _start ':
(. text+0x18): Reference not defined for ' main '
Collect2:error:ld returned 1 exit status
Make : * * [COMMAND.O] Error 1

See/USR/BIN/LD, yes, is the link operation, that is, the final step to generate the executable file, the problem is I just want to COMMAND.C to generate a target file COMMAND.O. It is doubtful that GCC command.c-o command.o this format is incorrect. Recall the next executable generation process: precompilation, compilation (only for Advanced assembly language), compiling, linking. And I just want it to stop after compiling. To split these steps:

Pre-compilation:

  GCC-E A.c-o A.I

Assembly:

  Gcc-s A.i-o A.S

Compile:

Gcc-c A.s-o A.O

Link:

GCC A.o-o A

In fact, the last link can be subdivided, for example, here will be COMMAND.O MAIN.O, libc library (I use printf), there is a link loading library, linked to an executable file.

So my mistake missed the "-C", to fill in:

1objects =MAIN.O COMMAND.O2 3 main:$ (objects)4     cc$ (objects)-o main5 6 main.o:main.c command.h7         GCC-C MAIN.C-o main.o8 9 COMMAND.O:COMMAND.C command.hTen         GCC-C COMMAND.C-o command.o One  A  - . Phony:clean - Clean : the     RMMain $ (objects)

It is not necessary to call the function inside the COMMAND.C in MAIN.C because the call command () function in MAIN.O will leave a symbol table in the MAIN.O file until the last link is made to determine its specific value.

Makefile Error in original 2 file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.