在头文件中搜索 #ifdef

Searching for #ifdef in a header file

本文关键字:搜索 #ifdef 文件      更新时间:2023-10-16
std::string systemStr = "C:\gcc1\gccxml.exe ";
    systemStr += """ ;
    systemStr += argv[1] ;
    std::cout<<"Header File is:"<<argv[1]<<endl;

在上面的代码片段中,argv[1] 表示头文件的名称。我想打开这个头文件并搜索可能存在的 #ifdefs。我该怎么做?问题是 argv[1] 是一个字符串。如果我不清楚,我很抱歉。

这样的事情怎么样...

std::cout<<"Header File is:"<<argv[1]<<endl;
std::ifstream file(argv[1]);
int lineNum = 0;
bool hasIfdef = false;
while( file.good() )
{
    std::string line;
    std::getline( file, line );
    lineNum++;
    if( line.find("#ifdef") != std::string::npos ||
        line.find("#ifndef") != std::string::npos )
    {
        std::cout << "Line " << lineNum << ": " << line << std::endl;
        hasIfdef = true;
    }
}

最简单的方法是使用 shell 脚本:

     cat header.h | grep -n "^[:space:]*#[:space:]*if[n]*def"

这将显示行号和 #ifdef 或 #ifndef。

如果您需要从 C 程序中执行此操作,您可以随时执行 shell 脚本。