Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to know if a header is included

Tags:

c++

header

In the source code is it possible to know if a header is included?

This an example of what I need :

#include<iostream>
using namespace std;

int main()
{
    char headname[4];
    cout<<"Enter a header name : ";
    cin>>headname;

    #ifdef headname
        cout<<headname<<" Defined"<<endl;
    #else
        cout<<headname<<" Not defined"<<endl;
    #endif

    return 0;
}

For example, if I enter "iostream", the output should be "iostream Defined".

like image 980
Az.Youness Avatar asked Oct 20 '13 18:10

Az.Youness


1 Answers

Yes. Headers usually use include guards such as:

#ifndef MY_HEADER_INCLUDED
#define MY_HEADER_INCLUDED

// [...]

#endif

On my Gentoo Linux / GCC system, looking at the iostream header I see:

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1

so you could check for _GLIBCXX_IOSTREAM. If you're not using GCC, open your iostream header file and see what macros they might have defined.

It should also be pointed out that cout belongs to the iostream header, so when _GLIBCXX_IOSTREAM (in my case) is not defined, the code will also fail to compile. But you can use printf() for the test.

like image 58
Razvan Cojocaru Avatar answered Oct 13 '22 05:10

Razvan Cojocaru