ros中为C++程序导入opencv库

用vscode运行c++程序:

只需配置三个.json
这部分详情可以参考Ubuntu20下 vscode配置OpenCV 4.5.5

  1. c_cpp_properties.json

    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}/**",
                
                    "/usr/local/include/opencv4"
                ],
                "defines": [],
                "compilerPath": "/usr/bin/gcc",
                "cStandard": "gnu11",
                "cppStandard": "gnu++14",
                "intelliSenseMode": "linux-gcc-x64"
            }
        ],
        "version": 4
    }
    
  2. launch.json

    {
        // 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": "g++ - Build and debug active file",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}",  //程序文件路径
                "args": [],  //程序运行需传入的参数
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": true,   //运行时是否显示控制台窗口
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "C/C++: g++ build active file",
                "miDebuggerPath": "/usr/bin/gdb"
            }
        ]
    }
    
  3. settings.json

    {
        "files.associations": 
        {
            "cctype": "cpp",
            "clocale": "cpp",
            "cmath": "cpp",
            "cstdarg": "cpp",
            "cstddef": "cpp",
            "cstdio": "cpp",
            "cstdlib": "cpp",
            "cstring": "cpp",
            "ctime": "cpp",
            "cwchar": "cpp",
            "cwctype": "cpp",
            "array": "cpp",
            "atomic": "cpp",
            "strstream": "cpp",
            "*.tcc": "cpp",
            "bitset": "cpp",
            "chrono": "cpp",
            "complex": "cpp",
            "condition_variable": "cpp",
            "cstdint": "cpp",
            "deque": "cpp",
            "list": "cpp",
            "unordered_map": "cpp",
            "vector": "cpp",
            "exception": "cpp",
            "algorithm": "cpp",
            "functional": "cpp",
            "iterator": "cpp",
            "map": "cpp",
            "memory": "cpp",
            "memory_resource": "cpp",
            "numeric": "cpp",
            "optional": "cpp",
            "random": "cpp",
            "ratio": "cpp",
            "set": "cpp",
            "string": "cpp",
            "string_view": "cpp",
            "system_error": "cpp",
            "tuple": "cpp",
            "type_traits": "cpp",
            "utility": "cpp",
            "fstream": "cpp",
            "initializer_list": "cpp",
            "iomanip": "cpp",
            "iosfwd": "cpp",
            "iostream": "cpp",
            "istream": "cpp",
            "limits": "cpp",
            "mutex": "cpp",
            "new": "cpp",
            "ostream": "cpp",
            "sstream": "cpp",
            "stdexcept": "cpp",
            "streambuf": "cpp",
            "thread": "cpp",
            "cfenv": "cpp",
            "cinttypes": "cpp",
            "typeindex": "cpp",
            "typeinfo": "cpp",
            "variant": "cpp",
            "bit": "cpp"
        }
    }
    
  4. tasks.json

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++ build active file",  /* 与launch.json文件里的preLaunchTask的内容保持一致 */
                "command": "/usr/bin/g++",
                "args": [
                    "-std=c++11",
                    "-g",
                    //"${file}",   /* 编译单个文件 */
                    "${fileDirname}/*.cpp",  /* 编译多个文件 */
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",  /* 输出文件路径 */
    
                    /* 项目所需的头文件路径 */
                    "-I","${workspaceFolder}/",
                    "-I","/usr/local/include/",
                    "-I","/usr/local/include/opencv4/",
                    "-I","/usr/local/include/opencv4/opencv2",
    
                    /* 项目所需的库文件路径 */
                    "-L", "/usr/local/lib",
    
                    /* OpenCV的lib库 */
                    "/usr/local/lib/libopencv_*",
    
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "Task generated by Debugger."
            }
        ],
        "version": "2.0.0"
    }
    

2.在ros中配置opencv

只需在工作包的CMakeLists.txt文件中加入以下配置:

  1. 通过locate OpenCVConfig.cmake找到Opencv库路径
  2. 在cmakelist.txt里手动添加set(OpenCV_DIR 路径)
  3. 在find_package中加入OpenCV
  4. 在include_directories中加入{OPENCV_INCLUDE_DIRS}
  5. 为可执行文件设置链接target_link_libraries(文件名 ${OpenCV_LIBS})
    (后两项不必要)
    图片
    图片
    图片
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值