测试用例:
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat srcImage=imread("1.jpg");
imshow("Origin",srcImage);
waitKey(0);
return 0;
}
错误描述:调试下正常显示图片,直接run会报错各种函数未定义,如下图
解决方法:这里主要是链接器没有进行配置。我们安装好Opencv后,没有配置环境变量,所以我们就给CPP部分添加 pkg-config opencv --libs --cflags opencv
这个东西,这个东西好像是引导g++调用opencv的,很关键,没有它程序会报错。需要修改一下vscode中工作空间的settings.json文件。
1、打开vscode-文件-首选项-设置-扩展-Run Code configuration-找到code-runner:Executor.Map下在settings.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、终端执行命令:
apt install apt-file
apt-file update
apt-file search opencv.pc
pkg-config --cflags opencv
结果显示 -I opencv opencv2 这就说明环境配置成功了,可以在vscode下进行run测试了。