[英]Multiple Definition of a function in C, Prototyping
Eclipse告诉我,我有一个函数的多重定义。 我只是无法发现错误。 这是我的main.c
#include <stdio.h>
#include "kontaktverzeichnis.h"
int main(){
kontakt_hinzufuegen();
return 0;
}
这是标题:
#ifndef KONTAKTVERZEICHNIS_H_
#define KONTAKTVERZEICHNIS_H_
#include "kontaktfunktionen.c"
int kontakt_hinzufuegen();
#endif /* KONTAKTVERZEICHNIS_H_ */
这是kontaktfunktionen.c
#include <stdio.h>
kontakt[];
kontakt_hinzufuegen(){
int i = 0;
printf("Bisher sind %i Kontakte angelegt.",kontakt[i]);
kontakt[i++];
}
struct kontaktname{
char* name;
char* vorname;
};
struct kontaktanschrift{
char* strasse;
int hausnummer;
int plz;
char* ort;
char* land;
};
我的错误在哪里?
您不应该#include
C文件,这不是组织代码的正确方法。
您应该分别编译C文件,然后将它们链接在一起,或者通过一次编译器调用一次将它们全部编译。
您的错误是kontaktfunktionen.h
中包含kontaktfunktionen.c
。 这将包括使用kontaktfunktionen.c
时已经声明的kontaktfunktionen.c
所有定义和声明。
正如其他人所说:您不应该在头文件中包括.c
文件。
不要#include
头文件中包含任何内容。 并在kontaktfunktionen.c
文件中执行#include "kontaktverzeichnis.h"
。
正如@StoryTeller所评论的那样,在kontaktfunktionen.c
文件kontakt_hinzufuegen()
定义为int kontakt_hinzufuegen()
,然后从函数kontakt_hinzufuegen
返回一个int
值,例如:
#include <stdio.h>
#include "kontaktverzeichnis.h"
// define the type for this array as below
int kontakt[];
int kontakt_hinzufuegen(){
int i = 0;
printf("Bisher sind %i Kontakte angelegt.",kontakt[i]);
kontakt[i++];
// Return an int value
return 0 ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.