參考鏈接:
gcc/g++ 編譯命令
ubuntu16.04+VS Code+OpenCV配置编译运行程序
Ubuntu 18.04系统下的环境配置vscode&&opencv&&c++
感謝大佬們!!
我這ubuntu的輸入法有問題啊!
环境:
ubuntu20.04
gcc (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0 之前装的7.5版本也是可以的,后来双系统被我搞崩了,顺手装的最新版本
vscode
opencv4.5.5 截止目前最高版本,我本来下的4.1.0版本,最后稀里糊涂的最高版本了,也是很迷
2022年1月7更新:
上面opencv版本是个大坑,我错的原因: 我是git clone的方式下载的opencv源码,这种方式默认下载最新版本的opencv,需要再git checkbranch到指定版本才行,但是我没有做这个操作,后来find_package我set了opencv4.pc的路径,所以出现多个opencv的版本,我全部卸掉重新安装的结果是对的。
出现这个界面就是对的!
添加扩展:
直接在Extension中搜C++, 一般第一个就是
vscode-cpptools 配置
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": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-I", "/usr/local/include/opencv4",
"-I", "/usr/local/include",
"-L", "/usr/local/lib",
"-l", "opencv_core",
"-l", "opencv_imgproc",
"-l", "opencv_imgcodecs",
"-l", "opencv_video",
"-l", "opencv_ml",
"-l", "opencv_highgui",
"-l", "opencv_objdetect",
"-l", "opencv_flann",
"-l", "opencv_imgcodecs",
"-l", "opencv_photo",
"-l", "opencv_videoio"
],
"options": {
"cwd": "/usr/bin"
},
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
到这里配置就完成了,没有那么多花里胡哨的,就这么配就可以了,我报了很多错误也走了很多弯路,但是有效的就是这样的。
opencv编译以及查看配置正确与否就是链接1.
opencv的github链接:
opencv
opencv_contrib
我想编译debug版本,所以我的cmake代码是这样的
sudo cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/qiao/c_project/opencv_/opencv_contrib/modules ..
如果想编译Release版本就是
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
测试程序:
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
//读取照片
Mat image = imread("/home/qiao/1.png");
//检测失误
if (image.empty())
{
cout << "Could not open or find the image" << endl;
cin.get(); //等待键盘输入
return -1;
}
String windowName = "OpenCV Test"; //窗口名称
namedWindow(windowName); //创建新窗口
imshow(windowName, image); //使用新窗口显示照片
waitKey(0); //等待键盘输入
destroyWindow(windowName); //关闭所有窗口
return 0;
}
用命令行编译的代码:
g++ main.cpp -o main $(pkg-config --cflags --libs opencv)
./main
到这里理论上是不报错,编译正确且显示正确的。