make error: multiple definitions of... despite include guard

make error: multiple definitions of... despite include guard

我的编译器给我一些关于函数的多个定义(或者更确切地说是特定 .c 文件中的所有函数)的非常奇怪的错误。

我正在使用 include guards 来防止多重声明

我的 header 文件没有定义,只有声明。文件中没有定义变量,只有函数。

I double-checked: 其他文件只包含.h文件。 .c 文件未包含在任何地方。

我正在使用相同的方法来包含我的其余代码,我没有遇到任何问题。因此,我不能给出我的问题的最小示例,因为我看不到正在工作的文件和导致 compiler-problems.

的文件之间没有(相关)差异

在 git 中合并两个分支后首次出现错误。在合并之前,这两个分支中的每一个都可以顺利编译。

现在,我很高兴能得到任何关于此问题可能来自何处的提示。

如果有人需要,我也会提供我的代码and/or error-logs。虽然这是一个相当长的代码,所以如果您需要它,请随时询问

make 的错误信息是

错误信息

src/rhs.o: In function `calculate_rhs':
/home/user/Desktop/WS5/src/rhs.c:15: multiple definition of `calculate_rhs'
src/rhs.o:/home/user/Desktop/WS5/src/rhs.c:15: first defined here
src/rhs.o: In function `evaluate_rhs1':
/home/user/Desktop/WS5/src/rhs.c:103: multiple definition of `evaluate_rhs1'
src/rhs.o:/home/user/Desktop/WS5/src/rhs.c:103: first defined here
src/rhs.o: In function `evaluate_rhs2':
/home/user/Desktop/WS5/src/rhs.c:140: multiple definition of `evaluate_rhs2'
src/rhs.o:/home/user/Desktop/WS5/src/rhs.c:140: first defined here
collect2: error: ld returned 1 exit status
make: [exec] Error 1 (ignored)

rhs.o 的编译器命令是

/usr/lib/petscdir/3.6.0/arch-linux2-c-debug/bin/mpicc -o src/rhs.o -c -fPIC -Wall -Wwrite-strings -Wno-strict-aliasing -Wno-unknown-pragmas -g3 -O0  -std=c99 -I/usr/lib/petscdir/3.6.0/include -I/usr/lib/petscdir/3.6.0/arch-linux2-c-debug/include    `pwd`/src/rhs.c

.h 文件在这里:http://pastebin.com/Za37iWr7

.c 文件在这里:http://pastebin.com/2mSzdZvT

上面的.c文件包含了.h文件,main-function

所有其他包含(rhs.c)文件的 header 是:

生成文件: http://pastebin.com/LDj1e7xB

提前致谢:)

很可能您的 makefile 或构建脚本在合并中搞砸了,它链接了同一个文件 (rhs.o) 两次。

问题出在 make 文件中。

注意:'all'应该是第一个目标,而不是第二个

注意:每个不使用目标名称创建文件的目标都应包含在 .PHONY: 目标中

注意:宏应该用“:=”而不是“=”定义,因为“=”会导致宏在每次调用时重新计算,而“:=”会导致宏只被评估一次。

注意:像这样写依赖文件的include:

(扩展 'ifneq' 以包含 'clean_all' 和 'cleaning' 以及 'clean_doc'

# wrap with ifneg... so will not rebuild *.d files when goal is 'clean_all', etc
#
ifneq "$(MAKECMDGOALS)" "clean_all"
-include $(OBJ:.o=.d)
-include $(TEST_OBJ:.o=.d)
endif

这是问题的根源:

OBJ = $(SRC)/main.o\
  $(SRC)/helper.o\
  $(SRC)/init.o\
  $(SRC)/basis.o\
  $(SRC)/gauss.o\
  $(SRC)/indices.o\
  $(SRC)/eval.o\
  $(SRC)/rhs.o\     <-- first instance of rhs.o
  $(SRC)/visual.o\
  $(SRC)/matrix.o\
  $(SRC)/rhs.o      <-- second instance of rhs.o

和这条规则:

exec:  $(OBJ) chkopts
    -$(CLINKER) -o sim $(OBJ) $(PETSC_KSP_LIB)

包含 $(OBJ) 的扩展,其中 $(OBJ) 包含上述错误