VS code 运行&调试opencv C++配置

文章目录

    • VS code opencv C++配置
      • 1 新建opencv_test.cpp测试代码
      • 2 配置launch.json
      • 3.tasks.json
    • 配置Makefile文件,不使用VScode的json配置文件
    • Reference

VS code opencv C++配置

之前一直在windows上用Visual studio配置opencv,现在经常使用Ubuntu系统,也需要编写opencv C++代码。当然使用Qt creator配置opencv环境是一种选择,只需要在.pro文件中增加引用目录和库目录就行:

INCLUDEPATH += /usr/local/include \
                    /usr/local/include/opencv \
                    /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_highgui.so \
            /usr/local/lib/libopencv_video.so \
            /usr/local/lib/libopencv_videoio.so \
            /usr/local/lib/libopencv_core.so    \
            /usr/local/lib/libopencv_imgproc.so \
            /usr/local/lib/libopencv_imgcodecs.so #按照项目需要添加库文件,不必全部添加

我平时更喜欢使用VScode,所以也把VScode的opencv配置了一遍,下面介绍一下主要步骤。

注意:本教程需要当前linux已经安装好opencv,即opencv库文件存在/usr/local/lib目录下;当然如果是自定义的目录,那么修改教程中的相应目录即可。
如果opencv安装在了默认目录/usr/local,那么VScode会自动搜索该目录,实现opencv自动补全和智能提示;否则,需要配置c_cpp_properties.json文件,将includePath配置为opencv安装目录。

1 新建opencv_test.cpp测试代码

#include
#include
using namespace cv;
int main()
{
    Mat img=imread("/media/han/E/mWork/datasets/opencv/HappyFish.jpg");
    cv::imshow("image",img);
    cv::waitKey();
    return 0;
}

2 配置launch.json

打开launch.json的方法:Ctrl +Shift +P ,输入launch,选择Debug:open 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": "${workspaceFolder}/${fileBasenameNoExtension}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

${workspaceFolder}表示当前工作目录,{fileBasenameNoExtension}表示当前文件名,不包括扩展
"program": "${workspaceFolder}/${fileBasenameNoExtension}.o",表示生成的执行文件在当前目录下,和当前文件同名,只是扩展为.o;例如本教程会生成opencv_test.o执行文件。

VS code中预定义路径相关变量

  • ${workspaceFolder} - the path of the folder opened in VS Code
  • ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
  • ${file} - the current opened file
  • ${relativeFile} - the current opened file relative to workspaceFolder
  • ${relativeFileDirname} - the current opened file’s dirname relative to workspaceFolder
  • ${fileBasename} - the current opened file’s basename
  • ${fileBasenameNoExtension} - the current opened file’s basename with no file extension
  • ${fileDirname} - the current opened file’s dirname
  • ${fileExtname} - the current opened file’s extension
  • ${cwd} - the task runner’s current working directory on startup
  • ${lineNumber} - the current selected line number in the active file
  • ${selectedText} - the current selected text in the active file
  • ${execPath} - the path to the running VS Code executable
    更多预定于变量参考 https://code.visualstudio.com/docs/editor/variables-reference

"preLaunchTask": "build",是默认launch.json中没有的,表示执行文件前需要的编译任务。具体的任务内容我们在task.json中定义。

3.tasks.json

打开tasks.json的方法:Ctrl +Shift +P ,输入task,选择Tasks:Configure Task

{
    // 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", "${file}","-o","${fileBasenameNoExtension}.o",
                "-I","/usr/local/include",
                "-I","/usr/local/include/opnecv",
                "-I","/usr/local/include/opnecv2",
                "-L","/usr/local/lib",
                "-l","opencv_core",
                "-l","opencv_highgui",
                "-l","opencv_imgproc",
                "-l","opencv_imgcodecs"                
            ]
        }
    ]
}

"label": "build"表示以下是名为build的任务配置

"command": "g++",表示调用的编译器是g++

args表示command的命令,其中“-I”表示包含目录,"-L"表示库目录路径,"-l"表示库文件,注意:/usr/local/lib目录下的库文件名为libopencv_core.so,但是我们只需要写opencv_core就行了。添加好自己需要的库文件,然后调试&运行opencv_test.cpp就可以了。

配置Makefile文件,不使用VScode的json配置文件

Makefile文件如下:

CROSS = 
CC = $(CROSS)gcc
CXX = $(CROSS)g++
DEBUG = -g -O2
CFLAGS = $(DEBUG) -Wall -c
RM = rm -rf

SRCS = $(wildcard ./*.cpp) 
OBJS = $(patsubst %.cpp, %.o, $(SRCS))

HEADER_PATH = -I/usr/local/include/ -I/usr/local/opencv -I/usr/local/opencv2
LIB_PATH = -L/usr/local/lib
LIBS = -lopencv_core -lopencv_highgui -lopencv_imgcodecs

VERSION = 1.0
TARGET = test_$(VERSION)

$(TARGET) : $(OBJS)
	$(CXX) $^ -o $@ $(LIB_PATH) $(LIBS) 

$(OBJS):%.o : %.cpp
	$(CXX) $(CFLAGS) $< -o $@ $(HEADER_PATH) 

.PHONY: clean
clean :
	-$(RM) $(TARGET) *.o 

wildcard通配符,wildcard把指定目录./下的所有后缀是cpp的文件全部展开;获取工作目录下的所有的.cpp文件列表
patsubst替换通配符,patsubst$(SRCS)中的变量符合后缀是.cpp的全部替换成.o
#$@是自动变量,指目标的名字TARGET
#$^指代所有前置条件,之间以空格分隔
#$<指代第一个前置条件

Reference

https://blog.csdn.net/qq849635649/article/details/51564338
https://seisman.github.io/how-to-write-makefile/variables.html#id7
https://code.visualstudio.com/docs/editor/variables-reference

你可能感兴趣的:(VS,opencv,ubuntu)