STenyaK STenyaK - 1 year ago 169
C Question

Can GCC not complain about undefined references?

Under what situation is it possible for GCC to not throw an "undefined reference" link error message when trying to call made-up functions?

For example, a situation in which this C code is compiled and linked by GCC:

void function()

...even though
is not present anywhere in the code (not headers, source files, declarations, nor any third party library).

Can that kind of code be accepted and compiled by GCC under certain conditions, without touching the actual code? If so, which?


EDIT: no previous declarations or mentions to
are present anywhere else. Meaning that a
grep -R
of the whole filesystem will only show that exact single line of code.

Answer Source

Yes, it is possible to avoid reporting undefined references - using --unresolved-symbols linker option.

g++ mm.cpp -Wl,--unresolved-symbols=ignore-in-object-files

From man ld


Determine how to handle unresolved symbols. There are four possible values for method:

           Do not report any unresolved symbols.

           Report all unresolved symbols.  This is the default.

           Report unresolved symbols that are contained in shared
           libraries, but ignore them if they come from regular object

           Report unresolved symbols that come from regular object
           files, but ignore them if they come from shared libraries.  This
           can be useful when creating a dynamic binary and it is known
           that all the shared libraries that it should be referencing
           are included on the linker's command line.

The behaviour for shared libraries on their own can also be controlled by the --[no-]allow-shlib-undefined option.

Normally the linker will generate an error message for each reported unresolved symbol but the option --warn-unresolved-symbols can change this to a warning.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download