Ubuntu 下 VSCode C++&&OpenCV环境配置文件

折腾n小时后才搞通的配置文件

很无奈,按照网上很多教程走,不是这样的code1就是那样的code2或者是does not exist!都弄到我怀疑人生啦,网上解决报错的教程少之甚少!后来根据一些教程的解释,自己摸索出来的。记录此以便日后自己再查阅!

#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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

#tasks.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}

#c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "/usr/local/include/opencv4/opencv2"
                    
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}",
                "/usr/local/include/opencv4/opencv2" 
                
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "vector-of-bool.cmake-tools",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

#CMakeLists.txt

# cmake needs this line
cmake_minimum_required(VERSION 3.0.0)

# define project name 
project(your_project_name VERSION 0.1.0)       

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})

# Declare the executable target built from your sources
add_executable(target_file main.cpp)

# Link your application with OpenCV libraries
target_link_libraries( target_file ${OpenCV_LIBS})

有错误请指正!

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Ubuntu中使用VScode配置C++OpenCV环境的步骤如下: 1. 首先,安装OpenCV。可以参考引用中提供的链接,按照最新最详细的教程进行安装。 2. 安装VScode。在Ubuntu软件中心或者通过终端使用以下命令安装: ``` sudo snap install --classic code ``` 3. 打开VScode,并安装C++插件。点击左侧菜单栏的扩展图标,搜索并安装"C++"插件。 4. 配置编译器路径和标准。创建一个名为`.vscode`的文件夹,并在其中创建一个名为`c_cpp_properties.json`的文件。将以下内容复制到文件中,并保存: ``` { "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include/x86_64-linux-gnu/sys", "/usr/local/include/opencv4" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "gnu17", "cppStandard": "gnu17", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 } ``` 5. 配置构建任务。创建一个名为`tasks.json`的文件,并将以下内容复制到文件中,并保存: ``` { "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.out", "`pkg-config","--libs","--cflags","opencv4`" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ], "version": "2.0.0" } ``` 6. 现在,你可以在VScode中编写和运行C++代码,并使用OpenCV库。按下`Ctrl + Shift + B`编译并运行代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值