Configuration: OpenCV on Ubuntu, VSCode with code-runner

本文详细介绍了如何在Ubuntu上安装OpenCV,包括使用apt命令和从源代码编译,以及如何在VSCode中配置C++环境,包括C++17支持、opencv路径添加和CodeRunner的执行设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. OpenCV installation:

https://opencv.org/get-started/

sudo apt install libopencv-dev

(or install from source:)

cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules -DCMAKE_BUILD_TYPE=Release -DENABLE_CXX11=ON -DWITH_GTK=ON -DWITH_GTK_2_X=ON -DOPENCV_ENABLE_NONFREE=ON -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=OFF ../opencv-4.x

make -j4

sudo make install

sudo ldconfig

2. VSCode Configuration:

  1. ctrl+shift+p   -->   INPUT "c/c++: edit configurations (JSON)"  -->  Click -->   c_cpp_properties.json is generated (in .vscode directory).
  2. ctrl+shift+p   -->   INPUT "tasks: configure default build task"  -->  Click  -->  Click "C/C++: g++-7 build active file"(g++, gcc also)  -->  tasks.json is generated (in .vscode directory).
  3. In tasks.json, add the last two lines below: 
    			"args": [
    // 				"-std=c++17",
                    "-fdiagnostics-color=always",
                    "-g", "${file}",
                    "-o", "${fileDirname}/${fileBasenameNoExtension}",
                    "-I", "${workspaceFolder}/",
                    "-I", "/usr/local/include/opencv4/",
                    "-L", "/usr/local/lib/libopencv_*",
    //				"-lstdc++fs"
    			],
  4. In c_cpp_properties.json, add the last one line below:
                "includePath": [
                    "${workspaceFolder}/**",
                    "/usr/local/include/opencv4"
                ],

3. Code Runner Configuration:

  1.  Extensions: install code runner
  2. Settings  -->  INPUT: "code runner: executor map"  -->  Edit in settings.json
  3. Modify this line:
        "code-runner.executorMap": {
            "cpp": "cd $dir && g++-7 -g $fileName -o $fileNameWithoutExt -I /usr/local/include/opencv4/ -L /usr/local/lib/libopencv_* && $dir$fileNameWithoutExt",

    Insert "-I /usr/local/include/opencv4/ -L /usr/local/lib/libopencv_*" before "&&" (the insertion position is significant);

If cppStandard is c++17, insert "-std=c++17" "-lstdc++fs" as above in 2.3

Check

 Now we can Run Code, or Debug/Run C/C++ File successfully.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值