首页 > 解决方案 > 无法使用带有 msvc 的 OpenCV 调试构建来编译项目

问题描述

opencv_world451d.lib 
helloworld.obj 
helloworld.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV_InputArray@1@@Z) referenced in function main
C:\Users\kaito\OneDrive\Documents\Cpp\OpenCVHelloWorld\helloworld.exe : fatal error LNK1120: 1 unresolved externals

当我使用 OpenCV debug buildopencv_world451d.lib时,vscode 不会编译我的项目。如果我切换到 OpenCV release build opencv_world451.lib,那么一切都很好。

这很奇怪,因为我在 Visual Studio 2019 中使用了完全相同的设置,并且无论是调试还是发布版本都一切正常。CMake 也构建它。

这是我的tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Wall",
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "/I",
                "C:/Program Files/opencv/build/include",
                "/link",
                "/LIBPATH:C:/Program Files/opencv/build/x64/vc15/lib",
                "opencv_world451d.lib"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always"
            },
            "detail": "compiler: cl.exe"
        }
    ]
}

使用调试构建时是否需要正确设置任何设置?

标签: c++opencvvisual-studio-code

解决方案


我通过使用编译器标志 /MDd 而不是 /MD 解决了这个问题。看起来调试构建需要适当的调试运行时。


推荐阅读