2015-03-31 98 views
0

我已经四处搜寻了答案,但没有看到我自己的makefile存在的问题。Makefile:'_start'的多重定义

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 

而生成文件:

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 [email protected] -c 

bumper_cars: objects.o 
    $(CC) $^ -o [email protected] 

clean: 
    rm -f bumper_cars.o 
    rm -f bumper_cars 
+1

我认为你需要发布soruce文件。 – 2015-03-31 17:11:28

+0

我认为问题在于,您没有使用#ifndef _HEADER_FILE_NAME – 2015-03-31 17:12:43

+0

Adapt [this example](http://stackoverflow.com/a/14180540/841108)来定义您的头文件以满足您的需求....使用'CC = gcc'和'.c'代替'.cpp'后缀 – 2015-03-31 17:13:26

回答

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 [email protected] $(LIBS) 

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


clean: 
    $(RM) *.o 
    $(RM) bumper_cars 
+0

我不知道为什么这会得到downvoted,但它解决了我的问题!问题是 - 什么是 - 超刨花和 - 我。旗帜呢? – Rui 2015-03-31 17:58:21

1

make是做以下:

  1. 编译bumper_cars.c成具有限定main目标代码。
  2. 然后它将sleeper.c编译成目标代码,该目标代码也定义了main

然后将两个对象结合起来形成二进制,问题是链接器因为重复的main函数而抱怨。

要么注释掉,要么#ifdef取出任一文件中的功能,然后再次发出make

+0

“sleeper.c”中的main()是在评论块。 – 2015-03-31 17:48:27

+0

是的,但问题不是主要的,而是'_start'符号。 – 2015-03-31 17:52:17