C++ 是否可能知道是否包含标题

C++ 是否可能知道是否包含标题,c++,header,C++,Header,在源代码中,是否可以知道是否包含头 这是我需要的一个例子: #include<iostream> using namespace std; int main() { char headname[4]; cout<<"Enter a header name : "; cin>>headname; #ifdef headname cout<<headname<<" Defined"<

在源代码中,是否可以知道是否包含头

这是我需要的一个例子:

#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;
}
#包括
使用名称空间std;
int main()
{
字符头名[4];
coutheadname;
#ifdef标题名

cout是。标题通常包括以下防护装置:

#ifndef MY_HEADER_INCLUDED
#define MY_HEADER_INCLUDED

// [...]

#endif
在我的Gentoo Linux/GCC系统上,查看iostream头,我看到:

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1
因此,您可以检查
\u GLIBCXX\u IOSTREAM
。如果您不使用GCC,请打开
IOSTREAM
头文件,查看它们可能定义了哪些宏

还应该指出的是,
cout
属于
iostream
头,因此当未定义
\u GLIBCXX\u iostream
(在我的情况下)时,代码也将无法编译。但是您可以使用
printf()
进行测试。

确保有可能:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main(void) {
    std::vector<std::string> includes;
    includes.push_back("iostream");
    includes.push_back("vector");
    includes.push_back("string");
    includes.push_back("algorithm");

    std::string user_input;
    std::cout << "Enter a header name: ";
    std::cin >> user_input;

    if ( std::find(includes.begin(), includes.end(), user_input) !=
            includes.end() ) {
        std::cout << user_input << " is included." << std::endl;
    } else {
        std::cout << user_input << " is not included." << std::endl;
    }

    return 0;
}

首先,检查头,“在运行时”和“在编译时”没有什么不同,因为
#include
是在编译时与任何
#ifdef
一起执行的。
#include
本质上是在.cpp文件顶部复制粘贴头。Razvan Cojocaru指出,您可以使用
#ifdef
检查天气
\u GLIBCXX\u IOSTREAM
。这里是一个例子ttle可使用此功能的示例:

class Flagger
{
    typedef unsigned long ulong;
    public:
        Flagger (ulong flags = 0)    : f(flags) { ; }
        Flagger (const Flagger& cpy) : f(cpy.f) { ; }

        void clear  (ulong flags) { f &= ~flags; }
        void set    (ulong flags) { f |= flags;  }
        void toggle (ulong flags) { f ^= flags;  }

        bool get    (ulong flags) { return f & flags; }

#ifdef _GLIBCXX_OSTREAM
        friend std::ostream& operator << (std::ostream &out, const Flagger& f)
                { /* print it how you want it*/ }
#endif

    private:
        ulong f;
};
类标记器
{
typedef无符号长ulong;
公众:
标记器(ulong flags=0):f(flags){;}
旗手(常数旗手&cpy):f(cpy.f){;}
无效清除(ulong标志){f&=~flags;}
无效集(ulong标志){f |=标志;}
无效切换(ulong标志){f^=flags;}
bool get(ulong flags){return f&flags;}
#ifdef_GLIBCXX_OSTREAM

friend std::ostream&operator是的,一个快速解决方法是:

您只需转到源代码头文件,并通过以下方式访问任何成员(头文件源代码中任意位置定义的任何常量)—


cout
#ifdef
在编译之前运行,绝对不是在运行时运行。是的,可以知道是否包含头文件。只需查看源文件的顶部。如果用户看不到源文件,那么谈论是否包含头文件是毫无意义的,因为源文件是他们唯一包含的内容插入。请更仔细地思考您的问题。@BBeta那么,如果您的代码正常工作,用户为什么会关心您是否包含了一个标题?问题非常不清楚。您需要告诉我们为什么需要它-这是问题的一个重要部分。根本没有理由知道任何给定的.cpp文件在任何给定行中包含了哪些内容编译后在代码中。但这将导致编译时检查(#ifdef),而不是运行时检查。
#ifdef
是一个预处理器指令。在运行时没有任何用处。没有人会简单地输入头名,程序应该告诉他是否存在此头…你说得对,我确实错过了“运行时”部分。好的,在运行时检查系统中是否存在头文件的唯一方法是在include搜索路径中实际查找头文件。但这不会告诉您在构建程序时是否包含了该头文件。这是编译时检查。您可以在编译时在std::vector中为示例:#如果定义了HEADER1,则将_向后推()“HEADER1”你可以在运行时检查编译时是否确实包含了其中一个头。但这只适用于少数头,而不适用于任何包含的头。嗯……这是怎么回事?OP askedIt并不是按照OP的要求做的,它允许程序的用户在运行时检查什么头源代码中包含了rs。OP误以为可以以某种方式自动化此过程。但事实并非如此。可以以完全相同的方式自动化此过程,就像可以在第一时间自动包含正确的标题一样,这样就不会给他带来任何实际问题。公平地说,这是有可能的sue使用嵌套包含。尽管如此,这个想法一开始还是令人难以置信。使用Unity编译怎么样?预编译头?目的是什么?
class Flagger
{
    typedef unsigned long ulong;
    public:
        Flagger (ulong flags = 0)    : f(flags) { ; }
        Flagger (const Flagger& cpy) : f(cpy.f) { ; }

        void clear  (ulong flags) { f &= ~flags; }
        void set    (ulong flags) { f |= flags;  }
        void toggle (ulong flags) { f ^= flags;  }

        bool get    (ulong flags) { return f & flags; }

#ifdef _GLIBCXX_OSTREAM
        friend std::ostream& operator << (std::ostream &out, const Flagger& f)
                { /* print it how you want it*/ }
#endif

    private:
        ulong f;
};