title: How widely supported is pragma weak, and does it overcome the issues of using gcc attribute?
body: <p>I just discovered the <code>#pragma weak</code> directive in GCC:</p>
  
  <blockquote>
    <p>6.57.9 Weak Pragmas</p>
    
    <p>For compatibility with SVR4, GCC supports a set of #pragma directives for declaring symbols to be weak, and defining weak aliases.</p>
    
    <p><code>#pragma weak symbol</code></p>
    
    <p>This pragma declares symbol to be weak, as if the declaration had the attribute of the same name. The pragma may appear before or after the declaration of symbol. It is not an error for symbol to never be defined at all. </p>
    
    <p><code>#pragma weak symbol1 = symbol2</code></p>
    
    <p>This pragma declares symbol1 to be a weak alias of symbol2. It is an error if symbol2 is not defined in the current translation unit.</p>
  </blockquote>
  
  <p><a href="http://gcc.gnu.org/onlinedocs/gcc/Weak-Pragmas.html" rel="nofollow noreferrer">http://gcc.gnu.org/onlinedocs/gcc/Weak-Pragmas.html</a></p>
  
  <p>Despite the fact that the GCC developers generally don't like <code>#pragma</code> and encourage you to use <code>__attribute__</code> instead for all sorts of things that could be pragmas, I'm inclined to believe <code>#pragma weak</code> may actually be superior to the attribute-based approach, which looks like:</p>
  
  <pre><code>extern __typeof(old_name) new_name __attribute__(weak, alias("old_name"))
  </code></pre>
  
  <p>Aside from the ugliness of requiring <code>__typeof</code> (or requiring you to know the type and spell it out explicitly, even if it's a really complex function type), the biggest issue of the attribute based approach is that <code>"old_name"</code> must be passed to gcc <strong>as a string</strong> to be pasted literally into the generated assembly. This is problematic because different systems have different name-mangling characteristics (most popular is prefixing an underscore on all C symbol names, or doing nothing at all), and to pass the correct string to the <code>alias</code> attribute, you need to know the name mangling convention of the system you're building for, which is really not knowledge that belongs in an application-level library where weak aliases might be useful.</p>
  
  <p>The syntax <code>#pragma weak new_name = old_name</code> seems to avoid this issue by handling both names at the compiler level, where t can mangle them both appropriately, unless I'm mistaken.</p>
  
  <p>So with all the preliminaries finished, my actual questions are: <strong>Am I mistaken about <code>#pragma weak</code> having this "portability" advantage?</strong> and <strong>Do all modern compilers on unix-like systems (gcc, pcc, tinycc, icc, llvm/clang, etc.) still support the traditional SVR4 <code>#pragma weak</code>?</strong></p>
  
  <p>I'm aware of the following similar question, but it does not seem quite the same and the answers to not satisfactorily address my question:</p>
  
  <p><a href="https://stackoverflow.com/questions/1410811/how-portable-is-weak-linking-pragma-weak-my-symbol">How portable is weak linking? #pragma weak my_symbol</a></p>