7

I am trying to get Visual Studio 2010 set up to do plain old ANSI compilation, without Microsoft extensions of any kind.

I started with an empty project template, since there doesn't seem to be a plain ANSI project template in 2010 anymore.

Then I set

Properties -> Configuration Properties -> C/C++ -> Language -> Disable Extensions = Yes (/Za)

Here is my code:

#include <iostream>

int main( int argc, const char* argv[] )
{
  std::cout << "Hello World!";
  return 0;
}

Here are the errors:

1>------ Build started: Project: ansi_test, Configuration: Release Win32 ------
1>Build started 4/27/2011 4:20:00 PM.
1>InitializeBuildStatus:
1>  Touching "Release\ansi_test.unsuccessfulbuild".
1>ClCompile:
1>  Main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(636): error C3861: 'width': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(636): error C3861: 'width': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(639): error C3861: 'flags': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(643): error C2227: left of '->sputc' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(643): error C2780: 'void std::fill(_FwdIt,_FwdIt,const _Ty &)' : expects 3 arguments - 0 provided
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility(2712) : see declaration of 'std::fill'
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(643): error C3861: 'rdbuf': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(648): error C2227: left of '->sputc' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(648): error C3861: 'rdbuf': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(654): error C2227: left of '->sputc' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(654): error C2780: 'void std::fill(_FwdIt,_FwdIt,const _Ty &)' : expects 3 arguments - 0 provided
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility(2712) : see declaration of 'std::fill'
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(654): error C3861: 'rdbuf': identifier not found
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(659): error C3861: 'width': identifier not found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.63

How do I get this, the most basic of C++ code to compile cleanly?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • looks like iostream does not have all the includes in this mode – CharlesB Apr 27 '11 at 20:42
  • 2
    I just tried it - no problems. –  Apr 27 '11 at 20:43
  • `/Za` has been documented many times over to break valid ISO language features. Just don't use it. – ildjarn Apr 27 '11 at 20:43
  • 1
    why does every other question about ANSI compliance recommend `/Za` if it is broken? –  Apr 27 '11 at 20:46
  • If you don't intend to use any of the MS stuff, why not use gcc and an IDE like code::Blocks? –  Apr 27 '11 at 20:48
  • I just tried it as well in VS2010 and it compiled and ran fine - though I was expecting it to have problems. Historically, MS headers haven't been too friendly with "/Za" (though I don't remember if that was so much standard C/C++ headers or SDK headers). I think MS provides `/Za` more for some compliance or testing reasons rather than for actual use (kind of like the old POSIX subsystem on NT). – Michael Burr Apr 27 '11 at 20:51
  • 7
    Here is the [link](http://permalink.gmane.org/gmane.comp.lib.boost.devel/212180) to a message from the person at MS, responsible for the standard library. He has given up on using /Za... – Bo Persson Apr 27 '11 at 20:53
  • Care to share what you have in `Property manager | Release Win32 | Common Properties | VC++ directories` ? It looks like VC is either picking up a wrong directory for `iostream` or it's entirely incapable of finding it. – joce Apr 27 '11 at 20:53
  • Just tried as well and had no problems compiling.... – Ben Schwehn Apr 27 '11 at 20:55
  • 1
    if I take out the `/Za` it compiles and runs but `/Wall` spews out crap loads of warnings, I would think something this simple should compile cleanly? –  Apr 27 '11 at 20:55
  • 1
    @Bo: Good link. If Lavavej can't get `/Za` fixed (or removed), there's not much hope for any normal user... – Michael Burr Apr 27 '11 at 20:57
  • /Wall reports lots of things that are really legit. It literally means **all** diagnostics the compiler can ever do. The "proper" level is /W4, (possibly with a few select warnings disabled). A choice of [silly warnings](http://www.velocityreviews.com/forums/t715830-msvc-sillywarnings-more.html) that some people disable. – Bo Persson Apr 27 '11 at 21:01
  • 3
    Microsoft should fix MSVC to do same as GCC and not issue diagnostics for system headers (even for high warning levels) - what's a user supposed to do about those? – Michael Burr Apr 27 '11 at 22:23
  • @Bo Persson : That was one of the specific mailings I had in mind, but sadly it was only one of many. – ildjarn May 03 '11 at 08:08

2 Answers2

5

The error disappears if you define wchar_t as native type.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • I can reproduce this. I think the setting is the default setting, so it should compile on a clean install. – Ben Schwehn Apr 27 '11 at 21:05
  • I had this disabled, I re-enabled it and my program compiles and runs now. The `/Za` option was not the problem, I have it set again, and everything still works. –  Apr 28 '11 at 15:17
0

If I copy your code into a file, test.cpp and then issue the command

cl /Za /EHsc test.cpp

then it compiles cleanly without any warnings or errors on my install of visual c++ express 2010. /EHsc is necessary in order to handle exceptions properly and I don't really understand why it isn't the default.

Is this different from what you are doing?

jcoder
  • 29,554
  • 19
  • 87
  • 130
  • I am pressing `CTRL-F7` to compile the `main.cpp` file –  Apr 28 '11 at 04:43
  • 1
    `cl /Za /Zc:wchar_t- test.cpp` will produce these errors. Looks like `/Za` and `/Zc:wchar_t-` are incompatible since VS2010. – Paul Aug 20 '13 at 17:08