我正在尝试使用http://clang.llvm.org/doxygen/group__CINDEX.html中的以下示例所示的.pch,但它似乎不起作用。
char *args[] ={ "-Xclang","-include-pch=IndexTest.pch“};
TU = clang_createTranslationUnitFromSourceFile(Idx,"IndexTest.c",2,args,0,0);
libclang无法读取包含pch标志,它正在将其读取为-include标志。
我想要的是:我的代码依赖于很多头文件。我想解析并创建一次翻译单元,然后将其保存为pch文件。现在我只想对一个有问题的文件进行解析。有可能这样做吗?
发布于 2011-06-13 01:32:21
我也遇到过类似的问题,也许解决方案也是类似的:
我使用clang在内部编译一些代码,并使用一个包含要发送给编译器的参数的向量:
llvm::SmallVector<const char *, 128> Args;
Args.push_back("some");
Args.push_back("flags");
Args.push_back("and");
Args.push_back("options");
//...添加像"Args.push_back(" -include -pch myfile.h.pch");“这样的行将导致错误,因为-include-pch标志被读取为-include标志。
在这种情况下,如果要使用pch文件,则必须使用“两个”参数:
llvm::SmallVector<const char *, 128> Args;
//...
Args.push_back("-include-pch");
Args.push_back("myfile.h.pch");
//...发布于 2011-11-09 23:52:23
像这样使用它:
char *args[] = { "-Xclang", "-include-pch", "IndexTest.pch" };这将解决您的问题。然而,有一个更大的问题,当你想使用多个pchs时…即使使用clang++编译器,它也不能工作。
发布于 2014-11-11 03:30:06
在clang's documentation中,您可以找到源代码示例:
// excludeDeclsFromPCH = 1, displayDiagnostics=1
Idx = clang_createIndex(1, 1);
// IndexTest.pch was produced with the following command:
// "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
// This will load all the symbols from 'IndexTest.pch'
clang_visitChildren(clang_getTranslationUnitCursor(TU), TranslationUnitVisitor, 0);
clang_disposeTranslationUnit(TU);
// This will load all the symbols from 'IndexTest.c', excluding symbols
// from 'IndexTest.pch'.
char *args[] = { "-Xclang", "-include-pch=IndexTest.pch" };
TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args, 0, 0);
clang_visitChildren(clang_getTranslationUnitCursor(TU), TranslationUnitVisitor, 0);
clang_disposeTranslationUnit(TU);虽然我没有检查过。你找到可行的解决方案了吗?也可以在my question上查看关于PCH的信息。
https://stackoverflow.com/questions/5893489
复制相似问题