0

My makefile has a problem.

eos$ make
gcc objects.o -o bumper_cars
objects.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.text+0x0): first defined here
objects.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o:(.fini+0x0): first defined here
objects.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.rodata.cst4+0x0):     first defined here
objects.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o:(.data+0x0): first defined here
objects.o:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtbegin.o:(.rodata+0x0): first defined here
objects.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtend.o:(.dtors+0x0): multiple definition of     `__DTOR_END__'
objects.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 objects.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status

And the makefile:

CC =  gcc
CFLAGS = -Wall -std=c99 -g -pthread

all: objects.o bumper_cars

objects.o: bumper_cars.c sleeper.c sleeper.h
    $(CC) $(CFLAGS) $^ -o $@ -c

bumper_cars: objects.o
    $(CC) $^ -o $@

clean:
    rm -f bumper_cars.o
    rm -f bumper_cars
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rui
  • 55
  • 1
  • 6
  • 1
    I think you need to post the soruce files. – Iharob Al Asimi Mar 31 '15 at 17:11
  • I think the problem is that you have not defined your header file with #ifndef _HEADER_FILE_NAME – Yasir Majeed Mar 31 '15 at 17:12
  • Adapt [this example](http://stackoverflow.com/a/14180540/841108) to suite your needs .... use `CC=gcc` and `.c` instead of `.cpp` suffix – Basile Starynkevitch Mar 31 '15 at 17:13
  • Does this happen if you run `make clean` and then run `make` again? Can you get the full `make` output from that run? Does removing `sleeper.h` from the `objects.o` prerequisites help anything? – Etan Reisner Mar 31 '15 at 17:15
  • Added the source files. Make clean/make does not do anything unfortunately. And I will look at that example, thank you, Basile. – Rui Mar 31 '15 at 17:18
  • 1
    And you're sure sleeper.c was compiled after you commented out main()? – clearlight Mar 31 '15 at 17:24
  • 1
    Please remove all the .o files and show the output when make is run in a clean environment. – clearlight Mar 31 '15 at 17:25
  • The output is the exact same after cleaning and remaking. And I'm not entirely sure what you mean, but I tried commenting out main and got this error instead: eos$ make ; gcc -Wall -std=c99 -g -pthread bumper_cars.c sleeper.c sleeper.h -o objects.o -c ; gcc: cannot specify -o with -c or -S with multiple files – Rui Mar 31 '15 at 17:32
  • for others with a similar problem, check if you are using the -o flag correctly – brita_ Jan 21 '17 at 13:34

2 Answers2

1

make is doing the following:

  1. Compiles bumper_cars.c into an object code which has main defined.
  2. Then it compiles sleeper.c into an object code, which also, has main defined.

Then make combines both objects to form the binary, the problem is the linker is complaining because of the duplicate main functions.

Either comment out or #ifdef out the function in either file and re-issue make again.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
1

Here is a link to everything you may want to know about the make utility: http://www.gnu.org/software/make/manual/make.html.

The posted make file has a few oops. One of those oops is that several *.c files will not make a single .o file but rather will be several .o files.

Library files are only used in the link step header files are only used in the compile step in the following, almost all compiler warnings are enabled.

I suggest using this:

CC     :=  gcc
CFLAGS := -Wall -Wextra -pedantic -std=c99 -g
LIBS   := -lpthread
RM     := rm -f

.PHONY: all clean

NAME := bumper_cars
SRCS := $(wildcard *.c)
OBJS := $(SRCS:.c=.o)

all: $(OBJS) $(NAME)

#
# link the .o files into the target executable
#
$(NAME): $(OBJS)
    $(CC) $^ -o $@ $(LIBS)

#
# compile the .c file into .o files using the compiler flags
#
%.o: %.c sleeper.h
    $(CC) $(CFLAGS) -c $< -o $@ -I.


clean:
    $(RM) *.o
    $(RM) bumper_cars
Martin Törnwall
  • 9,299
  • 2
  • 28
  • 35
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • I don't know why this got downvoted but it fixed my problem! Question though-- what do the -Wextra -pedantic and -I. flags do? – Rui Mar 31 '15 at 17:58