目前正在尝试在Debian上使用GCC,以及makefile,因为我正在创建一个标题。每当我尝试“make”makefile时,我都会收到如下错误:
setup.o:在功能'setup'中:
setup.c :(。text + 0x0):`setup'的多重定义
finalkek.o:finalkek.c :(。text + 0x0):首先在这里定义
collect2:ld返回1退出状态
make:*** [projExec]错误1
我的makefile如下所示:
projExec: finalkek.o setup.o
gcc -o projExec finalkek.o setup.o
finalkek.o: finalkek.c setup.h
gcc -c finalkek.c
setup.o: setup.c
gcc -c setup.c
finalkek.c
是我的主文件,setup
是我的标题。
在我的主文件中,这是我唯一提到它的时间:
include "setup.h" // Using the double quotes for a custom header...
void main()
{
setup();
rest of code here...
}
在我的标题文件setup.h
中,我有这样的话:
void setup()
{
rest of code here...
}
答案 0 :(得分:0)
我注意到的一些事情:虽然技术上允许,但在头文件中实现整个函数是有缺陷的做法。头文件仅用于原型(即void setup(void);
而不是整个void setup(void) { ... }
)。你的setup.c中有什么?此外,Make不应该像这样工作。
finalkek.o: finalkek.c setup.h
gcc -c finalkek.c
你不应该直接编译头文件,因为它不应该在其中有实际的实现,只是原型。这就是预处理器所做的,使用指令#include
,它获取指定头的全部内容,并将其放在C文件中。因此,通过告诉Make编译setup.h,您将在项目中包含该文件的内容两次,这可能会给您错误。
就像其他人所说的那样,将适当的函数setup()
的实际代码移动到setup.c。 setup.h应如下所示:
#ifndef SETUP_H
#define SETUP_H
void setup(void);
#endif
行#ifndef SETUP_H, #define SETUP_H
和#endif
是标题文件的格式化工具,可防止您多次包含同一文件。
然后是随附的setup.c:
#include "setup.h"
void setup(void) {
// your code here
}
finalkek.c:
int main(void) {
setup();
// rest of code here
}