2012-10-27 106 views
1

好吧,所以我一直使用gcc编译器。这是我用这个IDE的第一个项目。我的项目是组织这样的:Visual C++错误LNK2019

main.cpp包括a.h a.c包括a.h并实现在a.h

声明的方法。当我编译它,我得到如下:

1>main.obj : error LNK2019: unresolved external symbol "void __cdecl destroyPlayer(struct player *)" ([email protected]@[email protected]@@Z) referenced in function _main 
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl drawPlayer(struct player *)" ([email protected]@[email protected]@@Z) referenced in function _main 
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl movePlayer(struct player *,int *)" ([email protected]@[email protected]@[email protected]) referenced in function _main 
1>main.obj : error LNK2019: unresolved external symbol "struct player * __cdecl createPlayer(int,int)" ([email protected]@[email protected]@[email protected]) referenced in function _main 

这些都是a.h中的函数。使用GCC命令行我可以将它们一起编译。所有文件都被添加到项目中。我错过了什么吗?

+1

对不起,不是一个笨蛋。 –

回答

4

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

+0

命名主和一个.cpp ..没有帮助。所以,我正确地假设一旦文件被添加到项目中,编译器会自动将它包含在编译过程中? AKA gcc main.c a.c -o无论..还是它试图拉动一些dll-ish的东西? – Laz

+0

@RamBhat是的,将它添加到项目获得编译。这可能是不同的。您可以转到项目属性并查看用于编译的确切命令。 –

+0

@RamBhat尝试将故意的错误放入a.c中,只是为了检查它是否真的被编译。 – john

6

错误消息是不言自明:

解析的外部符号“无效__cdecl destroyPlayer ...

你的main.cpp和交流

对于main.cpp的连接预计,在ah中声明的所有函数都使用C++名称修饰和C++调用约定,而对于ac链接程序,期望在ah中声明的所有函数都使用C命名和__cdecl调用约定。

如果您在AC代码不使用C++的功能,你想从main.cpp中它确实使用了C++的功能调用它,然后在啊,你需要添加以下内容:

#ifdef __cplusplus 
extern "C" { 
#endif 

// your function declarations 

#ifdef __cplusplus 
} 
#endif 

或者,如果你不不需要混合使用C和C++,将所有文件重命名为.c或重命名为.cpp。

最后,确保文件实际上已重命名,因为Visual Studio支持“虚拟”重命名(只需在项目中,无需重命名实际文件)。

+0

将文件命名为.cpp工作..谢谢! – Laz

+0

@IgorLevicki:非常好...它确实对我有用...... –