簡體   English   中英

如何使用 cmake 在 vs 代碼中包含、構建和調試共享庫 (.so)?

[英]How to include, build and debug shared libraries (.so) in vs code using cmake?

我正在嘗試在vs代碼中調試catkin package節點但遇到了問題。 由於某種原因,在調試中,模式程序在需要從共享庫創建 object 時會中止。 當程序無法在調用堆棧上創建 object 時,有我的共享庫(.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 會在右下角彈出 window ,上面寫着:

無法打開“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 沒有問題,並且 that.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

我得到這個 output:

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) 所以它似乎認識到需要哪些庫,但看起來不知道在哪里可以找到它們

嘗試在 vs-code 的 github 錯誤報告https://github.com/microsoft/vscode-cpptools/issues/811中提到的修復

當我嘗試將 cv_bridge 與不兼容的 OpenCV 版本一起使用時遇到了同樣的錯誤(我使用源安裝方法安裝了更高版本的 OpenCV),請確保您鏈接的是與您使用的 ROS 版本捆綁在一起的 OpenCV。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM