c – 是否可以知道是否包含标题
发布时间:2020-12-16 05:06:47 所属栏目:百科 来源:网络整理
导读:在源代码中是否可以知道是否包含头? 这是我需要的一个例子: #includeiostreamusing namespace std;int main(){ char headname[4]; cout"Enter a header name : "; cinheadname; #ifdef headname coutheadname" Defined"endl; #else coutheadname" Not defi
在源代码中是否可以知道是否包含头?
这是我需要的一个例子: #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; } 例如,如果我输入“iostream”,则输出应为“iostream Defined”. 解决方法
是.标题通常使用包括以下内容的警卫:
#ifndef MY_HEADER_INCLUDED #define MY_HEADER_INCLUDED // [...] #endif 在我的Gentoo Linux / GCC系统上,查看iostream头我看到: #ifndef _GLIBCXX_IOSTREAM #define _GLIBCXX_IOSTREAM 1 所以你可以检查_GLIBCXX_IOSTREAM.如果您没有使用GCC,请打开您的iostream头文件,看看他们可能定义了哪些宏. 还应该指出cout属于iostream头,所以当_GLIBCXX_IOSTREAM(在我的情况下)没有定义时,代码也将无法编译.但是您可以使用printf()进行测试. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |