3

Reviewing material for an optimized C++ course next quarter. The professor for this course is enforcing /WALL and /WX for our project properties. The problem I'm having is that including the Iostream library produces over 800 warnings. Here's the code I'm attempting to run:

#include "pch.h"
#include <iostream>

int main() {
  std::cout << "Hello World";
  return(0);
}

A few of the warnings that I'm receiving includes:

C4514 'abs': unreferenced inline function has been removed
C4774 'sprintf_s': format string in argument 3 is not a string literal
C4820 'std::basic_ios ...': '7' bytes of padding added after...

Before asking Stack I emailed the Prof to ask about the warnings and was told:

You should be including iostream

If you get 100 warnings you included a header that's not needed

Is there something I'm missing? I know I wouldn't be able to edit source files for iostream as that's not portable coding. I looked around to see if I could explicitly include functions such as cout, cin, etc. Yet, I don't feel like this is the correct solution.

Edit:

A user requested an example of a more explicit warning message in case there was something missing in there. Here are a few:

C415 'abs': referenced inline function has been removed (Project: Hello World) (File: stdlib.h)
C4710 'int sprintf_s(char *const....: function not inlined. (Project: Hello World) (File: stdio.h)

The professor is using GCC through Visual Studio and our settings are pulled from a repository as premade projects.

Community
  • 1
  • 1
  • What version ov MSVC are you using? – n. m. could be an AI Jan 12 '19 at 16:05
  • I'm using MSVC Enterprise 2017 version 15.8.2. – TechSupportSparky Jan 12 '19 at 16:13
  • 1
    Well I'm getting lots of warnings too. [Possible duplicate](https://stackoverflow.com/questions/4001736/whats-up-with-the-thousands-of-warnings-in-standard-headers-in-msvc-wall) It looks like your professor might not really know what he's talking about. – n. m. could be an AI Jan 12 '19 at 16:35
  • That's true that it is a possible duplicate. I've taken a look at a few related questions. Unfortunately, the professor does not allow preprocessor commands suppressing warnings. – TechSupportSparky Jan 12 '19 at 17:00
  • 3
    Maybe your professor does not use Visual Studio and instead uses g++ and thought the result would be the same as -Wall on g++ – drescherjm Jan 12 '19 at 17:07
  • So what kind of advice are you seeking? We cannot handle quirks of your professor. – n. m. could be an AI Jan 12 '19 at 17:26
  • 1
    @n.m.: IMO the question is _"is there something I'm missing"_ (and the answer is no!) – Lightness Races in Orbit Jan 12 '19 at 17:27
  • The warnings appear to be for code other than that shown. Do not edit the compiler messages - they include important diagnostic information such as file name and line number - you have put us at a disadvantage by not including all information. – Clifford Jan 12 '19 at 17:28
  • @Clifford All the information _is_ in this question, which has been perfectly well-formed. See my answer for why the warnings are generated. – Lightness Races in Orbit Jan 12 '19 at 17:30
  • 2
    What compiler does your professor require you to use for the course? Have you perhaps chose to use an alternative for which his advice is erroneous? If he was translating requirements from say GCC and was less familiar with VC++, he might reasonably suggest /WALL when /W4 is a closer equivalent to GCC's -Wall. – Clifford Jan 12 '19 at 17:32
  • @Clifford This was already stated by drescherjm half an hour ago... – Lightness Races in Orbit Jan 12 '19 at 17:34
  • @LightnessRacesinOrbit : I agree that it is reproducible an therefore answerable if you care to take the trouble and are already familiar with the effect of /WALL, but as a general principle diagnostic information should not be elided. It would have been useful information to verify immediately that the diagnostics were truly in the header file code and not simply that the OP has posted different code. – Clifford Jan 12 '19 at 17:36
  • @Clifford The posted code is an MCVE and reproduces the problem. OP told us that the warnings are triggered by including `iostream`. Everything is here. Nothing is missing, nothing is ambiguous. What more do you want? – Lightness Races in Orbit Jan 12 '19 at 17:38
  • Well it just seems kind of pointless to not bother reading any comments then as a result start repeating them but YMMV, @Clifford. Have a good day! – Lightness Races in Orbit Jan 12 '19 at 17:39
  • The first thing to do is get rid of that `#include "pch.h"`. – Pete Becker Jan 12 '19 at 17:42
  • @LightnessRacesinOrbit : As I said the file names and line numbers for which the diagnostic is reported. If that were not useful diagnostic information, why are they output?! Where a user does not know what is important, they should not omit that information. The point was a general principle; whether it was specifically necessary in this case is irrelevant. Often it is possible to answer a question without having to reproduce it or even having the specific tool given complete information. – Clifford Jan 12 '19 at 17:42
  • Have a good day @Clifford – Lightness Races in Orbit Jan 12 '19 at 17:43
  • @LightnessRacesinOrbit : It was intended to be helpful advice for future reference, not a criticism - it helps in a great many cases, even when not strictly necessary, and the person asking is hardly in a position to determine when it is not necessary. You may be able to answer the question, but it may make the difference between one person being able to answer a question and many - why discard _information_ that is easily provided? You seem rather more animated about my comments than I really think they deserve. There are many unhelpful comments on SO you might get more exercised about. – Clifford Jan 12 '19 at 17:50
  • Have a good day @Clifford!!! (This means the conversion is over) – Lightness Races in Orbit Jan 12 '19 at 17:54
  • 1
    @LightnessRacesinOrbit : What was not expressed in drescherjm's earlier comment was the question regarding standard course software and whether that was perhaps not VC++. In that sense my comment was not a repetition; the rest was by way of explanation of my purpose for asking. – Clifford Jan 12 '19 at 17:54

1 Answers1

6

Your professor is, quite simply, wrong.

This has nothing to do with "including a header that's not needed" (why would that generate warnings?), but with using /WALL, which reveals some flaws in the stdlib implementation there!

This switch is not recommended; quoting James McNellis who gets it bang-on under the above referenced question:

/Wall enables many warnings that, while potentially useful sometimes, are not useful most of the time. /Wall in Visual C++ does not mean the same thing as -Wall on g++ (really, g++ "has /Wall wrong," since it doesn't actually enable all warnings). In any case, in Visual C++, all of the commonly important and useful warnings are enabled by /W4.

I would use /W4 in Visual Studio (and -Wall -Wextra in GCC).

Obviously I can't help you to persuade your professor of this, other than to suggest saying something along the lines of "I asked on Stack Overflow and found out that this is due to /Wall being too strict and generating warnings on Visual Studio's own headers. They suggest we use /W4 instead. . What do you think?"

It is true that you need to #include <iostream>, and it is true that you should never modify the provided standard headers. Also, don't forget to stream a '\n' to end your output line!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055