我正在尝试以编程方式使用clang将一些C ++编译为目标文件,并且在codegenaction期间我收到错误“无法与目标计算机接口”。我想知道我是否错过了一些步骤,否则问题可能是什么。
以下是该计划的输出:
C:\Application\Debug>Application
Initializing targets
Targets initialized
clang -cc1 version 3.4 based upon LLVM 3.4svn default target i686-pc-win32
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\lib\gcc\mingw32\4.7.2\include\c++
c:\mingw\lib\gcc\mingw32\4.7.2\include\c++\mingw32
c:\mingw\lib\gcc\mingw32\4.7.2\include\c++\backward
c:\mingw\lib\gcc\mingw32\4.7.2\include
c:\mingw\include
c:\mingw\lib\gcc\mingw32\4.7.2\include-fixed
End of search list.
error: unable to interface with target machine
1 error generated.
这是我(非常简单)的程序:
#include <string>
#include <memory>
#include <iostream>
#include <limits.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include "clang/CodeGen/CodeGenAction.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Tool.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/system_error.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include <Windows.h>
using namespace std;
using namespace llvm;
using namespace clang;
using namespace clang::driver;
int main(int argc, const char **argv, char * const *envp) {
llvm::InitializeNativeTarget();
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
TextDiagnosticPrinter *DiagClient =
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
driver::ArgStringList CCArgs;
CCArgs.push_back("-cc1");
CCArgs.push_back("-triple");
CCArgs.push_back("i686-pc-win32");
CCArgs.push_back("-emit-obj");
CCArgs.push_back("-O3");
CCArgs.push_back("-nostdsysteminc");
CCArgs.push_back("-mrelax-all");
CCArgs.push_back("-disable-free");
CCArgs.push_back("-v");
CCArgs.push_back("-coverage-file");
CCArgs.push_back("test.o");
CCArgs.push_back("-main-file-name");
CCArgs.push_back("test.cpp");
CCArgs.push_back("-mrelocation-model");
CCArgs.push_back("static");
CCArgs.push_back("-mdisable-fp-elim");
CCArgs.push_back("-fmath-errno");
CCArgs.push_back("-masm-verbose");
CCArgs.push_back("-mconstructor-aliases");
CCArgs.push_back("-target-cpu");
CCArgs.push_back("pentium4");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++\\mingw32");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++\\backward");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\include");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include-fixed");
CCArgs.push_back("-std=c++11");
CCArgs.push_back("-fdeprecated-macro");
CCArgs.push_back("-ferror-limit");
CCArgs.push_back("19");
CCArgs.push_back("-fmessage-length");
CCArgs.push_back("150");
CCArgs.push_back("-mstackrealign");
CCArgs.push_back("-fms-extensions");
CCArgs.push_back("-fmsc-version=1300");
CCArgs.push_back("-fobjc-runtime=gcc");
CCArgs.push_back("-fobjc-default-synthesize-properties");
CCArgs.push_back("-fcxx-exceptions");
CCArgs.push_back("-fexceptions");
CCArgs.push_back("-fdiagnostics-show-option");
CCArgs.push_back("-fcolor-diagnostics");
CCArgs.push_back("-backend-option");
CCArgs.push_back("-vectorize-loops");
CCArgs.push_back("-x");
CCArgs.push_back("c++");
CCArgs.push_back("-o");
CCArgs.push_back("test.o");
CCArgs.push_back("test.cpp");
OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
CompilerInvocation::CreateFromArgs(*CI,
const_cast<const char **>(CCArgs.data()),
const_cast<const char **>(CCArgs.data()) +
CCArgs.size(),
Diags);
// Create a compiler instance to handle the actual work.
CompilerInstance Clang;
Clang.setInvocation(CI.take());
// Create the compilers actual diagnostics engine.
Clang.createDiagnostics();
if (!Clang.hasDiagnostics())
return 1;
OwningPtr<CodeGenAction> Act(new EmitObjAction());
if (!Clang.ExecuteAction(*Act))
return 1;
return 0;
}