2
votes

Programming Language : C

At our work,we have a project which has a header file say header1.h . This file contains some function which are declared as external scope (via extern) and also defined as inline in the same header file(header1.h).

Now this file is included at several places in different C files. My understanding is that it will produce an error of multiple definitions with my past experience with GCC , and that is what I expect. But at our work we do not get these errors. Only difference is that we are using different compiler driver.

From my past experience, the best guess that I am making is that, the symbols are generated as weak symbols at the time of compilation and linker is using that information to choose one of them.

Could functions defined as inline result in weak symbols ? Is it possible, or there might be some other reason.

Also if inline can result in creation of weak symbols ,would there be a feature to turn it off or on.

1
No, weak symbols are a GNU-extension and not even defined in the standard, AFAIK. inline functions have special semantics, which you can look up someplace here on Stack Overflow. - cadaniluk
@Downvoter Weak symbols are part of the ELF standard: A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. ... - Andrew Henle
@AndrewHenle Of course, I mean the C standard, I forgot to write that. Being part of the ELF standard makes it not a GNU-specific extension but renders it implementation-defined anyway, independent of the standard. - cadaniluk

1 Answers

0
votes

If a function is inline, the entire function body will be copied in every time the function is used (instead of the normal assembler call/return semantic).

(Modern compilers, uses inline as a hint, and the actual result might just be a static function, with a unique copy in every compiled file it was used)