【vscode】使用vscode多源文件结合opencv编译调试 C/C++

使用vscode多源文件结合opencv库进行开发

vscode插件只需要安装



1、新建文件夹OPENCVTEST

2、添加文件main.cpp

内容如下:

#include "Lib/OpencvTest.h"
#include 

int main()
{
    OpencvTest* opencvtest = new OpencvTest();
    opencvtest->readImage();
    delete opencvtest;
    return 0;
}

3、新建文件夹Lib

4、添加文件OpencvTest.cpp与OpencvTest.h

内容如下:

OpencvTest.h

#pragma once

class OpencvTest
{
public:
    void readImage();
    OpencvTest();
    ~OpencvTest();
};
OpencvTest.cpp

#include "OpencvTest.h"
#include 
#include 

void OpencvTest::readImage()
{
    cv::Mat img = cv::imread("lena.jpg");
    cv::imshow("ImageShow", img);
    cv::waitKey(0);
}

OpencvTest::OpencvTest()
{
    std::cout << "OpencvTest Object Constructed" << std::cout;
}

OpencvTest::~OpencvTest()
{
    std::cout << "OpencvTest Object Destructed" << std::cout;
}

5、F5运行,选择C++(GDB/LLDB),生成launch.json文件,用以下内容替换该文件内容

注意修改miDebuggerPath的值,同样的如果用qt提供的mingw,我这里修改为D:/Qt/Qt5.7.0/Tools/mingw530_32/bin/gdb.exe同样可以调试运行

//launch.json    
{    
    "version": "0.2.0",    
    "configurations": [    
          
        {    
            "name": "C++ Launch",    
            "type": "cppdbg",    
            "request": "launch",    
            "program": "${file}.o",    
            "args": [],    
            "cwd": "${workspaceRoot}",    
            "environment": [],    
            "externalConsole": true,    
            "preLaunchTask": "g++",    
            "linux": {    
                "miDebuggerPath": "/usr/bin/gdb",    
                "MIMode": "gdb",    
                "setupCommands": [    
                    {    
                        "description": "Enable pretty-printing for gdb",    
                        "text": "-enable-pretty-printing",    
                        "ignoreFailures": true    
                    }    
                ]    
            },    
            "osx": {    
                "MIMode": "lldb"    
            },    
            "windows": {    
                "miDebuggerPath": "D:/Qt/Qt5.7.0/Tools/mingw530_32/bin/gdb.exe",    
                "MIMode": "gdb",    
                "setupCommands": [    
                    {    
                        "description": "Enable pretty-printing for gdb",    
                        "text": "-enable-pretty-printing",    
                        "ignoreFailures": true    
                    }    
                ]    
            }    
        }    
    ]    
}   


6、F5运行,配置任务,使用模版创建tasks.json文件,随便选择一个,生成tasks.json文件,用以下内容替换该文件内容

//tasks.json    
{    
    "version": "0.1.0",    
    "command": "g++",    
    "args": [    
        "-g",    
        "${file}",    
        "-o",    
        "${file}.o"    
    ],    
    "problemMatcher": {    
        "owner": "cpp",    
        "fileLocation": [    
            "relative",    
            "${workspaceRoot}"    
        ],    
        "pattern": {    
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",    
            "file": 1,    
            "line": 2,    
            "column": 3,    
            "severity": 4,    
            "message": 5    
        }    
    }    
}  

7、编写CMakeList.txt

每一个包含源文件的目录中都要编写CMakeLists.txt

根目录中的CMakeLists.txt,一般程序入口在此。内容如下:

# 使用CMake Tools插件(可选,如果这个项目去到一个没有这个插件的机器也同样可以生成项目)
include(CMakeToolsHelpers OPTIONAL)

# CMake 最低版本号要求
cmake_minimum_required(VERSION 2.8)

# 项目名称
project(OpencvTest)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_ROOT_SRCS变量
aux_source_directory(. DIR_ROOT_SRCS)

# 添加 Lib子目录
add_subdirectory(Lib)

# 指定生成目标
add_executable(OpencvTest main.cpp ${DIR_ROOT_SRCS})

# 添加链接库
target_link_libraries(OpencvTest OpencvLib)

子目录中的Lib/CMakeLists.txt,一般将子目录中的源文件编译为静态链接库。内容如下:

opencv链接库是使用cmake用mingw编译出来的,可参考 http://blog.csdn.net/qq_15947787/article/details/77600099

opencv头文件以及链接库路径可根据实际情况进行修改

include(CMakeToolsHelpers OPTIONAL)

cmake_minimum_required(VERSION 2.8)

#添加opencv头文件的搜索路径  
INCLUDE_DIRECTORIES(D:/opencv2.4.9/build/include/opencv)
INCLUDE_DIRECTORIES(D:/opencv2.4.9/build/include/opencv2)
INCLUDE_DIRECTORIES(D:/opencv2.4.9/build/include)

# 查找路径下的所有源文件
aux_source_directory(. DIR_LIB_SRCS)

# 生成链接库
add_library(OpencvLib ${DIR_LIB_SRCS})

# 添加链接库  
TARGET_LINK_LIBRARIES(OpencvLib D:/opencv2.4.9/build/x86/mingw/lib/libopencv_core249.dll.a)  
TARGET_LINK_LIBRARIES(OpencvLib D:/opencv2.4.9/build/x86/mingw/lib/libopencv_highgui249.dll.a)  
TARGET_LINK_LIBRARIES(OpencvLib D:/opencv2.4.9/build/x86/mingw/lib/libopencv_imgproc249.dll.a)  

8、在vscode终端下输入(找不到终端?F5调试就弹出来了)

cmake -G "MinGW Makefiles"


mingw32-make
【vscode】使用vscode多源文件结合opencv编译调试 C/C++_第1张图片

9、运行程序

在vscode终端下输入(也就是刚才CMakeLists.txt中的project项目名称,前提要在opencvtest中放入lena.jpg这张图)

.\OpencvTest.exe

最终的工程截图:

【vscode】使用vscode多源文件结合opencv编译调试 C/C++_第2张图片


工程下载:http://download.csdn.net/download/qq_15947787/10151111

参考:http://www.jianshu.com/p/a0ae073e973b

你可能感兴趣的:(opencv,c++,opencv)