question sur l'utilisation du mot clé extern
Bonjour,
J'essai d'apprendre a utiliser le mot clé "extern", mais j'ai une erreur à la compilation :
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl testStaticVar(void)" (?testStaticVar@@YAHXZ) referenced in function _main
1>C:\Users\Sébastien\Desktop\test C pur\langage C test\Debug\langage C test.exe : fatal error LNK1120: 1 unresolved externals
test.h :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| #ifndef TEST_H
#define TEST_H
extern int a ;
extern int testStaticVar();
// int testStaticVar()
// {
// int add = 1000 ;
// add += 1;
// return add ;
// }
#endif |
test.c :
Code:
1 2 3 4 5 6 7 8 9 10 11
| #include "test.h"
int a = 1000 ;
//a = 1000 ;
int testStaticVar()
{
static int add = 1000 ;
add += a;
return add ;
} |
main.cpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| #include <iostream>
#include "test.h"
using namespace std;
void main()
{
cout<<testStaticVar()<<endl ;
cout<<testStaticVar()<<endl ;
cout<<testStaticVar()<<endl ;
cout<<testStaticVar()<<endl ;
} |
Est il possible de ne pas mettre la définition de la fonction dans le .h mais juste son prototype et la définition dans le .c ? Si oui comment faut il faire ?