c++ - 如何设置 Clang 以使用 MinGW libstdc++

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

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

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

最佳答案

不使用Frontend时需要手动调用clang::InitializePreprocessorclang::BuiltinContext::InitializeBuiltins

此外,三元组必须将“MinGW32”命名为供应商。如果您命名为“MinGW”,那么 Clang 将默默地无法意识到您希望上述兼容性并生成无用的目标文件。

关于c++ - 如何设置 Clang 以使用 MinGW libstdc++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023696/

相关文章:

c++ - 同一台机器上的多个fastcgi程序

c++ - 在 C++ 中获取当前年份的更优雅的方式

c++ - 在 C++ 中使用 CLang

C++ 模板用法 : change variable place leads to compilation error

iphone - 是否可以使用来自非 Apple 操作系统的兼容 LLVM IR/位码来定位 iPhone?

c++ - stringstream 和 nullptr 的子类

c++ - 为什么每个架构的 opensslconf.h 不同?

c++ - 为什么 head 值不是 "NULL"?

c++ - 如何在 RAND 函数中获得更小的比例

macos - 在 Mac os 10.8 上编译 httpd 时,x86_64 出现 undefined symbol "_TLSv1_1_client_method"