title: gcc: duplicate identifiers in object files or libraries
body: <p>Duplicate identifiers are simply ignored by the archiver and also by the linker. They are detected only if the source or object files are passed directly to gcc, which considers multiple definitions an error. As well it should. The archiver/linker simply ignores them and we end up with only one of the identifiers in the executable. Which one it is depends on the order in which the object file or library gets passed. Below is a script to demonstrate this.</p>
  
  <p>Is there any way to make the archiver/linker consider this an error? Or are there other tools to deal with this?</p>
  
  <pre><code>#!/bin/sh
  
  dir=$(mktemp -d)
  echo "Using temporary directory: $dir"
  echo
  cd "$dir"
  
  create_lib()
  {
      lib=$1
      shift
      rm -f $lib
      ar r $lib "$@" 2&gt;/dev/null
  }
  
  make_file()
  {
      base=file$1
      cat &gt;$base.c &lt;&lt;-EOF
      int f()
      {
          return $1;
      }
      EOF
  
      gcc $base.c -o $base.o -c
  
      create_lib lib$base.a $base.o
  }
  
  make_file 1
  make_file 2
  
  cat &gt;main.c &lt;&lt;EOF
  int main()
  {
      extern int f();
      return f();
  }
  EOF
  
  gcc main.c -omain.o -c
  
  cat &lt;&lt;EOF
  1. Passing duplicate functions to archiver in different order
     produces no error, but different result:
  EOF
  
  create_lib libfile.a file1.o file2.o
  gcc main.o libfile.a -omain
  (echo -n "ar file1.o file2.o: "; ./main; echo $?)
  
  create_lib libfile.a file2.o file1.o
  gcc main.o libfile.a -omain
  (echo -n "ar file2.o file1.o: "; ./main; echo $?)
  
  echo
  
  cat &lt;&lt;EOF
  2. Passing duplicate libraries to linker in different order
     produces no error, but different result:
  EOF
  
  gcc main.o libfile2.a libfile1.a -omain
  (echo -n "gcc libfile2.a libfile1.a: "; ./main; echo $?)
  
  gcc main.o libfile1.a libfile2.a -omain
  (echo -n "gcc libfile1.a libfile2.a: "; ./main; echo $?)
  </code></pre>
  
  <p>Sample output:</p>
  
  <pre><code>Using temporary directory: /tmp/tmp.AaXzxGcSdd
  
  1. Passing duplicate functions to archiver in different order
     produces no error, but different result:
  ar file1.o file2.o: 1
  ar file2.o file1.o: 2
  
  2. Passing duplicate libraries to linker in different order
     produces no error, but different result:
  gcc libfile2.a libfile1.a: 2
  gcc libfile1.a libfile2.a: 1
  </code></pre>