c++ - 链接器 LNK2005 错误

标签 c++ visual-studio-2010 linker

我在这里搜索了很多次,发现这个问题很多次,但作者通常没有提供代码示例,我今天遇到了这个问题,但我不太确定如何解决它。

1 个错误列表 bool Init(?@@A_NA) 已经在 Client.obj 中定义。这是我的 Client.cpp、Main.cpp 和 Main.h 的部分代码。

客户端.cpp

#include "stdafx.h"
#include "Main.h"
// the rest of the code doesn't have anything to do with this error..

主要.h

#include "stdafx.h"
bool Init;
// the rest of the code doesn't have anything to do with this error..

main.cpp

#include "stdafx.h"
#include "Main.h"

int main()
{
    Init = false;
    return 0;
}

最佳答案

#include "Main.h" 中,您定义了 bool Init;,因此每次包含 Main.h 时,您将重新定义 Init。如果您将 Init 设为静态,

static bool Init;

此处 Init 将具有页面级范围,您的代码应该可以正常工作。

或者更好的是,你在 Math.h 中将 Init 设置为 extern,

extern bool Init;

然后在.cpp文件中定义它,这样你就会有多个声明,但一个定义。

关于c++ - 链接器 LNK2005 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27699548/

相关文章:

c++ - 无法编译最简单的 Phoenix lambda

c++ - Swig 简单类型 typedef 参数

c# - 如何使用 UIMap Coded UI Test Builder 进行本地测试

c++ - 替换字符串中的一些字符

visual-studio-2010 - Visual Studio 中 Sharepoint 文件的版本历史

visual-studio-2010 - 是否可以使用 MSBuild 将配置定义移动到属性表中?

c - avr-ld : avr:51 architecture of input file `main.o' is incompatible with avr output

linux - 有没有办法在不使用 libdl.so 的情况下通过 C 中的源代码实现 dlopen?

c - 如何在 SDK 的交叉编译器中访问 -ldl

php - 如何在网页和实际程序之间进行交互