2012-12-24 69 views
8

我一直试图在Windows上设置Clang。到目前为止,我通过Visual Studio和CMake以及其他一些惊喜幸存下来。但事实证明,Clang没有自带C++ stdlib实现,因此我决定使用为MinGW构建的GCC 4.7.0的libstdC++。如何设置Clang以使用MinGW libstdC++

首先,我将搜索路径添加到我的HeaderSearchOptions。

headeropts->AddPath(path, clang::frontend::IncludeDirGroup::CXXSystem, true, false, false); 

的路径是正是reside-我从字面上Windows资源管理器复制并粘贴它的标题(和然后加倍逃逸的反斜杠)。但是Clang仍然坚持认为它不能找到<iostream>,即使名为iostream的文件恰好存在于path

为什么找不到我的头文件,以及我需要做什么才能使用libstdC++?

这里是我的代码

llvm::LLVMContext c; 
llvm::Module m("", c); 

clang::CompilerInstance ci; 

clang::FileSystemOptions fso; 

clang::FileManager fm(fso); 

std::string errors; 
llvm::raw_string_ostream error_stream(errors); 
clang::DiagnosticOptions diagopts; 
clang::TextDiagnosticPrinter printer(error_stream, &diagopts); 
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagids(new clang::DiagnosticIDs); 
clang::DiagnosticsEngine engine(diagids, &diagopts, &printer, false); 

clang::SourceManager sm(engine, fm); 

clang::LangOptions langopts; 
langopts.CPlusPlus = true; 
langopts.CPlusPlus0x = true; 

clang::TargetOptions target; 
target.Triple = llvm::sys::getDefaultTargetTriple(); 
auto targetinfo = clang::TargetInfo::CreateTargetInfo(engine, &target); 

auto headeropts = llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions>(new clang::HeaderSearchOptions()); 
headeropts->AddPath("D:\\Backups\\unsorted\\x86_64-w64-mingw32-gcc-4.7.0-release-win64_rubenvb\\mingw64\\include\\c++\\4.7.0",clang::frontend::IncludeDirGroup::CXXSystem, true, false, false); 
headeropts->UseStandardCXXIncludes = true; 
headeropts->UseStandardSystemIncludes = true; 
clang::HeaderSearch hs(headeropts, fm, engine, langopts, targetinfo); 

auto x = llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions>(new clang::PreprocessorOptions()); 
clang::Preprocessor p(x, engine, langopts, targetinfo, sm, hs, ci); 

clang::ASTContext astcon(langopts, sm, targetinfo, p.getIdentifierTable(), p.getSelectorTable(), p.getBuiltinInfo(), 1000); 
CodeGenConsumer consumer; 
clang::CodeGen::CodeGenModule codegen(astcon, clang::CodeGenOptions(), m, llvm::DataLayout(&m), engine); 
consumer.mod = &codegen; 
clang::Sema sema(p, astcon, consumer, clang::TranslationUnitKind::TU_Complete); 

sm.createMainFileID(fm.getFile("main.cpp")); 
engine.getClient()->BeginSourceFile(langopts, &p); 
clang::ParseAST(sema); 
+0

一个简单的'-I'选项应该足够了。运行'gcc -v'和'gcc -E'(在一个示例输入上)来找出它正在使用的所有路径。 –

回答

4

有必要不使用前端时手动调用clang::InitializePreprocessorclang::BuiltinContext::InitializeBuiltins

另外,三联必须命名为“MinGW32”作为供应商。如果你的名字是“MinGW”,那么Clang就会默默地意识到你希望获得兼容性并产生无用的目标文件。