[英]Is it possible to know if a header is included
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". 例如,如果我输入“iostream”,则输出应为“iostream Defined”。
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: 在我的Gentoo Linux / GCC系统上,查看iostream头我看到:
#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1
so you could check for _GLIBCXX_IOSTREAM
. 所以你可以检查
_GLIBCXX_IOSTREAM
。 If you're not using GCC, open your iostream
header file and see what macros they might have defined. 如果您没有使用GCC,请打开您的
iostream
头文件,看看他们可能定义了哪些宏。
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. 还应该指出
cout
属于iostream
头,所以当_GLIBCXX_IOSTREAM
(在我的情况下)没有定义时,代码也将无法编译。 But you can use printf()
for the test. 但是您可以使用
printf()
进行测试。
Sure it's possible: 当然有可能:
#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;
}
Output: 输出:
paul@local:~/src/cpp/scratch$ ./incl
Enter a header name: iostream
iostream is included.
paul@local:~/src/cpp/scratch$ ./incl
Enter a header name: map
map is not included.
paul@local:~/src/cpp/scratch$
First off, checking for a header, "at runtime" and "at compile time" are no different since #include
is executed at compile time along with any #ifdef
. 首先,检查标题,“在运行时”和“在编译时”没有什么不同,因为
#include
在编译时与任何#ifdef
一起执行。 A #include
essentially does a copy-paste of the header at the top of your .cpp file. #include
本质上是在.cpp文件顶部的标题的复制粘贴。 Razvan Cojocaru pointed out that you can use the #ifdef
to check weather _GLIBCXX_IOSTREAM
is defined. Razvan Cojocaru指出,您可以使用
#ifdef
来检查天气_GLIBCXX_IOSTREAM
是否已定义。 Here is a little example of where this could be used: 以下是可以使用它的一个小例子:
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;
};
However, this is probably a bad idea for several reasons: 但是, 由于以下几个原因, 这可能是一个坏主意 :
So basically, yes it is possible , but not practical . 所以基本上,是的,这是可能的 ,但不实用 。 Especially for long-term maintenance.
特别适合长期维护。 The benefits don't outweigh the danger.
好处并不比危险大。 Just include the library in question yourself.
只需自己包含有问题的图书馆。
As stated by others here, it would be helpful if we knew what your final outcome is. 正如其他人所说,如果我们知道你的最终结果是什么,将会有所帮助。 The place you are planning to use this in most likely has a better solution.
您计划使用此功能的地方最有可能提供更好的解决方案。
Yes, A quick hack to do this is: 是的,快速破解这样做是:
All you need to do is go to the source code header file. 您需要做的就是转到源代码头文件。 And access any member (any constant defined anywhere in source code of header file) via -
并访问任何成员(任何在头文件的源代码中定义的常量) -
cout<<ACCESSED_MEMBER
now compile and run the program, if you got it printed on console then congrats, the file is included successfully in your code. 现在编译并运行程序,如果你在控制台上打印出来,那么祝贺,该文件已成功包含在您的代码中。
Note:1 To find particular source file for header. 注意:1查找标题的特定源文件。 you can type on terminal as
你可以在终端输入
locate <header_file_name>
You will get the location of file. locate <header_file_name>
您将获得文件的位置。 now check for any member by opening it in vi or nano or gedit or any editor of your choice. 现在通过在vi或nano或gedit或您选择的任何编辑器中打开它来检查任何成员。
Note:2 Do not make any changes to this header file if it is not owned by you. 注意:2如果此头文件不归您所有,请勿对其进行任何更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.