说在前面:
因为是新手第一次用linux调用opencv运行c++程序,所以中间状况频发,因此本博文涉及的内容比较多,希望大家取其精华去其糟粕有选择的借鉴~
正式开始:
一、默认vscode已经下载完成,可参考:
二、配置(默认为安装opencv)
1、安装c/c++插件(这里我已经安装好了,未安装的童鞋应该显示install)
2、新建工程(main.cpp) 并输入程序(一开始先用了一个简单的hello world测试基础环境)
3、配置launch.json
点击左侧运行/调试按钮,选择添加配置(Add configuration),选择c++(GDB/LLDB)
选择default configuration
将自动生成一个launch.json文件,这里的json文件还没有更改,是默认的
4、配置tasks.json
点击ctrl+shift+P-> 输入Task->选择Task:configure default build task
本文选择C/C++:g++
默认json如下,这里的json文件还没有更改,是默认的
5、配置c_cpp_properties.json
点击ctrl+shift+P-> 输入Edit>选择c/c++:Edit Configurations(JSON)
生成默认json文件,这里的json文件还没有更改,是默认的
6、测试基础hello world功能,可以看到不管是debug调试还是直接run都可以正常运行说明我们的json基础配置没有问题
三、opencv测试
新建try.cpp,先用一个简单的imshow运行一下看看结果
接下来是我遇到的第一个拦路虎!
点击launch后提示错误,选择debug anyway后提示找不到main.out
检查launch.json和tasks.json文件,注意launch.json中的preLaunchTask要与tasks.json中的label相同,修改json文件如下
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": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole":true,
"MIMode": "gdb",
"preLaunchTask": "build",
"miDebuggPath":"/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
]
}
]
}
运行后仍然提示找不到main,可是我们在之前已经测试了基本的hello world程序,可以成功执行,证明我们的json配置文件没有问题。因此怀疑是因为opencv没有配置完整导致报错,再次启动opencv的imshow测试代码,注意到程序有其他报错信息:
查了一下资料说是因为opencv的静态编译库没有链接到本程序中,于是找到一条亲测有用的解决方案:
终端输入:
g++ demo.cpp -o demo `pkg-config opencv --cflags --libs`
注意那个` 不是单引号,是数字1旁边的`
编译成功后生成了demo可执行文件,终端输入
./demo 即可成功运行
此时我回到VScode中重新运行在环境中进行测试,发现还是不行!???
是的第二个拦路虎来了!
我不李姐但我大为震惊,为什么终端可以运行,到VScode里面就不行了
原因只有一个!opencv动态链接库没有链接到vscode,修改tasks.json如下
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"`pkg-config","--cflags","opencv","--libs`",
"-I","/usr/local/include/opencv",
"-I","/usr/local/include/opencv2",
"-L","/usr/local/lib",
"-I","opencv_core",
"-I","opencv_imgproc",
"-I","opencv_imgcodecs",
"-I","opencv_video",
"-I","opencv_ml",
"-I","opencv_highgui",
"-I","opecv_obdetect",
"-I","opencv_flann",
"-I","opencv_imgcodecs",
"-I","opencv_photo",
"-I","opencv_videoio"
],
"options": {
"cwd": "/usr/bin"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
这时我们 点击左侧debug/run按钮,点击绿色三角运行即可!
但是问题又来了!!!debug可以但是run起来又开始提示我“未定义的字符”
又来一个拦路虎!
继续找解决方案ing...
解决方法:这里主要是因为安装好opencv之后,没有配置环境变量,因此需要修改以下vcsode中的工作空间的settings.json文件
1、打开vscode-文件-首选项-设置-扩展- Run Code configuration-找到在serrings.json编辑
2、修改其内容如下:
{
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt `pkg-config opencv --libs --cflags opencv` && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
"perl": "perl",
"perl6": "perl6",
"ruby": "ruby",
"go": "go run",
"lua": "lua",
"groovy": "groovy",
"powershell": "powershell -ExecutionPolicy ByPass -File",
"bat": "cmd /c",
"shellscript": "bash",
"fsharp": "fsi",
"csharp": "scriptcs",
"vbscript": "cscript //Nologo",
"typescript": "ts-node",
"coffeescript": "coffee",
"scala": "scala",
"swift": "swift",
"julia": "julia",
"crystal": "crystal",
"ocaml": "ocaml",
"r": "Rscript",
"applescript": "osascript",
"clojure": "lein exec",
"haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
"racket": "racket",
"ahk": "autohotkey",
"autoit": "autoit3",
"dart": "dart",
"pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
"d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
"haskell": "runhaskell",
"nim": "nim compile --verbosity:0 --hints:off --run",
"lisp": "sbcl --script"
}
}
3、终端执行命令:
sudo apt install apt-file
sudo apt-file update
sudo apt-file search opcv.pc
pkg-config --cflags opencv
显示如下-I /usr/local...即说明环境配置成功
回到我们的VScode重新run一下试试看!
普天同庆阿!!!终于搞好了!!