John Galt
John Galt

Reputation: 125

LLVM IR can't access global variable

I am trying to compile a program with LLVM and I produce this code

@c = common global i32
@d = common global i32

declare i32 @writeln(i32)

define i32 @a() {
entry:
  store i32 2, i32* @c, align 4
  ret i32 2
}

define i32 @main() {
entry:
  %calltmp = call i32 @a()
  ret i32 0
}

and i get this error when trying to compile it to object file

Undefined symbols for architecture x86_64:
  "_c", referenced from:
      _a in a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Does anyone knows what i am doing wrong ?

Upvotes: 2

Views: 1593

Answers (1)

Frank C.
Frank C.

Reputation: 8088

To quote from the LLVM-IR Language Reference:

Global variable definitions must be initialized.

All global variable declarations define a pointer to a region of memory and all memory objects in LLVM are accessed through pointers.

This is relaxed if you are defining a pointer to an external value, for obvious reasons:

@G = external global i32

Upvotes: 1

Related Questions