【发布时间】:2012-01-02 08:52:02
【问题描述】:
我有一个包含多个 c 和 h 文件的 C 程序。我决定将程序的一部分设为“仅标题”,因此我将代码从 c 移至 h。现在我遇到了多重定义问题,我不知道为什么。例如:
main.c includes utils.h
vector.c includes utils.h
我将 utils.c 中的所有内容都移到了 utils.h(当然还从项目中删除了 utils.c)。 utils.h 以
开头#ifndef UTILS_H_
#define UTILS_H_
// and end with:
#endif
为了确保我的后卫是独一无二的,我尝试更改它(例如:UTILS718171_H_)但它不起作用。
编译器仍然抱怨:
/tmp/ccOE6i1l.o: In function `compare_int':
ivector.c:(.text+0x0): multiple definition of `compare_int'
/tmp/ccwjCVGi.o:main.c:(.text+0x660): first defined here
/tmp/ccOE6i1l.o: In function `compare_int2':
ivector.c:(.text+0x20): multiple definition of `compare_int2'
/tmp/ccwjCVGi.o:main.c:(.text+0x6e0): first defined here
/tmp/ccOE6i1l.o: In function `matrix_alloc':
ivector.c:(.text+0x40): multiple definition of `matrix_alloc'
/tmp/ccwjCVGi.o:main.c:(.text+0x0): first defined here
...
问题可能是这样的:所有的 c 文件都被编译并获得他们自己的代码版本,然后在链接时它会导致问题,但老实说我不知道如何解决这个问题。
【问题讨论】:
-
尝试发布 utils.h。请记住只将原型或常量放在 utils.h 中
-
question 2174657 提供了一些关于何时/如何使用仅标头库的信息。
标签: c include multiple-definition-error