[LLVMdev] LLVM Basic Program Compilation

Prakash Premkumar prakash.prax at gmail.com
Sat Aug 2 01:02:01 PDT 2014


Thanks a lot Tim.

I modified my code add the main function :

#include <stdio.h>
> #include "llvm/IR/LLVMContext.h"
> #include "llvm/IR/Module.h"
> #include "llvm/IR/IRBuilder.h"
> #include "llvm/IR/Function.h"
>
> int main()
> {
>   llvm::LLVMContext& context = llvm::getGlobalContext();
>   llvm::Module* module = new llvm::Module("top", context);
>   llvm::IRBuilder<> builder(context);
>   llvm::FunctionType *funcType =
>   llvm::FunctionType::get(builder.getInt32Ty(), false);
>   llvm::Function *mainFunc =llvm::Function::Create(funcType,
> llvm::Function::ExternalLinkage, "main", module);
>   module->dump( );
> }


and when i compile with the commands you specified i get the following
error:

Undefined symbols for architecture x86_64:
>   "llvm::FunctionType::get(llvm::Type*, bool)", referenced from:
>       _main in try-nnbqRa.o
>   "llvm::getGlobalContext()", referenced from:
>       _main in try-nnbqRa.o
>   "llvm::llvm_unreachable_internal(char const*, char const*, unsigned
> int)", referenced from:
>       llvm::User::operator delete(void*, unsigned int) in try-nnbqRa.o
>   "llvm::Type::getInt32Ty(llvm::LLVMContext&)", referenced from:
>       llvm::IRBuilderBase::getInt32Ty() in try-nnbqRa.o
>   "llvm::User::operator new(unsigned long, unsigned int)", referenced from:
>       llvm::Function::Create(llvm::FunctionType*,
> llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in
> try-nnbqRa.o
>   "llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&)", referenced
> from:
>       _main in try-nnbqRa.o
>   "llvm::Function::Function(llvm::FunctionType*,
> llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*)",
> referenced from:
>       llvm::Function::Create(llvm::FunctionType*,
> llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in
> try-nnbqRa.o
>   "llvm::Module::dump() const", referenced from:
>       _main in try-nnbqRa.o
> ld: symbol(s) not found for architecture x86_64


As you mentioned try.s is unlinked, Can you please tell me how i will link
it.

Basically I am trying to write a simple interpreter for a language which
has a while loop and translates the code to llvm IR. I am writing the
interpreter in C.

Thanks a lot


On Sat, Aug 2, 2014 at 1:07 PM, Tim Northover <t.p.northover at gmail.com>
wrote:

> Hi Prakash,
>
> The way you're trying to use LLVM seems like a very odd way to start.
>
> The natural beginning move would be to write a C++ program that
> produced a simple module and then try to execute that simple module
> (maybe with lli).
>
> But what you're doing is creating an (extremely complex!) module
> (try.s) that, when executed will use LLVM again to create a much
> simpler module. Rather more self-referential than I'd expect (and much
> more difficult to get right, as you're discovering).
>
> The way I'd expect you to be testing that program is:
> $ clang++ `llvm-config --cxx-flags --ldflags --libs` try.cpp -otry
> $ ./try 2> mymodule.ll
> $ lli mymodule.ll
>
> (You'll still get an error, because you need to define a @main
> function to be able to execute a module, but it's a much simpler
> error).
>
> > gives me Program used external function '_ZN4llvm16getGlobalContextEv'
> which
> > could not be resolved!
>
> I'll say a bit about this error anyway. The "try.s" module produced by
> your command has many dangling references to all the functions it
> refers to from the LLVM libraries. It's the equivalent of a plain
> object file ("clang -c xyz.c") and hasn't been linked at all, so you
> shouldn't expect to be able to execute it directly (without more
> work).
>
> Cheers.
>
> Tim.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140802/29736b9b/attachment.html>


More information about the llvm-dev mailing list