如何设置Clang使用MinGW libstdc ++

时间:2012-12-24 16:31:44

标签: c++ clang

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

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

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

路径正好是标题所在的位置 - 我从Windows资源管理器中复制并粘贴它(然后将转义加倍转义为转义)。但是Clang仍然坚持认为它找不到<iostream>,即使名为iostream的文件恰好存在于path

为什么Clang不能找到我的标题,我还需要做什么才能使用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);

1 个答案:

答案 0 :(得分:4)

不使用前端时,必须手动调用clang::InitializePreprocessorclang::BuiltinContext::InitializeBuiltins

此外,三人必须命名&#34; MinGW32&#34;作为供应商。如果你的名字,比如说,&#34; MinGW&#34;,那么Clang将默默地意识到你希望所说的兼容性并产生无用的目标文件。