我正在尝试在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
我的task.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代码(VSC)来构建一个简单的应用程序。
以下配置使用的x64版本,即 Microsoft Visual Studio 2019 (社区版)附带的C / C++编译器来构建OpenCV应用程序。
请注意此cl.exe
文件上定义的路径,因为它们在您的系统中可能不同:
必须使用此配置替换工作空间中的配置。尝试运行任务时,它将提供两个选项供您选择:{
"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,
}
}
]
}
tasks.json
并执行cmd.exe
来设置环境变量和路径,以使您能够使用x64版本的vcvars64.bat
。它还指定了头文件和所需的库,以构建基于OpenCV的应用程序。此选项生成应用程序。
cl.exe
环境变量调整为指向OpenCV DLL目录。此选项执行应用程序。
关于c++ - 使用VSCode中的MSVS编译器构建OpenCV应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58887484/