title: How to set up Clang to use MinGW libstdc++
body: <p>I've been trying to set up Clang on Windows. So far I survived building with Visual Studio and CMake, and a few other surprises. But it turns out that Clang does not come with its own C++ stdlib implementation, so I decided to use GCC 4.7.0's libstdc++ built for MinGW.</p>
  
  <p>To begin with, I added the search path to my HeaderSearchOptions.</p>
  
  <pre><code>headeropts-&gt;AddPath(path, clang::frontend::IncludeDirGroup::CXXSystem, true, false, false);
  </code></pre>
  
  <p>The path is exactly where the headers reside- I literally copied and pasted it from Windows Explorer (and then doubled the backslashes for escapes). But Clang still insists that it cannot find <code>&lt;iostream&gt;</code>, even though a file named <code>iostream</code> exists exactly at <code>path</code>.</p>
  
  <p>Why can't Clang find my header, and what else do I need to do to use libstdc++?</p>
  
  <p>Here's my code</p>
  
  <pre><code>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, &amp;diagopts);
  llvm::IntrusiveRefCntPtr&lt;clang::DiagnosticIDs&gt; diagids(new clang::DiagnosticIDs);
  clang::DiagnosticsEngine engine(diagids, &amp;diagopts, &amp;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, &amp;target);
  
  auto headeropts = llvm::IntrusiveRefCntPtr&lt;clang::HeaderSearchOptions&gt;(new clang::HeaderSearchOptions());
  headeropts-&gt;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-&gt;UseStandardCXXIncludes = true;
  headeropts-&gt;UseStandardSystemIncludes = true;
  clang::HeaderSearch hs(headeropts, fm, engine, langopts, targetinfo);
  
  auto x = llvm::IntrusiveRefCntPtr&lt;clang::PreprocessorOptions&gt;(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(&amp;m), engine);
  consumer.mod = &amp;codegen;
  clang::Sema sema(p, astcon, consumer, clang::TranslationUnitKind::TU_Complete);
  
  sm.createMainFileID(fm.getFile("main.cpp"));
  engine.getClient()-&gt;BeginSourceFile(langopts, &amp;p);
  clang::ParseAST(sema);
  </code></pre>