Allow a specific undefined reference with GCC

Viewed 180

In a C++ program with GCC I am using linker flag -Wl,--no-undefined so the linker will throw an error if there are undefined references. Is there a pragma or something I can put in the code next to a specific symbol that I want to allow to be undefined at link time?

1 Answers

One option is to make that symbol weak:

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong. During linking, a strong symbol can override a weak symbol of the same name. In contrast, two strong symbols that share a name yield a link error during link-time. When linking a binary executable, a weakly declared symbol does not need a definition. In comparison, (by default) a declared strong symbol without a definition triggers an undefined symbol link error.

E.g.:

#pragma weak power2
int power2(int x);
Related