Makefile:n#39; _start'的多重定义

时间:2015-03-31 17:07:00

标签: c makefile

我一直在寻找答案,但我所看到的都不是我自己的makefile存在的问题。

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

和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

2 个答案:

答案 0 :(得分:1)

make正在执行以下操作:

  1. bumper_cars.c编译为已定义main的目标代码。
  2. 然后它将sleeper.c编译成目标代码,该代码也定义了main
  3. 然后make将两个对象组合起来形成二进制文件,问题是链接器因为重复main函数而抱怨。

    在任一文件中注释或#ifdef删除该功能,然后重新发布make

答案 1 :(得分:1)

以下是您可能想要了解的关于make实用程序的所有内容的链接:http://www.gnu.org/software/make/manual/make.html

发布的make文件有几个oops。其中一个oops是几个* .c文件不会生成单个.o文件,而是几个.o文件。

库文件仅用于链接步骤头文件仅用于以下编译步骤,几乎所有编译器警告都已启用。

我建议使用这个:

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