Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding indirectly included header files

34 views
Skip to first unread message

Paul

unread,
Nov 9, 2014, 8:39:43 AM11/9/14
to
I am using the code blocks IDE with Mingw, gcc and Windows 7. I've noticed that when I #include<iostream> I get access to the functions in stl_algobase.h However, I can't see a way to anticipate that.

What is the most straightforward way to find the files indirectly included by my #include preprocessor statements.

Thank you,

Paul

Paavo Helde

unread,
Nov 9, 2014, 9:53:29 AM11/9/14
to
Paul <peps...@gmail.com> wrote in
news:4799bf85-752c-4d8a...@googlegroups.com:
g++ -M myfile.cpp

or

g++ -MM myfile.cpp

The latter leaves out system headers. Note that you cannot rely much on
which system headers include others, this may easily change in the next
version (unless documented in the standard, of course).

Paul

unread,
Nov 9, 2014, 10:32:57 AM11/9/14
to
Thanks, Paavo.
I did indeed get both your commands to work in the way that you said. However, it's not clear to me from the output exactly which files are included in which headers. Maybe, it isn't supposed to be clear, and the right way is just to include everything that may be needed, with standard header guards used to prevent multiple declarations.

For example, the below code seems to work with gcc. But it might be bad, and the better method might be to #include<algorithm> even though it isn't needed.

#include <iostream>
using namespace std;
int main()
{

int x = min(5, 3);
cout << x;

}


Paavo Helde

unread,
Nov 9, 2014, 11:27:59 AM11/9/14
to
Paul <peps...@gmail.com> wrote in
news:66ad1417-57ce-4e3c...@googlegroups.com:
> I did indeed get both your commands to work in the way that you said.
> However, it's not clear to me from the output exactly which files are
> included in which headers. Maybe, it isn't supposed to be clear, and
> the right way is just to include everything that may be needed, with
> standard header guards used to prevent multiple declarations.

That's right. The only reliable source about which system headers include
other headers is the standard. <iostream> for example is documented to
include <ios>, <streambuf>, <istream> and <ostream>.

> For example, the below code seems to work with gcc. But it might be
> bad, and the better method might be to #include<algorithm> even though
> it isn't needed.

Of course, if your code uses std::max(), it should include <algorithm>.
There is no excuse to avoid that, even if the code might sometimes appear
to accidentally compile without that. And you can be sure all the needed
include guards are present in the system headers.

Cheers
Paavo




0 new messages