视觉C 错误LNK2019

Visual C++ error LNK2019

本文关键字:LNK2019 错误 视觉      更新时间:2023-10-16

好吧,所以我一直使用gcc编译器。这是我与这个IDE的第一个项目。我的项目是这样组织的:

main.cpp包括 a.h a.c包括a.h并实现a.h中声明的方法

当我编译时,我会得到以下内容:

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

这些都是a.h中的所有功能。使用GCC命令行,我可以将它们编译在一起。所有文件都添加到项目中。我想念什么吗?

错误消息是自称的:

未解决的外部符号" void __ cdecl destroyplayer ...

您有main.cpp和a.c。

对于main.cpp链接器期望在a.h中声明的所有功能使用C 名称mangling和C 调用惯例,而对于A.C链接器,请期望在A.H中声明的所有功能使用C naming和__cdecl调用convention。

如果您的A.C中的代码不使用C 功能,并且您想从Main.CPP调用它,它确实使用C 功能,那么在A.H中,您需要添加以下内容:

#ifdef __cplusplus
extern "C" {
#endif
// your function declarations
#ifdef __cplusplus
}
#endif

另外,如果您不需要混合C和C ,请将所有文件重命名为.c或.cpp。

最后,确保文件实际上是重命名的,因为Visual Studio支持"虚拟"重命名(仅在项目中,而无需重命名实际文件)。

我相信Visual Studio会自动编译.c文件作为C文件,因此名称Mangling可以在此中发挥作用。尝试将a.c重命名为a.cpp,看看是否有帮助。