[英]Visual Studio Code - configure OpenCV libraries for C++
I've installed OpenCV On Ubuntu successfully and I managed to run a sample code as:我已经成功地在 Ubuntu 上安装了 OpenCV,并且成功地运行了一个示例代码:
g++ main.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv`
I've tried to run it with Visual Studio Code, I've installed the extension of C/C++ and code runner and ran it with the following configuration:我尝试使用 Visual Studio Code 运行它,我已经安装了 C/C++ 和代码运行器的扩展,并使用以下配置运行它:
tasks.json:任务.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++11","`pkg-config","--cflags","--libs opencv`"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json:启动.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": ["-std=c++11","`pkg-config","--cflags","--libs opencv`"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"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"
}
]
}
I got the following error:我收到以下错误:
[Running] cd "/home/kfir/code/opencv_test/" && g++ main.cpp -o main && "/home/kfir/code/opencv_test/"main
main.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
#include <opencv2/core.hpp>
^~~~~~~~~~~~~~~~~~
compilation terminated.
[Done] exited with code=1 in 0.032 seconds
Note: I'm using VSCode on mac and connect Ubuntu remote machine by ssh, the terminal works fine with the command of g++ above注意:我在 mac 上使用 VSCode 并通过 ssh 连接 Ubuntu 远程机器,终端可以正常使用上面的 g++ 命令
Be sure to add the location to openCV header files in your includePath.请务必将位置添加到 includePath 中的 openCV header 文件。
c_cpp_properties.json file: c_cpp_properties.json文件:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${default}",
"~/opencv4.5-custom/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Mine says "~/opencv4.5-custom/include/opencv4" but it could be "/usr/include/opencv4" or something else, depending on how and where you installed openCV.我的说“~/opencv4.5-custom/include/opencv4”,但它可能是“/usr/include/opencv4”或其他东西,这取决于你安装 openCV 的方式和位置。
You also need to modify task.json to add the arguments to the compiler as given by the pkg-config --cflags --libs opencv4
command, if you would run it in the terminal.您还需要修改task.json以将 arguments 添加到
pkg-config --cflags --libs opencv4
命令给出的编译器中,如果您要在终端中运行它的话。 You'll need to give the path to the shared object files.您需要提供共享 object 文件的路径。
Here's the content of my task.json file:这是我的task.json文件的内容:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I", "~/opencv4.5-custom/include/opencv4",
"-L", "~/opencv4.5-custom/lib",
"-l", "opencv_core",
"-l", "opencv_videoio",
"-l", "opencv_imgproc",
"-l", "opencv_highgui"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
You'll also need the change the paths depending to you setup.您还需要根据您的设置更改路径。
Here I have only included the modules that are used by my program (with the lines "-l", "opencv_core"
and so on...) so add or remove modules according to your needs.在这里,我只包括了我的程序使用的模块(带有
"-l", "opencv_core"
等行......)所以根据您的需要添加或删除模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.