这是我的头文件。多个定义,即使我使用extern和包括警卫
#ifndef P6_H
#define P6_H
#include <stdio.h>
void FoundationC();
void StructureC();
void PlumbingC();
void ElectricC();
void HVACC();
void SheatingC();
extern int DAYS;
#endif
我正在使用makefile来执行所有编译。它能够编译单个的.o文件文件,但是当它试图将这些文件转换为单个可执行文件时,它会说变量DAYS有多个定义,即使它是extern并且在每个单独声明和初始化。我得到这个工作之前,但无法弄清楚为什么现在不工作。 哦,这里是我的makefile代码
all:
gcc -c P6.c
gcc -c foundations.c
gcc -c structure.c
gcc -c plumbing.c
gcc -c electric.c
gcc -c hvac.c
gcc -c sheating.c
gcc P6.h P6.o foundations.o structure.o plumbing.o electric.o hvac.o sheating.o -o P6
我意识到P6.h可能没有被包括在该命令,但包括警卫应该让没有关系没有?
而且我很抱歉,如果这个问题是一个傻瓜,但我没有以前寻找答案,这个问题让我疯了在个人层面上,尽管这是学校的事实。
这是我得到的错误。
gcc -c P6.c
gcc -c foundations.c
gcc -c structure.c
gcc -c plumbing.c
gcc -c electric.c
gcc -c hvac.c
gcc -c sheating.c
gcc P6.h P6.o foundations.o structure.o plumbing.o electric.o hvac.o sheating.o -o P6
structure.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
plumbing.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
electric.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
hvac.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
sheating.o:(.data+0x0): multiple definition of `DAYS'
foundations.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
你在这里显示的没有变量'DAYS'的任何定义。所以很难知道问题是什么,因为你没有显示任何可能导致问题的东西。不,你不应该编译P6.h.这不是它的工作原理。 – Art
“...这是外部的,并在每个单独声明和初始化”。努力理解你的英语,但这是否意味着你在多个.c文件中定义了这个变量?如果是这样,那就是错误的原因。 – Lundin
使用原型声明,而不是旧的K&R风格。自25年以来它们已经过时。 – Olaf