【问题标题】:How to include, build and debug shared libraries (.so) in vs code using cmake?如何使用 cmake 在 vs 代码中包含、构建和调试共享库(.so)?
【发布时间】:2020-09-04 08:15:03
【问题描述】:

我正在尝试在vs代码中调试catkin包节点,但是遇到了问题。出于某种原因,在调试中,模式程序在需要从共享库创建对象时中止。 当程序无法在调用堆栈上创建对象时,有我的共享库(.so)但 vscode 说库有未知来源,我可以得出结论,cmake 或 vscode 不知道共享库的位置。共享库包含在 CMakeLists.txt 中,如下所示:

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)

我已经颠倒了谷歌搜索,但我找不到任何解决方案如何在我的 cmake 中包含对我有用的共享库。

我正在调试这个小程序:

// C++
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>

// OpenCV
#include <opencv2/core.hpp>
#include <opencv2/core/utils/filesystem.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/core/utility.hpp>

// PnP Tutorial
#include "Mesh.h"
#include "Model.h"
#include "PnPProblem.h"
#include "RobustMatcher.h"
#include "ModelRegistration.h"
#include "Utils.h"

//ROS-->openCV
//bridge between ROS and opencv; used for reading from camera topic in ROS and giving it to opencv
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>

/**  GLOBAL VARIABLES  **/

using namespace cv;
using namespace std;

/**  Functions headers  **/
void help();
void initKalmanFilter( KalmanFilter &KF, int nStates, int nMeasurements, int nInputs, double dt);
void predictKalmanFilter( KalmanFilter &KF, Mat &translation_predicted, Mat &rotation_predicted );
void updateKalmanFilter( KalmanFilter &KF, Mat &measurements,
                         Mat &translation_estimated, Mat &rotation_estimated );
void fillMeasurements( Mat &measurements,
                       const Mat &translation_measured, const Mat &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 << "Current directory is: " << getCurrentPath() << 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<<"Before model.load()"<<endl;
    model.load(yml_read_path); // load a 3D textured object model
    cout<<"After model.load()"<<endl;
    Mesh mesh;                 // instantiate Mesh object
    mesh.load(ply_read_path);  // load an object mesh

    return 0;
}

程序总是在同一个地方失败,在方法 model.load(yml_read_path) 中,vscode 会在右下角弹出窗口,上面写着:

无法打开“strlen-avx2.S”:无法读取文件 '/build/glibc-OTsEL5/glibc-2.27/sysdeps/x86_64/multiarch/strlen-avx2.S' (错误:无法解析不存在的文件 '/build/glibc-OTsEL5/glibc-2.27/sysdeps/x86_64/multiarch/strlen-avx2.S')。

但我认为 strlen 没有问题,而且 .so 库不包括在内。

忽略所有这些包含头,我只是从另一个程序中复制它们,但我没有在我的 test_luka.cpp 中使用它

任何帮助将不胜感激。

编辑:

我尝试了@Boris 的建议 (https://github.com/microsoft/vscode-cpptools/issues/811),但没有任何成功,共享库仍然未知,唯一的区别是异常后我的程序转到 strlen-avx2.S。

$ sudo apt install glibc-source 
$ cd /usr/src/glibc 
$ sudo tar xvf glibc-2.27.tar.xz 

然后在launch.json中使用sourceFileMap为文件夹建立链接:

"sourceFileMap": {
    "/build/glibc-OTsEL5": "/usr/src/glibc"
},

当我像这样在 exe 目录中运行 ldd 时:

  ldd OpenCV_registration | grep libopencv_core

我得到这个输出:

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) 所以它似乎可以识别出需要哪些库,但看起来不知道在哪里可以找到它们

【问题讨论】:

  • 不确定问题出在哪里,但find_package(catkin REQUIRED COMPONENTS OpenCV REQUIRED ... 看起来不正确。调用中应该只有一个 REQUIRED 参数。目前尚不清楚这可能会如何影响事情,但我认为这会产生错误或警告。
  • @squareskittles 感谢您的回答我已经更改了它,但它仍然无法识别共享库。看看我的编辑也许还有其他线索。

标签: c++ visual-studio-code cmake shared-libraries ubuntu-18.04


【解决方案1】:

尝试在 vs-code 的 github 错误报告 https://github.com/microsoft/vscode-cpptools/issues/811 中提到的修复方法

【讨论】:

    【解决方案2】:

    当我尝试将 cv_bridge 与不兼容版本的 OpenCV 一起使用时遇到了同样的错误(我使用源安装方法安装了更高版本的 OpenCV),请确保您链接的 OpenCV 与您使用的 ROS 版本捆绑在一起。

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      相关资源
      最近更新 更多