GCC -flto change character visibility

I have a big piece of code that creates compilation errors using -flto only on some versions of gcc. I will try to summarize below

in file1.h

 extern char A [100]; 

in file1.c

 #include "file1.h" char A[100]; 

I also have C ++ code that uses the variable A C ++ code is compiled into an .o file, and then it all compiled with something like

 gcc file1.c cpp.o 

Using the gcc version on archlinux (5.2.0), there is no problem with or without -flto . However, using gcc on Ubuntu 14.04 (4.8.4), when the code is compiled with -flto , A becomes a local variable. I checked this with nm:

This is the output from nm a.out for the variable in question.

Ubuntu, no lto (similar arch, with a different number):

 00000000006162e0 BA 

Ubuntu lto

 00000000006092c0 b A.2722 

I understand that B is for a global variable, but B is not.

How can I guarantee that A supported as a global variable, even when I use -flto in Ubuntu?

+5
source share
1 answer

Considering this and this , it certainly seems to be a bug in the specific version of gcc you are using (gcc 4.8.4 comes bundled with Ubuntu 14.04).

Using code snippets from a question,
I was able to reproduce the behavior on my machine with Ubuntu 14.04.

A simple workaround for this is to explicitly mark the corresponding character as used .

file1.c has char A[100];

file2.c has __attribute__((used)) char A[100];

 gcc file1.c -o file1-default.o nm file1-default.o | grep "A$" 0000000000601060 BA <-- symbol is a global. gcc file1.c -flto -o file1-flto.o nm file1-flto.o | grep "A$" 0000000000601060 b A.2385 <-- symbol updated to a local. gcc file2.c -flto -o file2.o nm file2.o | grep "A$" 0000000000601060 BA <-- symbol retained as global. 
+2
source

All Articles