I have relatively simple setup.
I have a new Console C++ project.
But I was playing with global vars in C, and added two new .c files like this.
// Fruit.h
extern int global_variable;
Now the source:
// Fruit.c
#include "Fruit.h"
#include <stdio.h>
int global_variable = 37;
Also,
// Orange.h
void use_it(void);
and
// Orange.c
#include "Orange.h"
#include "Fruit.h"
#include <stdio.h>
void use_it(void)
{
printf("Global variable: %d\n", global_variable++);
}
Finally, this is my main:
#include "stdafx.h"
#include "Orange.h"
#include "Fruit.h"
int _tmain(int argc, _TCHAR* argv[])
{
use_it();
return 0;
}
But this is the error I get: "error LNK2019: unresolved external symbol "void __cdecl use_it(void)" (?use_it@@YAXXZ) referenced in function _wmain"
Any help?
followed this advice on global vars: here
Your main file is a C++ file and the external file is C, if you want to reference a C function from C++ in the header should surround the declarations with
#ifdef __cplusplus
extern "C"{
#endif
// declarations here
#ifdef __cplusplus
}
#endif
or
preface them with extern "C"
if those declarations will never be seen from a C file, as C has no idea what extern "C"
means.
The issue is that the compiler is looking for a C++ name mangled function not a C function, which uses a different mangling (usually none).