title: Debugging Symbols Lost When Linking?
body: <p>I'm trying to compile a program with debugging symbols so that valgrind will give me line numbers. I have found that if I compile a simple test program in one go (with -g) then it contains the symbols. However, if I compile in two passes (i.e. compile then link) then it does not contain the debugging symbols. </p>
  
  <p>Here's the compile command for the single pass case:</p>
  
  <pre><code>g++ -g file.c -o file
  </code></pre>
  
  <p>And for two passes</p>
  
  <pre><code>g++ -g -c file.c -o file.o
  g++ -g file.o -o file
  </code></pre>
  
  <p>The actual program looks like this and contains a simple Invalid Write</p>
  
  <pre><code>int main(){
      int* x = new int[10];
      x[10]=1;
  </code></pre>
  
  <p>}</p>
  
  <p>If I compile with one pass then valgrind gives the following (note the line number at the end)</p>
  
  <pre><code>==24114== 40 bytes in 1 blocks are definitely lost in loss record 2 of 9
  ==24114==    at 0xB823: malloc (vg_replace_malloc.c:266)
  ==24114==    by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==24114==    by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==24114==    by 0x100000F09: main (file.c:3)
  </code></pre>
  
  <p>whereas if I compile in two passes I get this (with no line number):</p>
  
  <pre><code>==24135== 40 bytes in 1 blocks are definitely lost in loss record 2 of 9
  ==24135==    at 0xB823: malloc (vg_replace_malloc.c:266)
  ==24135==    by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==24135==    by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
  ==24135==    by 0x100000F09: main (in ./file)
  </code></pre>
  
  <p>Any insight on this would be much appreciated. I am using gcc version 4.2.1 on OS X 10.7.3 </p>