title: "Undefined reference " to a function defined in static library
body: <p>I am trying to build a Library to use in an application. I built the library as below, and when i compile the application i get the below error:</p>
  
  <p>I have done the beolw things. </p>
  
  <p>I use:</p>
  
  <pre><code>gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
  </code></pre>
  
  <h2>In Library to be called from application:</h2>
  
  <p>Here i have lot of modules, but entry point to this library is func() 
  (i.e., main () is replaced with func() so that i can call the module, also func () is not declared as 'static'.)</p>
  
  <p>In one of files: </p>
  
  <pre><code>int func ();
  ...
  
  int func () 
  { ... } 
  </code></pre>
  
  <p>Then built the Library as:</p>
  
  <pre><code>gcc -Wall file.c -o file.o
  ...
  ...
  
  ar rvs libfun.a $(OBJS)
  </code></pre>
  
  <p>Also used ranlib and nm -s on libfun.a seperately to build symbol table, 
  but the total size of archive did not change after using these commands and still got the linking error.
  Here $(OBJS) contains all the object files</p>
  
  <h2>In Application:</h2>
  
  <p>extern int func ();</p>
  
  <p>Compile with:</p>
  
  <pre><code>gcc -Wall -L./path-to-lib  -lfun  -o appl
  </code></pre>
  
  <p>Then i get the below error:</p>
  
  <pre><code>In function `main':
  undefined reference to `func()'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I tried to build symbol table with "ar s" and "ranlib" but the results is same.</p>
  
  <p>One thing i observed is there is a difference in contents of "ar" which i built and the archives already present in project for other modules.</p>
  
  <p>The archive built by me contains  (ouput with "nm -s libfun.a" ):</p>
  
  <pre><code>Archive index:
  Cfg1 in f1.o
  mCfg1 in f1.o
  dpCfg in f1.o
  </code></pre>
  
  <p>But the other archives which i am using without any changes contain below strange pattern:</p>
  
  <pre><code>Archive index:
  _Z29platformSetjP38tagTCPIP_INTERFACE_INSTANCE_ATTRIBUTES in platform.o
  _Z27platformTestSetTcpjP20tagTCPIP_CONFIG_DATAPh in platform.o
  _Z23platformSetTcpIpjP20tagTCPIP_CONFIG_DATA in platform.o
  </code></pre>
  
  <p>I am not sure what is the difference above. Is it a shared Library or a Static library ?</p>
  
  <p>I am trying to compile with GCC and build archive with 'ar', but the other library files may be using g++ compiler. I am not sure. Just in case it matters.</p>
  
  <p>What am i doing wrong here in building my library ? Please help?</p>
  
  <p>Regards.</p>