Search code examples
c++linkage

Should I always give my global functions internal linkage?


I have a TU whose global functions won't be used by any other TUs. I read that declaring them as static gives them internal linkage, and this is good from an optimization standpoint. But I want to know what are the correct situations in which I should use them. Should I always give global functions/variables internal linkage when I know they won't be used anywhere else in the program?


Solution

  • Put them in an unnamed namespace instead.

    This is the idiomatic solution in C++ for functions that will be used only in the current TU.