在 VSCode 中使用 MSVS 编译器构建 OpenCV 应用程序
我正在尝试在 VSCode 中构建我的 C++ 项目.但是,我遇到 OpenCV 的链接问题错误 LNK2001:未解析的外部符号".我已经构建了与 vcpkg 一起使用的所有库.
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.
我使用这个 .bat 文件构建:
I build using this .bat file:
@echo off
if exist "C:Program Files (x86)Microsoft Visual Studio2019BuildToolsVCAuxiliaryBuildvcvarsall.bat" (
call "C:Program Files (x86)Microsoft Visual Studio2019BuildToolsVCAuxiliaryBuildvcvarsall.bat" x64
) else (
call "C:Program Files (x86)Microsoft Visual Studio2019CommunityVCAuxiliaryBuildvcvarsall.bat" x64
)
set compilerflags=/Od /Zi /EHsc /std:c++latest /I include /I C:includesvcpkginstalledx64-windowsinclude
set linkerflags=/OUT:binmain.exe
cl.exe %compilerflags% src*.cpp /link %linkerflags% /LIBPATH:C:includesvcpkginstalledx64-windowslib
del bin*.ilk *.obj *.pdb
我的 tasks.json 文件是:
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"
}
]
}
我的launch.json文件是:
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,
}
]
}
最后我的 settings.json 文件是:
and finally my settings.json file is:
{
"terminal.integrated.shell.windows": "cmd.exe"
}
我似乎找不到任何关于如何使用 MSVS 编译器将 vcpkg 库与 VSCode 正确链接的文档.我真的很感激一些帮助.
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.
推荐答案
我最近在 Windows 10 上安装了 OpenCV 4.3.0 (64bits) 并且不得不配置一个工作区在 Visual Studio Code (VSC) 中构建一个简单的应用程序.
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.
以下配置使用 cl.exe
的 x64 版本,这是 Microsoft Visual Studio 2019(社区版)附带的 C/C++ 编译器来构建 OpenCV 应用程序.
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.
请注意在此 tasks.json
文件中定义的路径,因为它们在您的系统中可能会有所不同:
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:将shell定义为
cmd.exe
并执行vcvars64.bat
来设置环境变量和路径,让您使用cl.exe
的 x64 版本.它还指定了构建基于 OpenCV 的应用程序所需的头文件和所需的库.此选项构建应用程序.
build_vs2019: defines the shell as
cmd.exe
and executesvcvars64.bat
to setup the environment variables and paths that let you use the x64 version ofcl.exe
. It also specifies the headers and required libraries to build an OpenCV-based application. This option builds the application.
run:取决于上一个任务的成功,在 cmd 行上运行 OpenCV 应用程序.它调整 PATH
环境变量以指向 OpenCV DLLs 目录.此选项执行应用程序.
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.
相关文章