c++ - 多文件 <iostream> 错误 LNK2005 in VS2015 with/Za

标签 c++ visual-studio-2015

当我从 VS2013 迁移我的项目时,我发现了一些奇怪的错误。以下是用于重现它的新创建项目的简化代码:

A.cpp :

#include <iostream>

extern void foo();

int main()
{
    std::cout << "some text from main" << std::endl;
    foo();
}

B.cpp :

#include <iostream>

void foo()
{
    std::cout << "some text from foo" << std::endl;
}

要添加的重要一点是项目将“禁用语言扩展”设置为是(/Za)。如果没有此设置,它会正常构建。

输出是一长串以下错误:

1>B.obj : error LNK2005: "public: static bool const std::numeric_limits<short>::is_signed" (?is_signed@?$numeric_limits@F@std@@2_NB) already defined in A.obj
1>B.obj : error LNK2005: "public: static int const std::numeric_limits<short>::digits" (?digits@?$numeric_limits@F@std@@2HB) already defined in A.obj
1>B.obj : error LNK2005: "public: static int const std::numeric_limits<short>::digits10" (?digits10@?$numeric_limits@F@std@@2HB) already defined in A.obj
1>B.obj : error LNK2005: "public: static bool const std::numeric_limits<unsigned short>::is_signed" (?is_signed@?$numeric_limits@G@std@@2_NB) already defined in A.obj
1>B.obj : error LNK2005: "public: static int const std::numeric_limits<unsigned short>::digits" (?digits@?$numeric_limits@G@std@@2HB) already defined in A.obj
1>B.obj : error LNK2005: "public: static int const std::numeric_limits<unsigned short>::digits10" (?digits10@?$numeric_limits@G@std@@2HB) already defined in A.obj
1>B.obj : error LNK2005: "public: static bool const std::numeric_limits<char16_t>::is_signed" (?is_signed@?$numeric_limits@_S@std@@2_NB) already defined in A.obj
1>B.obj : error LNK2005: "public: static int const std::numeric_limits<char16_t>::digits" (?digits@?$numeric_limits@_S@std@@2HB) already defined in A.obj
...

这似乎表明 <iostream> 的 VS 实现 header 以某种令人讨厌的方式破坏了一个定义规则。这是正确的,还是我遗漏了一些明显的东西?

最佳答案

我在编译 Apache Xerces3 C++ 代码时遇到了同样的问题。 Stein 的评论中已经提到了解决方案.您将需要更新 Visual Studio。版本 14.0.2541.03 更新 3 工作,我的旧版本 14.0.24720.00 更新 1 产生了错误。

关于c++ - 多文件 <iostream> 错误 LNK2005 in VS2015 with/Za,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31808256/

相关文章:

c++ - 将仿函数或函数作为参数传递

C++如何返回内部类

c++ - 如果我知道每个索引处的字符,如何创建字符串(在C++中)

c++ - 流的重载产生两种不同的结果

c# - 在 Visual Studio 2015 UML 类图中生成代码不起作用

c++ - 如何防止ActiveMQ CPP发送方法永远阻塞?

asp.net - Angular 2 与 RequireJS 导入错误

c# - 安装 Visual Studio 2015 会改变 VS 2013 使用的编译器吗?

c++ - friend 类无法访问 protected 成员

c# - VS 2015 工具集可能未知或缺失