C/C++ static function vs non-static function impact on linking time

Viewed 292

The functions are usually declared non-static because they may be called in other object files.

I'm just curious if having

  • several functions defined and used only in the source file,
  • having many source files of this type and
  • declaring those functions static instead of non-static,

it would improve the linking time. Is it worth making them static if are only used and defined in same source file?

1 Answers

If a function is defined and used in a single source file, it should always either be static, or defined in an anonymous namespace. This is because you risk violating ODR. Also, this allows the compiler to completely remove the function from the binary if it can be inlined everywhere. This is not possible for non-static functions.

About linking times, you should probably profile that.

Related