Visual C++ error LNK2019

10,063

Solution 1

I believe Visual Studio automatically compiles .c files as C files, so name mangling could play a part in this. Try renaming a.c to a.cpp and see if it helps.

Solution 2

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).

Share:
10,063
Laz
Author by

Laz

Tinkering..

Updated on June 15, 2022

Comments

  • Laz
    Laz almost 2 years

    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?

  • Laz
    Laz over 11 years
    named main and a to .cpp.. Doesnt help. So am I right in assuming that once a file is added to project, the compiler automatically includes it in the compilation process? AKA gcc main.c a.c -o whatever.. Or does it try to pull of some dll-ish stuff?
  • Luchian Grigore
    Luchian Grigore over 11 years
    @RamBhat yes, adding it to the project gets it compiled. It's probably something different. You can go to the project properties and see the exact command that's used to compile.
  • john
    john over 11 years
    @RamBhat Try putting a deliberate error into a.c, just to check if it really is being compiled.
  • Laz
    Laz over 11 years
    Tried adding an error. It doesnt compile error C2143: syntax error : missing ';' before '}'. So that proves a.c is going thru compilation.. This is a true linker issue ..
  • Laz
    Laz over 11 years
    @Luchian Grigore Apologies for the noob question..Where in the project properties can I get the exact command..
  • Luchian Grigore
    Luchian Grigore over 11 years
    @RamBhat right click on project - properties - C++ - command line
  • john
    john over 11 years
    When you renamed a.c to a.cpp did you force a rebuild? I see you've renamed it again. If it is C++ code it really should be a .cpp file.
  • Laz
    Laz over 11 years
    @LuchianGrigore I had screwed up renaming a.c earlier(big noob moment).. My first comment can be ignored. You were right, thanks!