views:

235

answers:

6

I'm trying to compile an unspecified piece of software, and I'm getting errors in the standard headers such as stdio.h. The errors are mostly undeclared identifiers such as _In_. IntelliSense finds their definitions just fine. In what general direction should I look for the cause of this?

Added: For example, in one cpp file stdio.h is the first file included - there are no defines that I can see before it. No precompiled headers are used. Other stuff on my install compiles just fine.

+5  A: 

"You're doing something unspecified wrong" is about the best I can do.

The standard library does compile, I can tell you that much.

So either your project configuration is wrong, or something in your code affects the included file (perhaps some bad #defines, for example)

If you want a more specific answer, you'll have to give us some specific information. Which errors are you getting? How is the file included? Can you show some minimal code that reproduces the problem?

jalf
Well it is Visual Studio, after all, so you never know :). Time to switch to gcc...
Michael Aaron Safyan
because in gcc it's impossible to make errors, right? The compiler magically detects what you want to do, and writes your code for you *and* sets compiler flags. Shame Microsoft didn't think of that when they wrote their compiler...I'm sorry to burst your bubble, but any C++ compiler allows you to 1) write code that breaks the standard library, and 2) specify compiler flags that makes it unable to compile your code.
jalf
To be fair, though, gcc doesn't store compiler flags including preprocessor defines in obscure places which are difficult to reproduce when describing your build environment. That's what make is for.
Steve Jessop
And there's nothing stopping one from using nmake with VC if you don't like GUI.
KTC
You could just check the command line section in project properties, to see exactly how it's calling the compiler. Or checking the build log it generates when it builds to see how it *called* it. Or, for that matter, just posting the contents of the project file here. It's just xml after all.So nah, I don't really think that makes a big difference.
jalf
@jalf: sorry, yes, when I said "that's what make is for", I meant, "gcc doesn't store compiler flags including defines in obscure places which are difficult to report because make stores compiler flags in obscure places for it". In fact make's arguably worse, since it can pull CPPFLAGS from the environment, which AFAIK Visual Studio doesn't.
Steve Jessop
A: 

The standard line should compile. However, if you have modified or deleted some files in the headers provided with the Visual Studio installation, you are in troubles and will have to reinstall everything.

One way to make sure is to make a a new "hello world" console application. It will include stdio in the stdafx.h.

I don't think that this is your problem and you should give more details about the problem if you want a better answer.

Is the stdio included in the stdafx.h?

luc
or better still, don't use a precompiled header in the first place. When you're trying to figure out if your compiler is broken, it's all the more reason to keep it simple.
jalf
+1  A: 

Since VS likes to use precompiled headers, you might want to make sure that you haven't violated any of the assumptions. One source of trouble is to name any header at all ahead of the line that includes stdafx.h.

Even in the without any precompiled headers issues, you might be inadvertently defining something that interacts badly with definitions in the stock headers. If you look inside stdio.h, you'll see that it has a number of interesting conditional compilation sections since the same file is distributed to a number of distinct platforms. Be sure to look at your project's settings, and if the issue is happening only when compiling a specific source file, then that file's compilation settings as well.

It is certainly worth starting a new project and checking if good ol' hello.c can be compiled...

#include <stdio.h>
int main(int argc, char **argv) {
    printf("hello, world.\n");
    return 0;
}

if not, then there is something seriously wrong with your VS installation.

One other possible but unlikely source of trouble would be if you have other compilers installed, and have somehow accidentally got VS using another stdio.h entirely... An inappropriate INCLUDE environment variable used to be able to cause that, but I'm not certain it does in recent versions. I got burned by that a long time ago, and have been much more careful about what variables I let individual compilers set in the global environment since then.

RBerteig
+1  A: 

Possibilities:

  • Compiling with the wrong flags. Specifically your include paths aren't set correctly, you're compiling for C++ instead of C, something along those lines.
  • You're including things in front of the header that are redefining things inside the standard headers. Your question makes this sound unlikely.
  • You're defining things on the compiler command line that are messing things up.
  • Your headers are incorrect, wrong, not meant to be used with your version of the compiler, etc.
  • Your compiler is incorrect, wrong, not standards-compliant, etc.

Best diagnostic is to dump preprocessed source. Every compiler has an option for this. I believe it's -E on gcc, check the docs or gui options for visual studio.

Dan Olson
A: 

The errors are mostly undeclared identifiers such as _In_. IntelliSense finds their definitions just fine. In what general direction should I look for the cause of this?

An undeclared identifier probably means that your standard header is itself trying to include another header (internally), not finding it, and therefore not getting identifiers properly declared/defined.

Added: For example, in one cpp file stdio.h is the first file included - there are no defines that I can see before it.

A define is different from a declaration, which is different from a definition. You probably already know this, but I just want to be sure.

Since the question is tagged as C++, I would encourage you to include cstdio instead of stdio.h. It probably won't fix the compiler error, but it's the official C++ header. On POSIX systems you'll sometimes find that the standard C headers get extended with things from POSIX. In my experience you don't have the same extensions in the C++-ified versions of those headers.

Max Lybbert
A: 

I think sometimes mismatch between different versions of headers library can cause this as well. Have you recently install new versions of standard libraries, like with SDK or whatever, or played with the PATHs and stuff like that?

KTC