如何在VS代码中使用cmake包含、构建和调试共享库(.so)?

问题描述 投票:0回答:1

我试图在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)

我在google上搜了个底朝天,但我找不到任何解决方案,如何在我的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': Unable to read file 'buildglibc-OTsEL5glibc-2.27sysdepsx86_64multiarchstrlen-avx2.S' (Error: 无法解析不存在的文件'buildglibc-OTsEL5glibc-2.27sysdepsx86_64multiarchstrlen-avx2.S')。)

但我的意见是,strlen没有问题,而且.so库没有被包含。

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

希望能得到任何帮助。

EDIT.我已经尝试了@Boris的建议(

我已经尝试了 @Boris 的建议 (https:/github.commicrosoftvscode-cpptoolsissues811。),但没有成功,共享库还是未知,唯一不同的是,我的程序在出现异常后进入了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 => usrlibx86_64-linux-gnulibopencv_core.so.3.2 (0x00007f0f4d85e000)libopencv_core.so.4.3 => usrlocalliblibopencv_core.so.4. .3 (0x00007f0f4bc62000)所以它似乎能识别出需要哪些库,但似乎不知道在哪里找到它们。

c++ visual-studio-code cmake shared-libraries ubuntu-18.04
1个回答
0
投票

试试vs-code的github bug报告中提到的修复方法吧 https:/github.commicrosoftvscode-cpptoolsissues811。

© www.soinside.com 2019 - 2024. All rights reserved.