0

I am having error with makefile, when im trying to compile executable with .o files Error list:

g++ -g -Wall -o main main.o  `pkg-config --cflags --libs gtkmm-3.0`
main.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o:(.text+0x0): first defined here
main.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crti.o:(.fini+0x0): first defined here
main.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
main.o: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o:(.data+0x0): first defined here
main.o: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/crtbegin.o:(.data+0x0): first defined here
main.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
main.o:(.data+0x10): first defined here
/usr/bin/ld: error in main.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
makefile:12: recipe for target 'main' failed
make: *** [main] Error 1

and makefile is

NAME=main
CFLAGS=-g -Wall -o $(NAME)
LIBFLAGS=`pkg-config --cflags --libs gtkmm-3.0`
SRCS=main.cpp
CC=g++
OBJECTS=$(SRCS:.cpp=.o) 
all: main
main: $(SRCS)
    $(CC) $(CFLAGS) $(OBJECTS) $(LIBFLAGS)
.cpp.o:
        $(CC) $(CFLAGS)  $< -o $@

Can someone explain my errors? I know that this isn't the best makefile.

TileHalo
  • 1
  • 1

1 Answers1

1

You have to be sure you use the -c flag on all compile lines which create object files. Without this flag, the compiler front-end (g++) will create an executable, not an object file.

In your example the suffix rule .cpp.o is creating a file named main.o, but it's actually linking that into an executable. If you ran ./main.o, it would work. Then when you try to link that executable into another executable named main, badness happens.

Change your rule:

.cpp.o:
        $(CC) $(CFLAGS) -c -o $@ $<
MadScientist
  • 92,819
  • 9
  • 109
  • 136