title: Error trying to compile a C++ program that uses a shared library
body: <p>I'm trying to compile an example program that uses a shared library (also developed by me) in C++, which name is <code>libtestlib.so</code>. </p>
  
  <h1>The error</h1>
  
  <p>I have compiled the shared library without problems, but when I try to compile an executable that uses this library, I have the following error: <code>iface/libtestlib.so: undefined reference to 'ALIB::function()'</code></p>
  
  <h1>What I have done</h1>
  
  <p>I have done the following:</p>
  
  <p>C++ library (files in <code>$project_dir/lib1</code>):</p>
  
  <pre><code>// HEADER
  #ifndef ALIB_H
  #define ALIB_H
  
  namespace ALIB{
      int function();
  }
  
  #endif
  -------------------------------------------
  // SOURCE
  #include "alib.h"
  
  using namespace ALIB;
  
  int ALIB::function(){
      return 101;
  }
  </code></pre>
  
  <p>The C interface for the C++ library (files in <code>$project_dir/iface</code>)</p>
  
  <pre><code>// HEADER
  #ifndef IFACE_H
  #define IFACE_H
  
  #include "alib.h"
  
  extern "C"{
      int IFACE_function();
  }
  
  #endif
  --------------------------------
  // SOURCE
  #include "iface.h"
  
  int IFACE_function(){
      return ALIB::function();
  }
  --------------------------------------------------------
  // CMakeLists used to build the library:
  cmake_minimum_required(VERSION 2.8)
  PROJECT( testlib )
  include_directories( ../lib1 )
  add_library( testlib SHARED iface.cpp )
  </code></pre>
  
  <p>The executable that uses the library (files in <code>$project_dir/main</code>):</p>
  
  <pre><code>// SOURCE
  #include "iface.h"
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main(){
      cout &lt;&lt; IFACE_function() &lt;&lt; endl;
  }
  
  -------------------------------------
  
  // CMakeLists used to build the executable (file in `$project_dir`):
  cmake_minimum_required(VERSION 2.8)
  PROJECT( testlib )
  find_library( LIB NAMES testlib PATHS ./iface )
  include_directories( ./lib1 ./iface )
  add_executable( testlib ./main/main.cpp )
  target_link_libraries( testlib ${LIB} )
  </code></pre>
  
  <h1>Generated files</h1>
  
  <p><code>$project_dir</code>:
  all generated by cmake. (CmakeLists.txt, CMakeCache, cmake_install.cmake, MakeFile)</p>
  
  <p><code>$project_dir/lib1</code>:
  <code>alib.cpp</code> and <code>alib.h</code></p>
  
  <p><code>$project_dir/iface</code>:
  <code>iface.cpp, iface.h, libtestlib.so</code> and all generated by cmake.</p>
  
  <p><code>$project_dir/main</code>:
  <code>main.cpp</code>.</p>
  
  <p>As tree:</p>
  
  <pre><code>$project_dir
  ├── CMakeCache.txt
  ├── CMakeFiles
  │   ├── ...
  ├── cmake_install.cmake
  ├── CMakeLists.txt
  ├── CMakeLists.txt~
  ├── iface
  │   ├── CMakeCache.txt
  │   ├── CMakeFiles
  │   │   ├── ...
  │   ├── cmake_install.cmake
  │   ├── CMakeLists.txt
  │   ├── CMakeLists.txt~
  │   ├── iface.cpp
  │   ├── iface.h
  │   ├── libtestlib.so
  │   └── Makefile
  ├── lib1
  │   ├── alib.cpp
  │   └── alib.h
  ├── main
  │   └── main.cpp
  └── Makefile
  </code></pre>