Click to See Complete Forum and Search --> : #IFDEF statements
Helical Cynic
09-14-2001, 10:22 AM
Much as I dislike MS Visual C++, that's what in our labs at school. Is there a way to change the #include statements based on which compiler the code detects? I think I need some sort of #IFDEF statement but I'm not certain. The problem is that (AFAIK) the ANSI standard libs are <iostream>, <fstream>, and when I use those in Linux/Dev-C++ my progs compile just fine. Under MSVC++, I get all sorts of errors beginning with the includes. Changing them to <iostream.h>, <fstream.h>, etc. fixes the problem.
Thanks for the help!
Bryan
bwkaz
09-14-2001, 11:17 AM
I think iostream.h and fstream.h will work in Linux C/C++ also. Maybe I should go try to compile a program to find out for sure, but I am fairly sure they will.
AFAIK, the <iostream.h> notation is fairly standard -- I know it is in Winblows because that's the only way to do it. In Linux, though, hmmm..... WINE appears to use .h, so does the kernel....
If you want to use the version without the .h, you can test to see if WINDOWS is defined. It should be in MSVC++ (well, maybe there are underscores before and after it, but I'm not sure). If it isn't, then maybe check for LINUX being defined, or maybe just assume it's Linux if not Windows.
The code would look something like this:
#ifdef WINDOWS
#include <iostream.h>
#else
#include <iostream>
#endif
But I think I would use .h's.
MrNewbie
09-14-2001, 11:27 AM
Doesn't saying #include <iostream> just include the file iostream.h anyway? Just a way to not have to add the .h?
TheLinuxDuck
09-14-2001, 11:43 AM
Originally posted by MrNewbie:
<STRONG>Doesn't saying #include <iostream> just include the file iostream.h anyway? Just a way to not have to add the .h?</STRONG>
IIRC, the <iostream> is simply a file that indicates to include <iostream.h>. Maybe not, but I think so.. (I don't have a machine with a recent enough compiler to test that theory ::smile: :)
Helical Cynic
09-14-2001, 11:57 AM
My reason for using <iostream> instead of <iostream.h> is that <iostream> is supposed to be the ANSI standard (which I hope would mean I could take this to my cousin's mac w/ codewarrior and compile it right. Is <iostream.h> the ANSI standard?
MrNewbie
09-14-2001, 03:46 PM
I think what people are saying is, it doesn't matter, since including <iostream> in effect includes <iostream.h> anyway.
MrNewbie
09-14-2001, 03:49 PM
Here's something from Thinking In C++ that may be of interest to you:
Standard C++ include format
As C++ evolved, different compiler vendors chose different extensions for file names. In addition, various operating systems have different restrictions on file names, in particular on name length. These issues caused source code portability problems. To smooth over these rough edges, the standard uses a format that allows file names longer than the notorious eight characters and eliminates the extension. For example, instead of the old style of including iostream.h, which looks like this:
#include <iostream.h>
you can now write:
#include <iostream>
The translator can implement the include statements in a way that suits the needs of that particular compiler and operating system, if necessary truncating the name and adding an extension. Of course, you can also copy the headers given you by your compiler vendor to ones without extensions if you want to use this style before a vendor has provided support for it.
The libraries that have been inherited from C are still available with the traditional ‘.h’ extension. However, you can also use them with the more modern C++ include style by prepending a “c” before the name. Thus:
#include <stdio.h>
#include <stdlib.h>
become:
#include <cstdio>
#include <cstdlib>
And so on, for all the Standard C headers. This provides a nice distinction to the reader indicating when you’re using C versus C++ libraries.
The effect of the new include format is not identical to the old: using the .h gives you the older, non-template version, and omitting the .h gives you the new templatized version. You’ll usually have problems if you try to intermix the two forms in a single program.
It's a bit long, but anyway, don't get confused by the <cstudio> stuff, the c is just for traditional C libraries instead of the C++ ones.
Stuka
09-14-2001, 05:27 PM
Um, bwkaz, you CAN do#include <iostream> in Windows - in fact, I'm sitting here at work compiling code with that notation right now. What you MUST do, however, if you don't use the .h notation, you must either put a using declaration, or else prepend the namespace (std) to the object name, i.e.:#include <iostream>
using namespace std;
int main(){
cout << "Hello World";
return 1;
}
or:#include <iostream>
int main(){
std::cout<<"Hello World";
}
bwkaz
09-14-2001, 05:57 PM
I stand corrected.
Helical Cynic
09-15-2001, 11:53 AM
Thanks! the namespace seems to have fixed it.
pinoy
09-15-2001, 04:48 PM
Prior to standardization, most header files ended with .h (this was also before namespaces were added).
The standards committee basically decided that everything should be in a namespace std, including the C library (eg printf). To support backwards compatibility, they decided to rename the header files without the .h. The old C library had also been renamed with a 'c' at the front. eg. <cstdio> instead of <stdio.h>
For backwards compatibility the C library (and optionally the old C++ 'library') are provided with the old header files, usually by just including the corresponding C++ header file and importing it into the global namespace.