ZillGate
ZillGate

Reputation: 1183

Error when creating a global variable in llvm

I am trying to create a global variable in a function pass. The code is

gVar= new GlobalVariable(
    /*Type=*/Int32Type,
    /*isConstant=*/false,
    /*Linkage=*/GlobalValue::CommonLinkage,
    /*Initializer=*/0, // has initializer, specified below
    /*Name=*/"gVar",
    /*ThreadLocalMode*/GlobalVariable::InitialExecTLSModel);

However, I keep getting the following compiler error:

error: no matching function for call to ‘llvm::GlobalVariable::GlobalVariable(const llvm::Type*&, bool, llvm::GlobalValue::LinkageTypes, int, const char [4], llvm::GlobalVariable::ThreadLocalMode)’

Could you please tell me the right way to declare a global variable in llvm? Thank you very much!

In addition, I've referred to the header file:

http://llvm.org/docs/doxygen/html/GlobalVariable_8h_source.html

and this post

How can I declare a global variable in LLVM?

Upvotes: 3

Views: 3595

Answers (1)

Eli Bendersky
Eli Bendersky

Reputation: 273366

You need to pass a Module to the constructor. There are plenty of examples in the LLVM code base for creating global vars. For example, in examples/ExceptionDemo/ExceptionDemo.cpp:

new llvm::GlobalVariable(module,
                         stringConstant->getType(),
                         true,
                         llvm::GlobalValue::LinkerPrivateLinkage,
                         stringConstant,
                         "");

By the way - important note: you should not be creating new globals or doing anything else that modifies a module in a function pass. If you have to do that, you need a module pass.

Upvotes: 6

Related Questions