using .pch files with libclang api's
I am trying to use .pch as illustrated in the following example at http://clang.llvm.or开发者_StackOverflowg/doxygen/group__CINDEX.html but it doesnot seem to work.
char *args[] = { "-Xclang", "-include-pch=IndexTest.pch" };
TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args, 0, 0);
libclang fails to read -include-pch flag, it is reading it as -include flag.
What I want is the following: My code depends on lots of headers. I want to parse and create Translation unit once and save it as pch file. Now I just want parsing to happen to a single file in question. Is it possible to do?
I've encountered a similar problem, maybe the solution is also similar :
I'm using clang to compile some code internally, and a vector containing arguments to be sent to the compiler :
llvm::SmallVector<const char *, 128> Args;
Args.push_back("some");
Args.push_back("flags");
Args.push_back("and");
Args.push_back("options");
//...
Adding a line like "Args.push_back("-include-pch myfile.h.pch");" will lead to an error due to the fact that -include-pch flag is read as -include flag.
In this case, if you want to use a pch file, you have to use "two" arguments :
llvm::SmallVector<const char *, 128> Args;
//...
Args.push_back("-include-pch");
Args.push_back("myfile.h.pch");
//...
Use it like this:
char *args[] = { "-Xclang", "-include-pch", "IndexTest.pch" };
This will solve your problem. However, there is a bigger problem, when you want to use multiple pchs... It doesn't work, even with the clang++ compiler.
In clang's documentation you can find source code example:
// 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);
Though i did not check it. Did you find working solution? Check out my question about PCH too.
精彩评论