LLVM Basic Program Compilation

I am just getting started with llvm.

Here’s code I am trying to compile:


#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"

int main()
{
  llvm::LLVMContext& context = llvm::getGlobalContext();
  llvm::Module* module = new llvm::Module("top", context);
  llvm::IRBuilder<> builder(context); 

  module->dump( );
}

when i compile with :

For some reasons lli and other tools is not compiled with -fPIC
-rdynamic flags,so you have to append -extra-object=/path/to/llvmlib
to let lli resolve them.

Thanks a lot waxiadao . can you kindly tell me the exact commands that I will have to use?

Hi Waxiado, Chris and Tim,

when i compiled with :

$ clang++ -g try.cpp llvm-config --cppflags --ldflags --libs core jit native-o3 -S -emit-llvm

I get the following warnings

clang: warning: -lz: ‘linker’ input unused
clang: warning: -lpthread: ‘linker’ input unused
clang: warning: -lcurses: ‘linker’ input unused
clang: warning: -lm: ‘linker’ input unused
clang: warning: -lLLVMX86Disassembler: ‘linker’ input unused
clang: warning: -lLLVMX86AsmParser: ‘linker’ input unused
clang: warning: -lLLVMX86CodeGen: ‘linker’ input unused
clang: warning: -lLLVMSelectionDAG: ‘linker’ input unused
clang: warning: -lLLVMAsmPrinter: ‘linker’ input unused
clang: warning: -lLLVMMCParser: ‘linker’ input unused
clang: warning: -lLLVMX86Desc: ‘linker’ input unused
clang: warning: -lLLVMX86Info: ‘linker’ input unused
clang: warning: -lLLVMX86AsmPrinter: ‘linker’ input unused
clang: warning: -lLLVMX86Utils: ‘linker’ input unused
clang: warning: -lLLVMJIT: ‘linker’ input unused
clang: warning: -lLLVMRuntimeDyld: ‘linker’ input unused
clang: warning: -lLLVMExecutionEngine: ‘linker’ input unused
clang: warning: -lLLVMCodeGen: ‘linker’ input unused
clang: warning: -lLLVMObjCARCOpts: ‘linker’ input unused
clang: warning: -lLLVMScalarOpts: ‘linker’ input unused
clang: warning: -lLLVMInstCombine: ‘linker’ input unused
clang: warning: -lLLVMTransformUtils: ‘linker’ input unused
clang: warning: -lLLVMipa: ‘linker’ input unused
clang: warning: -lLLVMAnalysis: ‘linker’ input unused
clang: warning: -lLLVMTarget: ‘linker’ input unused
clang: warning: -lLLVMMC: ‘linker’ input unused
clang: warning: -lLLVMObject: ‘linker’ input unused
clang: warning: -lLLVMCore: ‘linker’ input unused
clang: warning: -lLLVMSupport-o3: ‘linker’ input unused
clang: warning: argument unused during compilation: ‘-L/usr/local/lib’

and when i compile the .s file with lli

i get

LLVM ERROR: Program used external function ‘_ZN4llvm16getGlobalContextEv’ which could not be resolved!

Can you kindly help me resolve it?

Can you kindly help me resolve it?

Isn't this exactly where we started the thread? I think you've been
given quite a lot of advice here, but you seem to have just decided to
ignore it and go back to what you started with.

Most importantly, you still seem to be confused about exactly what
you're compiling and what you want the LLVM bitcode lli is executing
to do. You need to sort out that confusion before you've got any hope
of making significant progress.

I think the best advice was actually Chris's: the Kaleidoscope
tutorial (LLVM Tutorial: Table of Contents — LLVM 16.0.0git documentation) sounds very similar to what
you're trying to do.

Cheers.

Tim.

Thanks Tim. I will have a look at it.