Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-file <iostream> error LNK2005 in VS2015 with /Za

I observered some strange error, when I migrated my projects from VS2013. Here is simplified code of a newly created project to reproduce it:

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;
}

The important thing to add is project has "Disable Language Extension" set to Yes (/Za). Without this setting, it is bulding properly.

The output is a long list of following errors:

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
...

This seems to indicate, that VS implementation of <iostream> header breaks One Definition Rule in some nasty way. Is it right, or am I missing something obvious?

like image 450
Grzegorz Szpetkowski Avatar asked Aug 04 '15 11:08

Grzegorz Szpetkowski


1 Answers

I had the same problem compiling the Apache Xerces3 C++ code. The solution was already mentioned by the comment from Stein. You will need an update for the Visual Studio. Version 14.0.2541.03 Update 3 worked, where my older version 14.0.24720.00 Update 1 produced the error.

like image 161
V15I0N Avatar answered Oct 06 '22 02:10

V15I0N