title: Undefined reference error despite of having the library specified in CMake (issues linking with libtorch (C++11 ABI?)
body: <p>I'm trying to create a test executable out of a library I made. Let's name them lib1 and lib2. lib1 gets built along with its test just fine. the lib2 is also built without any issues. However, whenever I try to link lib2 with its test executable (i.e. a sample program that uses lib2) I get the following error:</p>
  <pre><code>usr/bin/ld: CMakeFiles/Lib2_Test.dir/Lib2_Test.cpp.o: in function `main':
  Lib2_Test.cpp:(.text+0xf3): undefined reference to `Lib2::Lib2(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, int, int)'
  /usr/bin/ld: Lib2_Test.cpp:(.text+0x3f5): undefined reference to `Lib2::Evaluate(bool&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;, float&amp;, cv::Mat&amp;, cv::Mat&amp;, bool)'
  collect2: error: ld returned 1 exit status
  make[2]: *** [CMakeFiles/Lib2_Test.dir/build.make:130: Lib2_Test] Error 1
  make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/Lib2_Test.dir/all] Error 2
  make: *** [Makefile:130: all] Error 2
  </code></pre>
  <p>I tried viewing the headers using <code>readelf -d</code> and using the <code>ldd</code> command, both of  the libs seems to be having all the necessary references. However lib1 doesn't have any issues, while lib2 generates unreferenced related errors when being linked to an executable that uses it.</p>
  <p>Below are the cmakeLists I made for both of them and later on I also included the output of <code>readelf</code>.</p>
  <p>CMakelist.txt for <code>lib1</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 3.11)
  project(Lib1)
  
  set(CMAKE_CXX_STANDARD 17)
  
  find_package(Torch REQUIRED)
  find_package(OpenCV REQUIRED)
  
  add_definitions(-D_LIB1_BUILD_DLL) 
  
  set( CMAKE_CXX_FLAGS  &quot;${CMAKE_CXX_FLAGS} -fPIC&quot; )
  
  set(CMAKE_INSTALL_PREFIX /home/me/Desktop/LibtorchPort/built_stuff)
  
  include_directories( /home/me/Desktop/LibtorchPort/Dependencies/include ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
  LINK_DIRECTORIES(/home/me/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib)
  
  # http://dlib.net/examples/CMakeLists.txt.html
  add_subdirectory(/home/me/dlib-19.21 /home/me/dlib-19.21/build)     
  
  set(Lib1_SRC ./lib1.cpp)
  
  add_library(lib1 SHARED  ${Lib1_SRC})
  
  # Link
  target_link_libraries(lib1 ${TORCH_LIBRARIES})
  target_link_libraries(lib1 ${OpenCV_LIBS})
  target_link_libraries(lib1 dlib::dlib)
  
  install(TARGETS lib1 LIBRARY DESTINATION lib)
  </code></pre>
  <p>This is the CMakeList.txt for <code>lib1_test</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 3.11)
  project(Lib1_Test)
  set(CMAKE_CXX_STANDARD 17)
  
  find_package(Torch REQUIRED)
  find_package(OpenCV REQUIRED)
  
  set(CMAKE_INSTALL_PREFIX /home/me/Desktop/LibtorchPort/built_stuff)
  include_directories( /home/me/Desktop/LibtorchPort/Dependencies/include ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
   
  # Link
  add_executable(lib1_dynamic_test ./Lib1_Test.cpp)
  target_link_directories(lib1_dynamic_test PRIVATE /home/me/Desktop/LibtorchPort/Lib1/build)
  
  target_link_libraries(lib1_dynamic_test lib1 )
  target_link_libraries(lib1_dynamic_test ${TORCH_LIBRARIES} )
  target_link_libraries(lib1_dynamic_test ${OpenCV_LIBS})
  install(TARGETS lib1_dynamic_test DESTINATION bin)
  </code></pre>
  <p>This is the CMakeList.txt for <code>lib2</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 3.11)
  project(Lib2)
  
  set(CMAKE_CXX_STANDARD 17)
  
  find_package(Torch REQUIRED)
  find_package(OpenCV REQUIRED)
  
  add_definitions(-D_LIB2_BUILD_DLL) 
  
  set(CMAKE_INSTALL_PREFIX /home/me/Desktop/LibtorchPort/built_stuff)
  
  include_directories( /home/me/Desktop/LibtorchPort/Dependencies/include ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
  
  LINK_DIRECTORIES(/home/me/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib)
  LINK_DIRECTORIES(/home/me/Desktop/LibtorchPort/Lib1/build)
  
  set(LIB2_SRC  ./lib2.cpp )
  
  add_library(lib2_dynamic SHARED ${LIB2_SRC} )
  
  target_link_directories(lib2_dynamic PRIVATE /home/me/Desktop/LibtorchPort/Lib1/build)
  
  target_link_libraries(lib2_dynamic  lib1)
  target_link_libraries(lib2_dynamic  ${TORCH_LIBRARIES})
  target_link_libraries(lib2_dynamic  ${OpenCV_LIBS})
  
  install(TARGETS lib2_dynamic LIBRARY DESTINATION lib)
  </code></pre>
  <p>And finally here is the CMakeList for <code>lib2_test</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 3.11)
  project(Lib2_Test)
  
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_INSTALL_PREFIX /home/me/Desktop/LibtorchPort/built_stuff)
  
  #find_package(Torch REQUIRED)
  find_package(OpenCV REQUIRED)
  
  include_directories( /home/me/Desktop/LibtorchPort/Dependencies/include ${OpenCV_INCLUDE_DIRS})
  LINK_DIRECTORIES(/home/me/Desktop/LibtorchPort/Lib1/build)
  
  add_executable(Lib2_Test ./lib2_test.cpp)
  
  target_link_directories(Lib2_Test PRIVATE /home/me/Desktop/LibtorchPort/Lib2/build)
  #target_link_directories(Lib2_Test PUBLIC /home/me/Desktop/LibtorchPort/Lib1/build)
  
  #Link
  target_link_libraries(Lib2_Test ${OpenCV_LIBS})
  target_link_libraries(Lib2_Test lib2_dynamic)
  install(TARGETS Lib2_Test DESTINATION bin)
  </code></pre>
  <p>running the <code>readelf -d lib1</code> this is the output I get:</p>
  <pre><code>Dynamic section at offset 0x908f8 contains 38 entries:
    Tag        Type                         Name/Value
   0x0000000000000001 (NEEDED)             Shared library: [libtorch.so]
   0x0000000000000001 (NEEDED)             Shared library: [libc10.so]
   0x0000000000000001 (NEEDED)             Shared library: [libtorch_cpu.so]
   0x0000000000000001 (NEEDED)             Shared library: [libopencv_highgui.so.3.4]
   0x0000000000000001 (NEEDED)             Shared library: [libopencv_imgproc.so.3.4]
   0x0000000000000001 (NEEDED)             Shared library: [libopencv_core.so.3.4]
   0x0000000000000001 (NEEDED)             Shared library: [libcblas.so.3]
   0x0000000000000001 (NEEDED)             Shared library: [liblapack.so.3]
   0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
   0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
   0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
   0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
   0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
   0x000000000000000e (SONAME)             Library soname: [libLib1.so]
   0x000000000000001d (RUNPATH)            Library runpath: [/home/me/anaconda3/lib/python3.8/site-packages/torch/lib:/home/me/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib:/usr/local/lib:]
   0x000000000000000c (INIT)               0x1a000
   0x000000000000000d (FINI)               0x57e00
   0x0000000000000019 (INIT_ARRAY)         0x90b28
   0x000000000000001b (INIT_ARRAYSZ)       32 (bytes)
   0x000000000000001a (FINI_ARRAY)         0x90b48
   0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
   0x000000006ffffef5 (GNU_HASH)           0x328
   0x0000000000000005 (STRTAB)             0x6840
   0x0000000000000006 (SYMTAB)             0x1758
   0x000000000000000a (STRSZ)              56053 (bytes)
   0x000000000000000b (SYMENT)             24 (bytes)
   0x0000000000000003 (PLTGOT)             0x92000
   0x0000000000000002 (PLTRELSZ)           8112 (bytes)
   0x0000000000000014 (PLTREL)             RELA
   0x0000000000000017 (JMPREL)             0x17ff0
   0x0000000000000007 (RELA)               0x14b58
   0x0000000000000008 (RELASZ)             13464 (bytes)
   0x0000000000000009 (RELAENT)            24 (bytes)
   0x000000006ffffffe (VERNEED)            0x149f8
   0x000000006fffffff (VERNEEDNUM)         5
   0x000000006ffffff0 (VERSYM)             0x14336
   0x000000006ffffff9 (RELACOUNT)          6
   0x0000000000000000 (NULL)               0x0
  </code></pre>
  <p>and this is the output for <code>lib2</code>:</p>
  <pre><code>Dynamic section at offset 0x37ba0 contains 32 entries:
    Tag        Type                         Name/Value
   0x0000000000000001 (NEEDED)             Shared library: [libLib1.so]
   0x0000000000000001 (NEEDED)             Shared library: [libtorch.so]
   0x0000000000000001 (NEEDED)             Shared library: [libtorch_cpu.so]
   0x0000000000000001 (NEEDED)             Shared library: [libopencv_core.so.3.4]
   0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
   0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
   0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
   0x000000000000000e (SONAME)             Library soname: [libLib2_dynamic.so]
   0x000000000000001d (RUNPATH)            Library runpath: [/home/me/anaconda3/lib/python3.8/site-packages/torch/lib:/home/me/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib:/home/me/Desktop/LibtorchPort/Lib1/build:/usr/local/lib:]
   0x000000000000000c (INIT)               0x1e000
   0x000000000000000d (FINI)               0x2ec10
   0x0000000000000019 (INIT_ARRAY)         0x38108
   0x000000000000001b (INIT_ARRAYSZ)       16 (bytes)
   0x000000000000001a (FINI_ARRAY)         0x38118
   0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
   0x000000006ffffef5 (GNU_HASH)           0x2f0
   0x0000000000000005 (STRTAB)             0x7d88
   0x0000000000000006 (SYMTAB)             0x1dd0
   0x000000000000000a (STRSZ)              62708 (bytes)
   0x000000000000000b (SYMENT)             24 (bytes)
   0x0000000000000003 (PLTGOT)             0x39000
   0x0000000000000002 (PLTRELSZ)           14784 (bytes)
   0x0000000000000014 (PLTREL)             RELA
   0x0000000000000017 (JMPREL)             0x19e90
   0x0000000000000007 (RELA)               0x17b38
   0x0000000000000008 (RELASZ)             9048 (bytes)
   0x0000000000000009 (RELAENT)            24 (bytes)
   0x000000006ffffffe (VERNEED)            0x17a78
   0x000000006fffffff (VERNEEDNUM)         3
   0x000000006ffffff0 (VERSYM)             0x1727c
   0x000000006ffffff9 (RELACOUNT)          4
   0x0000000000000000 (NULL)               0x0
  </code></pre>
  <p>And yet the <code>lib1</code> builds and links just fine, while <code>lib2</code> that is dependant on <code>lib1</code>, has issues when being linked to its test or any other libs. I'm clueless at this point and have no idea what could have caused this. What am I missing?</p>
  <h2>Update 1</h2>
  <p>This is the <code>lib2_test.cpp</code> : <a href="https://paste.ee/p/pOgFk" rel="nofollow noreferrer">https://paste.ee/p/pOgFk</a>
  and this is how the header file looks like :</p>
  <pre class="lang-cpp prettyprint-override"><code>#ifndef Lib2_H
  #define Lib2_H
  
  /* If we are we on Windows, we want a single define for it.*/
  #if !defined(_WIN32) &amp;&amp; (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
  #define _WIN32
  #endif /* _WIN32 */
  
  #if defined(_WIN32) &amp;&amp; defined(_Lib2_BUILD_DLL)
  /* We are building Lib2 as a Win32 DLL */
  #define LIB2_API __declspec(dllexport)
  #elif defined(_WIN32) &amp;&amp; defined(Lib2_DLL)
  /* We are calling Lib2 as a Win32 DLL */
  #define LIB2_API __declspec(dllimport)
  #elif defined(__GNUC__) &amp;&amp; defined(_Lib2_BUILD_DLL)
  /* We are building Lib2 as a shared / dynamic library */
  #define LIB2_API __attribute__((visibility(&quot;default&quot;)))
  #else
  /* We are building or calling Lib2 as a static library */
  #define LIB2_API
  #endif
  
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;map&gt;
  #include &lt;memory&gt;
  #include &lt;opencv2/core.hpp&gt;
  
  
  enum class ValidationStatus
  {
      None = -1,
      UnderValidation = 0,
      Validated = 1,
      Invalidated = 2,
      AnomalyDetected = 3,
      ToomuchAnonalyDetected = 4
  };
  
  typedef std::tuple&lt;ValidationStatus, std::string, bool&gt; Lib2Result;
  
  class Lib2Impl;
  
  class LIB2_API Lib2
  {
  private:
      std::shared_ptr&lt;Lib2Impl&gt; Lib2;
  
  public:
      Lib2(std::string shape_predictor_path = &quot;&quot;, std::string eyeNet_path = &quot;&quot;, int valid_presence_delay = 5, int fpasPassed = 0);
  
      std::vector&lt;Lib2Result&gt; Run(std::map&lt;std::string, bool&gt;&amp; validity_result, 
                                         std::vector&lt;std::tuple&lt;std::string, float&gt;&gt;&amp; ids,
                                         std::vector&lt;cv::Mat&gt;&amp; faces, 
                                         cv::Mat&amp; originalImage,
                                         bool show_debug_info=false);
  
      Lib2Result Evaluate(bool&amp; status, std::string&amp; name, float&amp; theta, cv::Mat&amp; face_image, cv::Mat&amp; originalImage, bool debug_info = true);
      ~Lib2();
  
  };
  
  #endif // !Lib2_H
  </code></pre>
  <p>Concerning the comments, as you can see the calls on the <code>lib2_test.cpp</code> is OK and it uses the same signature.</p>
  <h2>Update 2</h2>
  <p>I need to add this as well that the project was built just fine in Windows using Visual Studio and also cmake! However, under
  Linux (Ubuntu 20.04), I'm facing these issues. So this is not just calling different/wrong methods or with wrong signatures. This code should compile and link just fine but I'm doing something wrong here which I'm not sure what it is.</p>
  <h2>Note 2</h2>
  <p>The lib1 and lib2 are just made up name for the actual filenames (I just chose lib1 and lib2 to keep things simpler and replaced the names here, so if you see difference in cases, don't mind it, the actual files are the same.</p>