Okay, So I have always used the gcc compiler. This is my first project with this IDE. My project is organised like this:
main.cpp which includes a.h
a.c which includes a.h and implements the methods declared in a.h
When I compiled it I get the following:
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl destroyPlayer(struct player *)" (?destroyPlayer@@YAXPAUplayer@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl drawPlayer(struct player *)" (?drawPlayer@@YAXPAUplayer@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl movePlayer(struct player *,int *)" (?movePlayer@@YAXPAUplayer@@PAH@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "struct player * __cdecl createPlayer(int,int)" (?createPlayer@@YAPAUplayer@@HH@Z) referenced in function _main
These are all the functions in a.h. With the GCC command line I could compile them together. All files are added to the project. Am I missing something?
The error message is self-explanatory:
unresolved external symbol "void __cdecl destroyPlayer...
You have main.cpp and a.c.
For main.cpp linker expects that all functions declared in a.h use C++ name mangling and C++ calling convention, while for a.c linker expects that all functions declared in a.h use C naming and __cdecl calling convention.
If your code in a.c is not using C++ features, and you want to call it from main.cpp which does use C++ features, then in a.h you need to add the following:
#ifdef __cplusplus
extern "C" {
#endif
// your function declarations
#ifdef __cplusplus
}
#endif
Alternatively, if you don't need to mix C and C++, rename all the files to .c or to .cpp.
Finally, make sure that files are actually renamed because Visual Studio supports "virtual" rename (just in the project, without renaming the actual file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With