2011-05-05 56 views
1

我試圖使用.pch,如下面的例子中所示在http://clang.llvm.org/doxygen/group__CINDEX.html,但它似乎沒有工作。使用.pch文件與libclang api的

char * args [] = {「-Xclang」,「-include-pch = IndexTest.pch」};

TU = clang_createTranslationUnitFromSourceFile(Idx,「IndexTest.c」,2,args,0,0);

libclang無法讀取-include-pch標誌,它正在讀取它作爲-include標誌。

我想要的是以下內容: 我的代碼依賴於很多頭文件。我想解析並創建一次翻譯單元並將其保存爲pch文件。 現在我只想解析發生在有問題的單個文件。有可能嗎?

+0

你有沒有解決過這個問題?我看到同樣的事情。 – 2011-06-11 07:20:08

回答

1

我遇到類似的問題,也許該解決方案也類似:

我使用鐺在內部編譯一些代碼,並且被髮送到編譯器包含的參數向量:

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"); 
//... 
0

使用方法如下:

char *args[] = { "-Xclang", "-include-pch", "IndexTest.pch" }; 

這將爲您解決問題。然而,當你想使用多個pchs時,會出現更大的問題......即使使用clang ++編譯器,它也不起作用。

0

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); 

雖然我沒有檢查它。你找到工作解決方案嗎?請查看關於PCH的my question