Build OpenCV application with MSVS compiler in VSCode

前端 未结 1 440
一个人的身影
一个人的身影 2020-12-21 21:39

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 librarie

相关标签:
1条回答
  • 2020-12-21 22:30

    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.

    0 讨论(0)
提交回复
热议问题