简体   繁体   中英

making global variables “hidden” and “aliased”

Is there any way to make a global variable both "hidden" and "aliased"? It would seem the following would be sufficient:

int __voo __attribute__((visibility("hidden")));
extern int voo __attribute__((weak,alias("__voo")));

But this results in:

$ gcc -c alias.c
/tmp/cczCek0H.s: Assembler messages:
/tmp/cczCek0H.s:5: Error: `voo' can't be equated to common symbol '__voo'

Removing "weak" has no effect. However, adding "static" to the variable declaration allows the link to succeed, but is not the desired effect, as static limits the variable visibility scope to the compilation unit, not the broader scope of the linked object.

Intel System Studio 2019, GCC 4.8.5 and GCC 5.3.1 all show the same behavior.

I had a similar issue. I wasn't even trying to use hidden, and I still got the "can't be equated to common symbol" error. The fix was to initialize the int __voo to some value (even 0). Perhaps it was a BSS vs Data thing, or just a bug.

I got it to work by also supplying -fno-common to the compiler invocation. This might make some not strictly correct C code stop working though, so read what -fno-common does before using it.

It also works if I compile with clang .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM