'Multiple defenitions of function
I'm making a program that has 3 files: 2.c, 2tele.c, tele.h
2.c
#include <stdio.h>
#include <stdlib.h>
#include "2tele.c"
int main(void){
TELEFONE tf = preenche_telefone(tf.numero, tf.nome);
mostra_telefone(tf);
}
2tele.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tele.h"
void mostra_telefone(TELEFONE tf){
printf("\nNumero = %d\nNome = %s", tf.numero, tf.nome);
}
TELEFONE preenche_telefone(char nome[], int numero){
TELEFONE tf;
printf("Introduza o numero\n");
scanf("%d", numero);
tf.numero = numero;
printf("Introduza o nome\n");
scanf("%s", nome);
strcpy(tf.nome,nome);
return tf;
}
tele.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct contacto{
int numero;
char nome[100];
}TELEFONE;
void mostra_telefone(TELEFONE tf);
In line 6 of 2tele.c I'm getting the error multiple definition of 'mostra_telefone', however I have read other posts and have also defined it on tele.h.
What should I do?
Edit: When i include "tele.h" instead of "2tele.c" it says that "TELEFONE tf = preenche_telefone(tf.numero, tf.nome);" has a bad initializer
Solution 1:[1]
In line 6 of
2tele.cI'm getting the error multiple definition of 'mostra_telefone',
That's a misleading claim. You might get such a message when you attempt to link the program, but that would be a function of the details of your build procedure. You appear to have cherry-picked a detail of the diagnostic that does not convey the nature of the problem. Always give us an exact copy of the full diagnostic message.
however I have read other posts and have also defined it on
tele.h.
No, you haven't. You have declared it in tele.h, which is in fact exactly what you should do. And every source file that references that function should #include the header, to get only a declaration, and avoid #includeing 2tele.c, which would provide a[n additional] definition. For functions, the difference is that a definition includes the function body.
Including the .c file instead of the .h file means that 2.c provides a definition of mostra_telefone(). If you compile 2.c to create an object file and also compile 2tele.c to create an object file, or if you compile both together to build a program directly, then each one provides a definition. It does not matter that the two definitions are lexically identical, or that they ultimately are derived from the same source file. There can be multiple declarations of the function, in the same or different translation units, but in the whole program, as compiled, there can be only one definition.
Solution: no file #includes 2tele.c. All that want to access the functions defined within instead declare those functions before use without defining them (except 2tele.c itself). The conventional way to do this is to #include a header containing the needed declarations -- in your case, that's tele.h.
But you say,
When i include "tele.h" instead of "2tele.c" it says that "TELEFONE tf = preenche_telefone(tf.numero, tf.nome);" has a bad initializer
Turn up your compiler's warning level. With gcc and other compilers that understand its options, for example, turn on at least -Wall. That should clue you in to the root of this problem:
2.c: In function ‘main’: 2.c:6:5: warning: implicit declaration of function ‘preenche_telefone’ [-Wimplicit-function-declaration] TELEFONE tf = preenche_telefone(tf.numero, tf.nome);
That arises because there is no declaration of preenche_telefone() in tele.h. It is an error for main() to attempt to call that function without an in-scope declaration for it, but for historical reasons, many compilers accept the call and try to guess a declaration. That guess will invariably assume that the function's return type is int, and an int is not a valid initializer for a struct such as your TELEFONE.
Solution: Add the needed declaration of preenche_telefone() to tele.h.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
