好吧,所以我一直使用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++ 名称修饰和 C++ 调用约定,而对于 a.c,链接器期望在 a.h 中声明的所有函数都使用 C 命名和 __cdecl 调用约定。
如果您在 a.c 中的代码没有使用 C++ 功能,并且您想从确实使用 C++ 功能的 main.cpp 调用它,那么在 a.h 中您需要添加以下内容:
#ifdef __cplusplus
extern "C" {
#endif
// your function declarations
#ifdef __cplusplus
}
#endif
或者,如果您不需要混合使用 C 和 C++,请将所有文件重命名为 .c 或 .cpp。
最后,确保文件确实重命名,因为 Visual Studio 支持“虚拟”重命名(仅在项目中,而不重命名实际文件)。
关于c++ - Visual C++ 错误 LNK2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099083/