You are not logged in.
I'm new to make and I'm trying to figure out how to write makefiles. I've got a simple "Hello, World" program in a file called hello.c, which contains nothing but this:
#include <stdio.h>
int main (int argc, char *argv[])
{
printf ("Hello, world\n");
return 0;
}
I have a makefile which contains only this:
all: bkf
bkf: hello.o
gcc hello.o -o bkf
hello.o: hello.c
gcc -Wall hello.c -o hello.o
clean:
rm -rf *.o
Running "make hello.o" works, running "make clean" works, but just "make" returns this:
hello.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crt1.o:(.text+0x0): first defined here
hello.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crti.o:initfini.c:(.fini+0x0): first defined here
hello.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
hello.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crt1.o:(.data+0x0): first defined here
hello.o: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/crtbegin.o:(.data+0x0): first defined here
hello.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crti.o:initfini.c:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
hello.o:(.dtors+0x8): first defined here
/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
/usr/bin/ld: error in hello.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
Can someone tell me what's going on here and why it won't link? I've been through the gcc and ld man pages, every make tutorial on the net I can find, and I can't find anything wrong with it.
(I realise explicit linking is hardly necessary for a single-file program. I'm just trying to learn the makefile process here.)
EDIT: I somehow solved this... I previously had the -c option in the linking command in the makefile (per the tutorials). I kept getting the above errors, so I took it out. I put it back in, and all of a sudden it works, everything compiles and links. I really don't know what the difference is. Does the order of options make a difference? E.g., "gcc -c -Wall" vs "gcc -Wall -c"? That's the only thing I can think of.
Last edited by ibrunton (2011-08-21 15:16:12)
Offline
The gcc man page say for the "-c option":
"...the -c option says not to run the linker."
To *compile*, source file to object file (.c -> .o):
gcc COMPILER_FLAGS_HERE -c SOURCE_FILES
note: the position of the "-c" option doesn't matter, it just tell gcc to not try to link.
To *linking*:
gcc LINKER_FLAGS_HERE [-o EXECUTABLE_NAME] OBJECTS...
Sorry in advance for my poor english...
Offline
Just completing what Chippeur said. First you have to compile all your *.c files, then you link everything. Pay attention to the order of flags and source files. In this case your makefile looks like it:
Ps: I add an extra option by myself to let you run your application (make run)
run: all
./bkf
all: bkf
bkf: hello.o
gcc hello.o -o bkf
hello.o: hello.c
gcc -Wall -c hello.c
clean:
rm -rf *.o
Offline