[llvm-dev] Create the GlobalVariable which have extern in one header file

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Thu Feb 21 21:02:34 PST 2019

On Thu, 21 Feb 2019 at 18:42, Mustakimur Khandaker via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> GlobalVariable *gvar_data = new GlobalVariable(
>     M, blockItems->getType(), true, GlobalValue::CommonLinkage,
>     blockItems, "DATA_TABLE");
> gvar_data->setAlignment(16);
> gvar_data->setSection("data_section");
> gvar_data->addAttribute(llvm::Attribute::OptimizeNone);
> I am not sure if I am using the correct Linkage or not.
> The pass has failed to complete it. Here is the runtime fault. Any guess what I am doing incorrect?

As you've guessed, it's the linkage.

"common" is the name for the C situation where multiple source files
simply declare "int a;" with nothing else specified. The names get
merged, and the variable gets zero-initialized by default. A "real"
definition (like you're trying to provide) would take precedence over
this and specify the actual data.

You almost certainly want GlobalValue::ExternalLinkage for a
bog-standard global variable.



More information about the llvm-dev mailing list