I’m new to C++ and trying to understand how it is finding headers. Originally I was just trying to find out what classes are available for me to include in my source code. I believe that different compilers will use different include directories, and hence class availability will vary. My plan was to find the “include” directory that the compiler was using and assume I can include anything there. So I am just getting more confused as I go.
First, I am writing C++ code in Code::Blocks, on Windows 7. The IDE is set to use GNU GCC for compilation, which I learned means it uses the G++ compiler for C++ code. I found my compiler here: C:\MinGW\bin\mingw32-g++.exe, Code::Blocks settings point to that.
So I assumed that G++ must be using C:\MinGW\include recursively to find all its headers. To test my theory, I searched for “iostream.h”. To my surprise, I do not even have “iostream.h” on my C drive. Despite that, my code compiles and works when I include that.
So my questions:
-
How is G++ finding the iostream header when my hard drive does not even have it?
-
Will all the standard C++ headers (as listed here: http://msdn.microsoft.com/en-us/library/ct1as7hw.aspx) be available to all C++ compilers? with the same exact name so I don’t have to change my source code?
Regarding the second question, the standard does not require the headers to be available as files. It requires the
#includedirective to be present in the program and the compiler to behave as if the declarations that the standard required were present in the program. But the compiler is free to inject the declarations in any way it deems fit.That being said, g++ in particular does have files backing up each one of the headers. Without knowing your particular configuration I cannot tell you where the headers will be but you can stop the compilation process after the preprocessor and examine the output:
The paths above are on a MacOSX Lion, and it shows that in this particular configuration
iostreamis included from/usr/include/c++/4.2.1/iostream. In windowsheadmight not be available, but you can redirect the output to a file and read it from there.