title: How to include, build and debug shared libraries (.so) in vs code using cmake?
body: <p>I am trying to debug catkin package node in vs code but I have encountered a problem. For some reason, in debug, mode program aborts when it needs to create object from a shared library.
  When the program fails to create an object on call stack there are my shared libraries (.so) but vscode says that libraries have unknown source from what I can conclude that cmake or vscode do not know where shared libraries are located. Shared libraries are included in CMakeLists.txt like this:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.0.2)
  
  ## Compile as C++11, supported in ROS Kinetic and newer
  add_compile_options(-std=c++14)
  
  project(1_OpenCV_detection)
  
  message(${PROJECT_SOURCE_DIR})
  # Aggregate the sources
  #file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/include/*.cpp")
  set(SOURCES "${PROJECT_SOURCE_DIR}/include/Model.cpp"
              "${PROJECT_SOURCE_DIR}/include/Mesh.cpp"
              "${PROJECT_SOURCE_DIR}/include/CsvReader.cpp"
              "${PROJECT_SOURCE_DIR}/include/CsvWriter.cpp"
              "${PROJECT_SOURCE_DIR}/include/ModelRegistration.cpp"
              "${PROJECT_SOURCE_DIR}/include/PnPProblem.cpp"
              "${PROJECT_SOURCE_DIR}/include/RobustMatcher.cpp"
              "${PROJECT_SOURCE_DIR}/include/Utils.cpp")
  message(${SOURCES})
  #set(OpenCV_DIR "/usr/share/OpenCV/")
  set(OpenCV_DIR "/usr/local/lib/cmake/opencv4/")
  #message(${OpenCV_DIR})
  
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  ## Specify additional locations of header files
  ## Your package locations should be listed before other locations
  include_directories(
    include
    ${opencv_INCLUDE_DIRS}
    ${catkin_INCLUDE_DIRS}
    ${OpenCV_DIR}
    /usr/local/include/opencv4/opencv2/core/utils/
    /usr/local/include/opencv4/opencv2/core/
    /usr/local/include/opencv4/opencv2/
    /opt/ros/melodic/include/
    /usr/include/boost/
    #/usr/include/opencv/opencv2/
    #/usr/share/OpenCV/
    #linux
    /usr/include/c++/7/
    /usr/include/x86_64-linux-gnu/c++/7/
    /usr/include/c++/7/backward/
    /usr/lib/gcc/x86_64-linux-gnu/7/include/
    #/usr/local/include/
    /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed/
    /usr/include/x86_64-linux-gnu/
    /usr/include/
    /usr/local/lib/
  )
  add_library(opencv_so SHARED IMPORTED GLOBAL)
  set_target_properties(
    opencv_so 
    PROPERTIES 
      IMPORTED_LOCATION /usr/local/lib/libopencv_core.so.4.3
      IMPORTED_IMPLIB   /usr/local/lib/libopencv_core.so.4.3
  ) 
  ## Find catkin macros and libraries
  ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
  ## is used, also find other catkin packages
  find_package(catkin REQUIRED COMPONENTS
  OpenCV REQUIRED
  cv_bridge
  image_transport
  roscpp
  sensor_msgs
  std_msgs
  )
  catkin_package(
  #  INCLUDE_DIRS include
  #  LIBRARIES 1_OpenCV_detection
    CATKIN_DEPENDS cv_bridge image_transport roscpp sensor_msgs std_msgs
  #  DEPENDS system_lib
  )
  add_executable(OpenCV_registration src/OpenCV_registration.cpp ${SOURCES} ${OpenCV_DIR})
  target_link_libraries(OpenCV_registration ${catkin_LIBRARIES} ${OpenCV_LIBS}  opencv_so)
  
  add_executable(test_luka src/test.cpp ${SOURCES} ${OpenCV_DIR})
  target_link_libraries(test_luka ${catkin_LIBRARIES} ${OpenCV_LIBS}  opencv_so)
  </code></pre>
  
  <p>I have searched google upside down but I couldn't find any solution how to include shared libraries in my cmake that works for me.</p>
  
  <p>I am trying to debug this small program:</p>
  
  <pre><code>// C++
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fstream&gt;
  
  // OpenCV
  #include &lt;opencv2/core.hpp&gt;
  #include &lt;opencv2/core/utils/filesystem.hpp&gt;
  #include &lt;opencv2/highgui.hpp&gt;
  #include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/calib3d.hpp&gt;
  #include &lt;opencv2/video/tracking.hpp&gt;
  #include &lt;opencv2/core/utility.hpp&gt;
  
  // PnP Tutorial
  #include "Mesh.h"
  #include "Model.h"
  #include "PnPProblem.h"
  #include "RobustMatcher.h"
  #include "ModelRegistration.h"
  #include "Utils.h"
  
  //ROS--&gt;openCV
  //bridge between ROS and opencv; used for reading from camera topic in ROS and giving it to opencv
  #include &lt;ros/ros.h&gt;
  #include &lt;image_transport/image_transport.h&gt;
  #include &lt;cv_bridge/cv_bridge.h&gt;
  #include &lt;sensor_msgs/image_encodings.h&gt;
  
  /**  GLOBAL VARIABLES  **/
  
  using namespace cv;
  using namespace std;
  
  /**  Functions headers  **/
  void help();
  void initKalmanFilter( KalmanFilter &amp;KF, int nStates, int nMeasurements, int nInputs, double dt);
  void predictKalmanFilter( KalmanFilter &amp;KF, Mat &amp;translation_predicted, Mat &amp;rotation_predicted );
  void updateKalmanFilter( KalmanFilter &amp;KF, Mat &amp;measurements,
                           Mat &amp;translation_estimated, Mat &amp;rotation_estimated );
  void fillMeasurements( Mat &amp;measurements,
                         const Mat &amp;translation_measured, const Mat &amp;rotation_measured);
  string CurrentPath;
  string getCurrentPath()
  {
      size_t size;
          char *path=NULL;
      path=getcwd(path,size);
      CurrentPath=path;
      return path;
  }
  /**  Main program  **/
  int main(int argc, char *argv[])
  {
      //CommandLineParser parser(argc, argv, keys);
      cout &lt;&lt; "Current directory is: " &lt;&lt; getCurrentPath() &lt;&lt; endl;
  
      string video_read_path = samples::findFile("src/1_OpenCV_detection/Data/box.mp4");                             // recorded video
      string yml_read_path = samples::findFile("src/1_OpenCV_detection/Data/cube_30mm/cube_30mm.yml");               // 3dpts + descriptors
      string ply_read_path = samples::findFile("src/1_OpenCV_detection/Data/cube_30mm/meshes/cube_30mm.ply");        // object mesh
  
      Model model;                // instantiate Model object
      cout&lt;&lt;"Before model.load()"&lt;&lt;endl;
      model.load(yml_read_path); // load a 3D textured object model
      cout&lt;&lt;"After model.load()"&lt;&lt;endl;
      Mesh mesh;                 // instantiate Mesh object
      mesh.load(ply_read_path);  // load an object mesh
  
      return 0;
  }
  </code></pre>
  
  <p>Program always fails with on same place, in method model.load(yml_read_path) and vscode raises popup window in right bottom corner which says:</p>
  
  <blockquote>
    <p>Unable to open 'strlen-avx2.S': Unable to read file
    '/build/glibc-OTsEL5/glibc-2.27/sysdeps/x86_64/multiarch/strlen-avx2.S'
    (Error: Unable to resolve non-existing file
    '/build/glibc-OTsEL5/glibc-2.27/sysdeps/x86_64/multiarch/strlen-avx2.S').</p>
  </blockquote>
  
  <p>but my opinion is that there is no problem with strlen and that .so libraries are not included somehow.</p>
  
  <p>Ignore all these include headers, I just copied them from another program but I am not using it in my test_luka.cpp</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>I have tried @Boris suggestion (<a href="https://github.com/microsoft/vscode-cpptools/issues/811" rel="nofollow noreferrer">https://github.com/microsoft/vscode-cpptools/issues/811</a>) but without any success, shared libraries are still unknown only difference is that after exception I program goes to strlen-avx2.S.</p>
  
  <pre><code>$ sudo apt install glibc-source 
  $ cd /usr/src/glibc 
  $ sudo tar xvf glibc-2.27.tar.xz 
  </code></pre>
  
  <p>Then make link for folders using sourceFileMap in launch.json:</p>
  
  <pre><code>"sourceFileMap": {
      "/build/glibc-OTsEL5": "/usr/src/glibc"
  },
  </code></pre>
  
  <p>When I run ldd in exe directory like this:</p>
  
  <pre><code>  ldd OpenCV_registration | grep libopencv_core
  </code></pre>
  
  <p>I get this output:</p>
  
  <p>libopencv_core.so.3.2 => /usr/lib/x86_64-linux-gnu/libopencv_core.so.3.2 (0x00007f0f4d85e000)
  libopencv_core.so.4.3 => /usr/local/lib/libopencv_core.so.4.3 (0x00007f0f4bc62000)
  So it seems to recognizes which libraries are needed but looks like doesn't know where to find them</p>