Reputation: 134
I'm writing a compiler for the Decaf language. I'm done with the lexer (flex) and parser (bison). Also, I've generated the AST and implemented type checking. I am now trying to implement codegen using LLVM IR.
However, while linking the files, g++ throws up errors saying it cant find llvm::getGlobalContext() etc.
g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` -c ast.cc
bison -d -t -y --verbose parser.y
g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` y.tab.c -c
flex -8 lexer.l
g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` lex.yy.c -c -lfl
g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` -c symtable.cc
g++ -O2 -Wno-write-strings -std=c++11 `llvm-config --cxxflags` ast.o lex.yy.o y.tab.o symtable.o -o dcc -lfl
ast.o: In function `IntegerConstant::Codegen()':
ast.cc:(.text._ZN15IntegerConstant7CodegenEv+0x6): undefined reference to `llvm::getGlobalContext()'
ast.cc:(.text._ZN15IntegerConstant7CodegenEv+0xe): undefined reference to `llvm::Type::getInt64Ty(llvm::LLVMContext&)'
ast.cc:(.text._ZN15IntegerConstant7CodegenEv+0x1f): undefined reference to `llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)'
ast.o: In function `CharConstant::Codegen()':
ast.cc:(.text._ZN12CharConstant7CodegenEv+0x7): undefined reference to `llvm::getGlobalContext()'
ast.cc:(.text._ZN12CharConstant7CodegenEv+0xf): undefined reference to `llvm::Type::getInt64Ty(llvm::LLVMContext&)'
ast.cc:(.text._ZN12CharConstant7CodegenEv+0x20): undefined reference to `llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)'
ast.o: In function `BooleanConstant::Codegen()':
ast.cc:(.text._ZN15BooleanConstant7CodegenEv+0x6): undefined reference to `llvm::getGlobalContext()'
ast.cc:(.text._ZN15BooleanConstant7CodegenEv+0xe): undefined reference to `llvm::Type::getInt64Ty(llvm::LLVMContext&)'
ast.cc:(.text._ZN15BooleanConstant7CodegenEv+0x1f): undefined reference to `llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)'
ast.o: In function `_GLOBAL__sub_I_ast.cc':
ast.cc:(.text.startup._GLOBAL__sub_I_ast.cc+0x2b): undefined reference to `llvm::getGlobalContext()'
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'dcc' failed
make: *** [dcc] Error 1
I've included the LLVM headers in ast.h, and nowhere else (seeing as I only call the codegen() methods in ast.cc
I'd be happy to provide more context if it helps. Also, the project compiles without errors in case I comment out the llvm parts (which for now include only a couple of codegen functions for integer/bool/char constant classes in my AST.
Here's my Makefile, if it helps :
CCC = g++
CCFLAGS= -O2 -Wno-write-strings -std=c++11
LLVMFLAGS = `llvm-config --cxxflags`
LEX = flex
LFLAGS= -8
YACC= bison
YFLAGS= -d -t -y --verbose
RM = /bin/rm -f
dcc: ast.o y.tab.o lex.yy.o symtable.o
${CCC} ${CCFLAGS} ${LLVMFLAGS} ast.o lex.yy.o y.tab.o symtable.o -o dcc -lfl
symtable.o: symtable.h symtable.cc
${CCC} ${CCFLAGS} ${LLVMFLAGS} -c symtable.cc
ast.o: ast.cc ast.h
${CCC} ${CCFLAGS} ${LLVMFLAGS} -c ast.cc
y.tab.o: parser.y
${YACC} ${YFLAGS} parser.y
${CCC} ${CCFLAGS} ${LLVMFLAGS} y.tab.c -c
lex.yy.o: lexer.l
${LEX} $(LFLAGS) lexer.l
${CCC} ${CCFLAGS} ${LLVMFLAGS} lex.yy.c -c -lfl
clean:
/bin/rm -f lex.yy.* y.tab.* *.o *.output dcc
Kindly help.
I've defined the virtual Codegen() functions inside ast.h, and their implementations inside ast.cc
I'm using
using namespace llvm;
Value *ErrorV(const char *Str) { return 0; }
static Module *TheModule;
static IRBuilder<> Builder(getGlobalContext());
static std::map<std::string, Value*> NamedValues;
Value *IntegerConstant::Codegen() {
return ConstantInt::get(Type::getInt64Ty(getGlobalContext()), val_, true);
}
at the end of ast.cc (as all other functions I wrote were under
using namespace std;
Upvotes: 1
Views: 1539
Reputation: 36
To link, you need "llvm-config --ldflags --libs". I suggest using all three flags together in your LLVMFLAGS variable.
Upvotes: 2