Ubuntu下vscode调试opencv库正常,运行报未定义错误解决方法

测试用例

#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测试了。

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值