title: LLVM-C creating object file results in: "TargetMachine can't emit a file of this type"
body: <p>Trying to generate a very simple object file with LLVM-C. Unfortunately I'm still stuck on "TargetMachine can't emit a file of this type" tried reordering code and various things for CPU (x64-64, generic and LLVMGetHostCPUName()). Clearly something (hopefully obvious) is missing here.</p>
  
  <p>The code below is compiled with <code>clang -Wall -O2 test.c LLVM-C.dll -o test</code></p>
  
  <p>Output:</p>
  
  <pre><code>target: x86-64, [64-bit X86: EM64T and AMD64], 1, 1
  triple: x86_64-pc-windows-msvc
  features: +sse2,+cx16,+sahf,-tbm,-avx512ifma,+sha,-gfni,-fma4,-vpclmulqdq,+prfchw,+bmi2,-cldemote,+fsgsbase,-ptwrite,+xsavec,+popcnt,+aes,-avx512bitalg,-movdiri,+xsaves,-avx512er,-avx512vnni,-avx512vpopcntdq,-pconfig,-clwb,-avx512f,+clzero,-pku,+mmx,-lwp,-rdpid,-xop,+rdseed,-waitpkg,-movdir64b,+sse4a,-avx512bw,+clflushopt,+xsave,-avx512vbmi2,+64bit,-avx512vl,-invpcid,-avx512cd,+avx,-vaes,+cx8,+fma,-rtm,+bmi,-enqcmd,+rdrnd,+mwaitx,+sse4.1,+sse4.2,+avx2,+fxsr,-wbnoinvd,+sse,+lzcnt,+pclmul,-prefetchwt1,+f16c,+ssse3,-sgx,-shstk,+cmov,-avx512vbmi,-avx512bf16,+movbe,+xsaveopt,-avx512dq,+adx,-avx512pf,+sse3
  datalayout: e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
  error: (null)
  error: TargetMachine can't emit a file of this type
  </code></pre>
  
  <p>Module.txt:</p>
  
  <pre><code>; ModuleID = 'test'
  source_filename = "test"
  target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
  target triple = "x86_64-pc-windows-msvc"
  
  define float @ftest() {
  ftest:
    ret float 0x401B333340000000
  }
  </code></pre>
  
  <p>Code (test.c):</p>
  
  <pre><code>#include &lt;llvm-c/Core.h&gt;
  #include &lt;llvm-c/Analysis.h&gt;
  #include &lt;llvm-c/TargetMachine.h&gt;
  
  #include &lt;stdio.h&gt;
  
  int main(){
      LLVMInitializeNativeTarget();
      LLVMTargetRef target = LLVMGetFirstTarget();
      printf("target: %s, [%s], %d, %d\n", LLVMGetTargetName(target), LLVMGetTargetDescription(target), LLVMTargetHasJIT(target), LLVMTargetHasTargetMachine(target));
      printf("triple: %s\n", LLVMGetDefaultTargetTriple());
      printf("features: %s\n", LLVMGetHostCPUFeatures());
      LLVMTargetMachineRef machine = LLVMCreateTargetMachine(target, LLVMGetDefaultTargetTriple(), "generic", LLVMGetHostCPUFeatures(), LLVMCodeGenLevelDefault, LLVMRelocDefault, LLVMCodeModelDefault);
  
      LLVMContextRef context = LLVMContextCreate();
      LLVMModuleRef module = LLVMModuleCreateWithNameInContext("test", context);
  
      LLVMSetTarget(module, LLVMGetDefaultTargetTriple());
      LLVMTargetDataRef datalayout = LLVMCreateTargetDataLayout(machine);
      char* datalayout_str = LLVMCopyStringRepOfTargetData(datalayout);
      printf("datalayout: %s\n", datalayout_str);
      LLVMSetDataLayout(module, datalayout_str);
      LLVMDisposeMessage(datalayout_str);
  
      LLVMTypeRef f32 = LLVMFloatTypeInContext(context);
      LLVMTypeRef ftype = LLVMFunctionType(f32, 0, 0, 0);
      LLVMValueRef ftest = LLVMAddFunction(module, "ftest", ftype);
      LLVMBasicBlockRef bb = LLVMAppendBasicBlockInContext(context, ftest, "ftest");
      LLVMBuilderRef builder = LLVMCreateBuilderInContext(context);
      LLVMPositionBuilderAtEnd(builder, bb);
      LLVMValueRef v1 = LLVMConstReal(f32, 2.5);
      LLVMValueRef v2 = LLVMConstReal(f32, 4.3);
      LLVMValueRef result = LLVMBuildFAdd(builder, v1, v2, "f-add");
      LLVMBuildRet(builder, result);
      LLVMVerifyFunction(ftest, LLVMPrintMessageAction);
  
      char* errors = 0;
      LLVMPrintModuleToFile(module, "module.txt", &amp;errors);
      printf("error: %s\n", errors);
      LLVMDisposeMessage(errors);
  
      LLVMTargetMachineEmitToFile(machine, module, "result.o", LLVMObjectFile, &amp;errors);
      printf("error: %s\n", errors);
      LLVMDisposeMessage(errors);
      return 0;
  }
  </code></pre>