Multiple Definition of a function in C, Prototyping

313 views Asked by At

Eclipse tells me that I have mutliple Definitions of a function. I just can't spot the mistake. This is my main.c

#include <stdio.h>
#include "kontaktverzeichnis.h"


int main(){

    kontakt_hinzufuegen();

    return 0;
}

This is the header:

#ifndef KONTAKTVERZEICHNIS_H_
#define KONTAKTVERZEICHNIS_H_
#include "kontaktfunktionen.c"

int kontakt_hinzufuegen();


#endif /* KONTAKTVERZEICHNIS_H_ */

and this is 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;

};

Where is my error?

3

There are 3 answers

0
Abhineet On BEST ANSWER

Do not #include anything in your header file. And do a #include "kontaktverzeichnis.h" in the kontaktfunktionen.c file.

As @StoryTeller commented, define your kontakt_hinzufuegen() as int kontakt_hinzufuegen() in the kontaktfunktionen.c file and return an int value from the function kontakt_hinzufuegen as for ex::

#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 ;

}
0
Marco On

Your error is that in kontaktfunktionen.h you are including kontaktfunktionen.c. This will include all the definitions and declarations from kontaktfunktionen.c which are already declared when you use kontaktfunktionen.c

As others have said: You should not include .c files in your header files.

0
unwind On

You're not supposed to #include C files, that's not the proper way to organize your code.

You should compile the C files separately and then link them together, or compile them all at once with a single compiler invocation.