[LLVMdev] Variable declarations vs. definitions
Dustin Laurence
dllaurence at dslextreme.com
Sat Jan 9 21:31:12 PST 2010
On 01/09/2010 01:12 PM, Chris Lattner wrote:
> The equivalent of "extern int G;" is:
>
> @G = external global i32
Hmm. Is it really? This
@foo = external global i32
@foo = global i32 5
define i32
@main(i32 %argc, i8 **%argv)
{
%fooVal = load i32* @foo
ret i32 %fooVal
}
produces a "redefinition of global '@foo'" error. But this
extern int x;
int x = 5;
int *y = &x;
compiles to
@x = global i32 5 ; <i32*> [#uses=1]
@y = global i32* @x ; <i32**> [#uses=0]
The difference is crucial, because I want to put
"@foo = external global i32"
in a header file that is then #included in every module where it is
used, *including the defining module* (for a consistency check, and
because otherwise I'd have to create extra headers that are only
#included by the outside world but not the defining module). It appears
that the front end is supposed to decide between external and global.
In my case I actually could maintain all declarations by hand for one
global word used to return exception information, but that wouldn't work
for a more involved case.
Is there no way to get the same effect as with define/declare for
functions? There I have no problem #includeing a declaration into the
same file as a definition.
The alternative appears to be asking the linker to do it. The docs for
"linkonce" say "This is typically used to implement inline functions,
templates, or other code which must be generated in each translation
unit that uses it." That sounds like my case, and it compiles, but I
don't know if that's gong to get me into trouble.
Dustin
More information about the llvm-dev
mailing list