使用VSCode中的MSVS编译器构建OpenCV应用程序 [英] Build OpenCV application with MSVS compiler in VSCode

查看:390
本文介绍了使用VSCode中的MSVS编译器构建OpenCV应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用VSCode构建我的C ++项目。但是,我遇到了OpenCV错误LNK2001:无法解析的外部符号的链接问题。我已经构建了vcpkg使用的所有库。



我使用以下.bat文件构建:

  @ 
如果存在,则回显 C:\Program Files(x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\ vcvarsall.bat(
调用 C:\Program Files(x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat x64
)else(
调用 C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat x64

set editor.flags = / Od / Zi / EHsc / std:c ++ latest / I include / IC:\includes\vcpkg\installed\x64-windows\include
set linkerflags = / OUT:bin\main.exe
cl.exe%compilerflags%src\ * .cpp / link%linkerflags%/ LIBPATH:C:\includes\vcpkg\已安装\x64-windows \lib
del bin\ * .ilk * .obj * .pdb

我的task.json文件为:

  {
version: 2.0.0,
任务:[[
{
la bel:建立C ++项目,
type: shell,
group:{
kind: build,
isDefault: true
},
command: .\\build.bat
},
{
label: Build&运行C ++项目,
type: shell,
group:{
kind: test,
isDefault:true
},
command: .\\build.bat&& .\\bin\\main.exe
}
]
}

我的launch.json文件为:

  {
version: 0.2 .0,
配置:[
{
name: C ++调试(Windows),
type: cppvsdbg,
请求:启动,
程序: $ {workspaceFolder} /bin/main.exe,
preLaunchTask:建立C ++项目,
args :[],
stopAtEntry:false,
cwd: $ {workspaceFolder},
environment:[],
externalConsole:true,
}
]
}

最后是我的settings.json文件是:

  {
terminal.integrated.shell.windows: cmd.exe
}

我似乎找不到有关如何使用VSCode正确链接vcpkg库的任何文档。 MSVS编译器。我ld非常感谢您的帮助。

解决方案

我最近安装了 OpenCV 4.3.0 (64位),并且必须在 Visual Studio代码(VSC)中配置工作区以构建简单的应用程序。


以下配置使用 cl.exe 的x64版本,它是 Microsoft Visual Studio 2019 (社区版)附带的C / C ++编译器,用于构建OpenCV应用程序。 / p>

请注意在此 tasks.json 文件中定义的路径,因为它们在您的系统中可能不同:

  {
version: 2.0.0&,
windows:{
options: {
shell:{
executable: C:\\WINDOWS\\System32\\cmd.exe,
args ;:[ / d, / c; ]
}
},
isShellCommand:true,
showOutput: always,
echoCommand:true,
},
任务:[
{
label: build_vs2019,
type: shell,
windows:{
command: call C C:\\Program Files(x86)\\Microsoft Visual Studio\\2019\\社区\\VC\\辅助\\Build\\vcvars64.bat\; && cl.exe,
args:[
/ Zi,
/ EHsc,
/ Fe:,
$ {fileDirname} \\ $ {fileBasenameNoExtension} .exe,
$ {file},
-I, C:\\ opencv\\build\\include,
/ link, / libpath:C:\\opencv\\build\\x64\\vc15 \\lib, opencv_world430.lib;
],
problemMatcher:[ $ msCompile; ],
},
group:{
kind: build,
isDefault:true
}
},
{
label: run,
type: shell,
dependsOn:[ build_vs2019 ],
windows:{
command: $ {fileDirname} \\ $ {fileBasenameNoExtension} .exe,
args:[[ ; superDark.jpg ],
选项:{
env:{
PATH: C:\\opencv\\build\\x64 vc15\\bin;
}
}
},
演示:{
reveal:沉默,
clear:true ,
和 showReuseMessage:false,
}
}
]
}

此配置必须用于替换工作空间中的配置。尝试运行任务时,它将为您提供两个选项:




  • build_vs2019 :将外壳定义为 cmd.exe 并执行 vcvars64.bat 设置环境变量和路径,使您可以使用x64版本的 cl.exe 。它还指定了头文件和所需的库,以构建基于OpenCV的应用程序。此选项将构建应用程序。



  • 运行:取决于运行OpenCV应用程序的上一个任务是否成功在cmd线上。它将调整 PATH 环境变量以指向OpenCV DLL目录。此选项执行应用程序。




I'm trying to build my C++ project in VSCode. However, I'm experiencing link issues with OpenCV "error LNK2001: unresolved external symbol". I've build all the libraries I use with vcpkg.

I build using this .bat file:

@echo off
if exist "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" (
    call "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64
) else (
    call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
)
set compilerflags=/Od /Zi /EHsc /std:c++latest /I include /I C:\includes\vcpkg\installed\x64-windows\include
set linkerflags=/OUT:bin\main.exe
cl.exe %compilerflags% src\*.cpp /link %linkerflags% /LIBPATH:C:\includes\vcpkg\installed\x64-windows\lib
del bin\*.ilk *.obj *.pdb

My tasks.json file is:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build C++ project",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command": ".\\build.bat"
        },
        {
            "label": "Build & run C++ project",
            "type": "shell",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "command": ".\\build.bat && .\\bin\\main.exe"
        }
    ]
}

My launch.json file is:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Debug (Windows)",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/main.exe",
            "preLaunchTask": "Build C++ project",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
        }
    ]
}

and finally my settings.json file is:

{
    "terminal.integrated.shell.windows": "cmd.exe"
}

I can't seem to find any documentation on how to properly link vcpkg libraries with VSCode using the MSVS compiler. I would really appreciate some help.

解决方案

I recently installed OpenCV 4.3.0 (64bits) on Windows 10 and had to configure a workspace in Visual Studio Code (VSC) to build a simple application.

The following configuration uses the x64 version of cl.exe, the C/C++ compiler that ships with Microsoft Visual Studio 2019 (Community Edition) to build the OpenCV application.

Pay attention to the paths defined on this tasks.json file because they might be different in your system:

{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "C:\\WINDOWS\\System32\\cmd.exe",
                "args": [ "/d", "/c" ]
            }
        },
        "isShellCommand": true,
        "showOutput": "always",
        "echoCommand": true,
    },
    "tasks": [
        {
            "label": "build_vs2019",
            "type": "shell",    
            "windows": {
                "command": "call \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\" && cl.exe",
                "args": [
                    "/Zi",
                    "/EHsc",
                    "/Fe:",
                    "${fileDirname}\\${fileBasenameNoExtension}.exe",
                    "${file}",
                    "-I", "C:\\opencv\\build\\include",
                    "/link", "/libpath:C:\\opencv\\build\\x64\\vc15\\lib", "opencv_world430.lib"
                ],              
                "problemMatcher": [ "$msCompile" ],
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "run",
            "type": "shell",
            "dependsOn": [ "build_vs2019" ],
            "windows": {
                "command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "args": [ "superDark.jpg" ],
                "options": {
                    "env": {
                        "PATH": "C:\\opencv\\build\\x64\\vc15\\bin"
                    }
                }
            },  
            "presentation": {               
                "reveal": "silent",
                "clear": true,
                "showReuseMessage": false,
            }
        }
    ]
}

This configuration must be used to replace the one in your workspace. It will give you two options to select from when attempting to run tasks:

  • build_vs2019: defines the shell as cmd.exe and executes vcvars64.bat to setup the environment variables and paths that let you use the x64 version of cl.exe. It also specifies the headers and required libraries to build an OpenCV-based application. This option builds the application.

  • run: depends on the success of the previous task to run the OpenCV application on the cmd-line. It adjusts the PATH environment variable to point to OpenCV DLLs directory. This option executes the application.

这篇关于使用VSCode中的MSVS编译器构建OpenCV应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆