Ubuntu中使用vscode+cmake引用第三方库进行编译调试

在自己的项目中引用openCV和Eigen库。

opencv和Eigen库的编译安装参见上一篇文章https://blog.csdn.net/qq_41816368/article/details/130090823?spm=1001.2014.3001.5502#t4

其他的第三方库放在工作空间的thirdparty文件夹,项目编译时一起编译,不提前安装。

首先新建一个文件夹作为工作空间

mkdir test

在创建Thirdparty文件夹,把第三方库放进来。

进入工作空间文件夹,在vscode中打开

cd test
code .

创建一个c++文件

#include <opencv2/opencv.hpp>
#include "Thirdparty/Sophus/sophus/geometry.hpp"

int main()
{
    // image.pngをimgに代入
    cv::Mat img = cv::imread("./street.jpeg");

    // imgの表示
    cv::imshow("img", img);

    // キーが押されるまで待機
    cv::waitKey(0);

    return 0;
}

因为要使用cmake编译,所以要创建CMakeLists.txt,加入如下代码

cmake_minimum_required(VERSION 2.6)
 
project(vscode_cmake)

find_package(OpenCV REQUIRED)
find_package(Eigen3 3.1.0 REQUIRED)

include_directories(
    ${PROJECT_SOURCE_DIR}
    ${PROJECT_SOURCE_DIR}/ThirdParty/Sophus
    ${EIGEN3_INCLUDE_DIR}
)

add_executable (vscode_cmake ./main.cc)

target_link_libraries(${PROJECT_NAME} 
${OpenCV_LIBS}
${EIGEN3_LIBS}
)

这里需要把opencv和eigen库放入find_package  target_link_libraries,把要用到的未提前安装的第三方库加入include_directories。

再创建build.sh文件

vim build.sh

写入以下内容

cd Thirdparty/DBoW2
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j

cd ../../g2o

echo "Configuring and building Thirdparty/g2o ..."

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j

cd ../../Sophus

echo "Configuring and building Thirdparty/Sophus ..."

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j

cd ../../../

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
make

注意给build.sh文件赋予权限,否则可能无法运行

chmod +x build.sh && ./build.sh

此时,工作空间文件夹下包含三个文件,main.cc  CMakeLists.txt  build.sh

在vscode中按ctrl+shift+p,选择Configure Tasks回车,再选择Create tasks,json file from template回车,再选择others,写入如下内容

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "vscode_cmake",
            "type": "shell",
            "command": "./build.sh",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

再添加c_cpp_properties.json在.vscode文件夹中。写入如下内容

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/opencv4",
                "/usr/include/eigen3"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

includePath中要写入openCV和Eigen的路径。

ctrl+shift+p,Run Build Tasks,即完成了编译,tasks.json文件就是运行了command里写的build.sh。而build.sh文件就是使用cmake进行了编译,这里注意编译的type要写debug,不要写release,否则后面打不了断点。

ctrl+shift+d, create a launch.json file,debugger选择GDB,删除默认生成的"configurations",点击右下角Add Configuration,修改“program”为cmake编译生成的executable文件的路径,注意要写绝对路径

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/username/test/build/vscode_cmake",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

在main.cc文件夹中打上断点,就可以调试了。

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值