How do you suppress GCC linker warnings?

Recently, I was on a crusade to remove the warning from our code and become more familiar with the GCC warnings (eg -Wall, -Wno-<warning to disable>, -fdiagnostics-show-optionetc.). However, I was not able to figure out how to disable (or even control) linker warnings. The most common linker warning that I get is in the following form:

ld: warning: <some symbol> has different visibility (default) in 
<path/to/library.a> and (hidden) in <path/to/my/class.o>

The reason I was getting this was because the library I used was built using visibility defaultwhile my application was built with visibility hidden. I fixed this by restoring the library with visibility hidden.

My question though: how can I suppress this warning if I want? This is not what I need to do now, when I figured out how to fix it, but I'm still interested in knowing how you suppress this particular warning or any linker warnings in general?

Using -fdiagnostics-show-optionC / C ++ / linker for any of the flags does not say where this warning comes from other compiler warnings.

+5
source share
2 answers

In fact, you cannot turn off the GCC linker warning, because it is stored in a specific section of the binary library that you are communicating with. (The section is called .gnu.warning.symbol)

, , , ( libc-symbols.h):

:

#include <sys/stat.h>

int main()
{
    lchmod("/path/to/whatever", 0666);
    return 0;
}

:

$ gcc a.c
/tmp/cc0TGjC8.o: in function ยซ main ยป:
a.c:(.text+0xf): WARNING: lchmod is not implemented and will always fail

:

#include <sys/stat.h>

/* We want the .gnu.warning.SYMBOL section to be unallocated.  */
#define __make_section_unallocated(section_string)    \
  __asm__ (".section " section_string "\n\t.previous");

/* When a reference to SYMBOL is encountered, the linker will emit a
   warning message MSG.  */
#define silent_warning(symbol) \
  __make_section_unallocated (".gnu.warning." #symbol) 

silent_warning(lchmod)

int main()
{
    lchmod("/path/to/whatever", 0666);
    return 0;
}

:

$ gcc a.c
/tmp/cc195eKj.o: in function ยซ main ยป:
a.c:(.text+0xf): WARNING:

:

#include <sys/stat.h>

#define __hide_section_warning(section_string)    \
    __asm__ (".section " section_string "\n.string \"\rHello world!                      \"\n\t.previous");

/* If you want to hide the linker output */
#define hide_warning(symbol) \
  __hide_section_warning (".gnu.warning." #symbol) 


hide_warning(lchmod)

int main()
{
    lchmod("/path/to/whatever", 0666);
    return 0;
}

:

$ gcc a.c
/tmp/cc195eKj.o: in function ยซ main ยป:
Hello world!

, Hello world! , .

+4

, ld, , . , , -Wl,--warn-once g++ ( --warn-once ld).

0

All Articles