<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-text-flowed" style="font-family: -moz-fixed;
      font-size: 14px;" lang="x-western">I'm attempting to use clang
      programatically to compile some C++ to an object file and I get
      the error "unable to interface with target machine" during the
      codegenaction. I'm wondering if I have missed some step or
      otherwise what the problem could be. <br>
      <br>
      Below is the output of the program:
      <br>
      <br>
      C:\Application\Debug>Application
      <br>
      Initializing targets
      <br>
      Targets initialized
      <br>
      clang -cc1 version 3.4 based upon LLVM 3.4svn default target
      i686-pc-win32
      <br>
      #include "..." search starts here:
      <br>
      #include <...> search starts here:
      <br>
       c:\mingw\lib\gcc\mingw32\4.7.2\include\c++
      <br>
       c:\mingw\lib\gcc\mingw32\4.7.2\include\c++\mingw32
      <br>
       c:\mingw\lib\gcc\mingw32\4.7.2\include\c++\backward
      <br>
       c:\mingw\lib\gcc\mingw32\4.7.2\include
      <br>
       c:\mingw\include
      <br>
       c:\mingw\lib\gcc\mingw32\4.7.2\include-fixed
      <br>
      End of search list.
      <br>
      error: unable to interface with target machine
      <br>
      1 error generated.
      <br>
      <br>
      <br>
      Here is my (very simple) program:
      <br>
      <br>
      <br>
      #include <string>
      <br>
      #include <memory>
      <br>
      #include <iostream>
      <br>
      #include <limits.h>
      <br>
      <br>
      #include <llvm/Bitcode/ReaderWriter.h>
      <br>
      #include "clang/CodeGen/CodeGenAction.h"
      <br>
      #include "clang/Basic/DiagnosticOptions.h"
      <br>
      #include "clang/Driver/Compilation.h"
      <br>
      #include "clang/Driver/Driver.h"
      <br>
      #include "clang/Driver/Tool.h"
      <br>
      #include "clang/Frontend/CompilerInstance.h"
      <br>
      #include "clang/Frontend/CompilerInvocation.h"
      <br>
      #include "clang/Frontend/FrontendDiagnostic.h"
      <br>
      #include "clang/Frontend/TextDiagnosticPrinter.h"
      <br>
      #include "llvm/ADT/OwningPtr.h"
      <br>
      #include "llvm/ADT/SmallString.h"
      <br>
      #include "llvm/ExecutionEngine/ExecutionEngine.h"
      <br>
      #include "llvm/ExecutionEngine/JIT.h"
      <br>
      #include "llvm/IR/Module.h"
      <br>
      #include "llvm/IR/LLVMContext.h"
      <br>
      #include "llvm/Support/system_error.h"
      <br>
      #include "llvm/Support/Host.h"
      <br>
      #include "llvm/Support/ManagedStatic.h"
      <br>
      #include "llvm/Support/Path.h"
      <br>
      #include "llvm/Support/TargetSelect.h"
      <br>
      #include "llvm/Support/raw_ostream.h"
      <br>
      <br>
      #include <Windows.h>
      <br>
      <br>
      using namespace std;
      <br>
      using namespace llvm;
      <br>
      using namespace clang;
      <br>
      using namespace clang::driver;
      <br>
      <br>
      int main(int argc, const char **argv, char * const *envp) {
      <br>
      <br>
          llvm::InitializeNativeTarget();
      <br>
      <br>
          IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new
      DiagnosticOptions();
      <br>
          TextDiagnosticPrinter *DiagClient =
      <br>
              new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
      <br>
      <br>
          IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new
      DiagnosticIDs());
      <br>
          DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
      <br>
      <br>
          driver::ArgStringList CCArgs;
      <br>
      <br>
          CCArgs.push_back("-cc1");
      <br>
          CCArgs.push_back("-triple");
      <br>
          CCArgs.push_back("i686-pc-win32");
      <br>
          CCArgs.push_back("-emit-obj");
      <br>
          CCArgs.push_back("-O3");
      <br>
          CCArgs.push_back("-nostdsysteminc");
      <br>
          CCArgs.push_back("-mrelax-all");
      <br>
          CCArgs.push_back("-disable-free");
      <br>
          CCArgs.push_back("-v");
      <br>
          CCArgs.push_back("-coverage-file");
      <br>
          CCArgs.push_back("test.o");
      <br>
          CCArgs.push_back("-main-file-name");
      <br>
          CCArgs.push_back("test.cpp");
      <br>
          CCArgs.push_back("-mrelocation-model");
      <br>
          CCArgs.push_back("static");
      <br>
          CCArgs.push_back("-mdisable-fp-elim");
      <br>
          CCArgs.push_back("-fmath-errno");
      <br>
          CCArgs.push_back("-masm-verbose");
      <br>
          CCArgs.push_back("-mconstructor-aliases");
      <br>
          CCArgs.push_back("-target-cpu");
      <br>
          CCArgs.push_back("pentium4");
      <br>
          CCArgs.push_back("-internal-isystem");
      <br>
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++");
      <br>
          CCArgs.push_back("-internal-isystem");
      <br>
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++\\mingw32");
      <br>
          CCArgs.push_back("-internal-isystem");
      <br>
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++\\backward");
      <br>
          CCArgs.push_back("-internal-isystem");
      <br>
      CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include");
      <br>
          CCArgs.push_back("-internal-isystem");
      <br>
          CCArgs.push_back("c:\\mingw\\include");
      <br>
          CCArgs.push_back("-internal-isystem");
      <br>
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include-fixed");
      <br>
          CCArgs.push_back("-std=c++11");
      <br>
          CCArgs.push_back("-fdeprecated-macro");
      <br>
          CCArgs.push_back("-ferror-limit");
      <br>
          CCArgs.push_back("19");
      <br>
          CCArgs.push_back("-fmessage-length");
      <br>
          CCArgs.push_back("150");
      <br>
          CCArgs.push_back("-mstackrealign");
      <br>
          CCArgs.push_back("-fms-extensions");
      <br>
          CCArgs.push_back("-fmsc-version=1300");
      <br>
          CCArgs.push_back("-fobjc-runtime=gcc");
      <br>
          CCArgs.push_back("-fobjc-default-synthesize-properties");
      <br>
          CCArgs.push_back("-fcxx-exceptions");
      <br>
          CCArgs.push_back("-fexceptions");
      <br>
          CCArgs.push_back("-fdiagnostics-show-option");
      <br>
          CCArgs.push_back("-fcolor-diagnostics");
      <br>
          CCArgs.push_back("-backend-option");
      <br>
          CCArgs.push_back("-vectorize-loops");
      <br>
          CCArgs.push_back("-x");
      <br>
          CCArgs.push_back("c++");
      <br>
          CCArgs.push_back("-o");
      <br>
          CCArgs.push_back("test.o");
      <br>
          CCArgs.push_back("test.cpp");
      <br>
      <br>
          OwningPtr<CompilerInvocation> CI(new
      CompilerInvocation);
      <br>
          CompilerInvocation::CreateFromArgs(*CI,
      <br>
              const_cast<const char **>(CCArgs.data()),
      <br>
              const_cast<const char **>(CCArgs.data()) +
      <br>
              CCArgs.size(),
      <br>
              Diags);
      <br>
      <br>
          // Create a compiler instance to handle the actual work.
      <br>
          CompilerInstance Clang;
      <br>
          Clang.setInvocation(CI.take());
      <br>
      <br>
          // Create the compilers actual diagnostics engine.
      <br>
          Clang.createDiagnostics();
      <br>
          if (!Clang.hasDiagnostics())
      <br>
              return 1;
      <br>
      <br>
          OwningPtr<CodeGenAction> Act(new EmitObjAction());
      <br>
          if (!Clang.ExecuteAction(*Act))
      <br>
              return 1;
      <br>
      <br>
          return 0;
      <br>
      }
      <br>
      <br>
      <br>
    </div>
  </body>
</html>