我正在尝试在 VSCode 中构建我的 C++ 项目。但是,我遇到了 OpenCV 的链接问题“错误 LNK2001:未解析的外部符号”。我已经构建了我使用 vcpkg 的所有库。
我使用这个 .bat 文件构建:
@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
我的 tasks.json 文件是:
{
"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 文件是:
{
"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 文件是:
{
"terminal.integrated.shell.windows": "cmd.exe"
}
我似乎找不到任何关于如何使用 MSVS 编译器将 vcpkg 库与 VSCode 正确链接的文档。非常感谢您的帮助。
最佳答案
我最近在 Windows 10 上安装了 OpenCV 4.3.0 (64 位) 并且必须在 Visual Studio Code (VSC) 中配置工作区构建一个简单的应用程序。
以下配置使用 x64 版本的 cl.exe
,这是 Microsoft Visual Studio 2019(社区版)附带的 C/C++ 编译器,用于构建 OpenCV 应用程序.
请注意此 tasks.json
文件中定义的路径,因为它们在您的系统中可能有所不同:
{
"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,
}
}
]
}
此配置必须用于替换您工作区中的配置。尝试运行任务时,它将为您提供两个选项供您选择:
build_vs2019:定义shell为
cmd.exe
并执行vcvars64.bat
设置环境变量和路径您使用 x64 版本的cl.exe
。它还指定了构建基于 OpenCV 的应用程序所需的 header 和库。此选项构建应用程序。run:取决于上一个在命令行上运行 OpenCV 应用程序的任务是否成功。它调整
PATH
环境变量以指向 OpenCV DLLs 目录。此选项执行应用程序。
关于c++ - 在 VSCode 中使用 MSVS 编译器构建 OpenCV 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51571894/