Experimental record - mac configures opencv with visual studio code

install opencv

Install opencv with Homebrew

brew install wget
brew install cmake
brew install opencv

 

 

Looks like it's already installed hhh

Check the installed opencv version again by the following command

brew info opencv

 If everything is ticked in green, it should be installed.

Vscode configures c/c++ environment

To configure the cpp project, you can see the following link: VScode C++ Environment Configuration (MAC)

Install the extension first

C/C++

CodeLLDB

Then create a cpp file

#include <iostream>
using namespace std;
int main(){
    cout << "hello world" << endl;
    return 0;
}

 Click Debug directly, click Run Debug

 Choose C++ (GDB/LLDB)

 It doesn't seem to work here. . .

Install the following dependencies instead

Then debugging will automatically generate the file

 Then just run

Try changing the code to the following

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main(){
    int b = 1;
    auto a = b;
    cout << a << endl;
    vector<string> msg{"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
    int len = msg.size();
    cout << "长度" << len << endl;
    for (int i=0; i<5; i++){
        // ms.push_back("a");
        cout << i << endl;
    }

    return 0;
}

  but report an error

 The reason is that the current compilation does not support C++11 and later standards. I feel that there is still a problem with this compilation method.

Try this link instead: vsCode Mac version configures C/C++ and runs the code_how to run c++ code on mac vscode_water w's blog-CSDN blog

Use vscode to debug and run, click the debug button on the right, click the run and debug arrows, and select "run code"

 

 It seems that it is more straightforward to run this way, try to add opencv to see if it is feasible.

no. . .

.vscode The following three files need to be stored in the folder under the project folder  .

  • c_cpp_properties.json:.hpp The function of this file is to configure vscode to configure the overall C++ environment. It is to tell vscode IDE what header files and library files  we need to include  .lib, and understand this as a preparatory work.
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/Cellar/opencv/4.5.3/lib/**",
	            "/usr/local/Cellar/opencv/4.5.3/include/opencv4/"
            ],
            // “includePath” 后面就是放的就是头文件和库文件所在的路径,每个人的情况不同,可以把下面的路径输入进去查找一下有没有对应的文件,以进行确认。
            // 后面的两个星号 ** 表示递归查找,就是查找目标目录的同时,该目录下的子目录也一并查找。
            "defines": [],
            "macFrameworkPath": [],
            "compilerPath": "/usr/local/bin/gcc-11",
            "cStandard": "gnu17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "macos-gcc-x64"
        }
    ],
    "version": 4
}

To check the location, you can use the following methods:

Open the Finder, and search.

  • launch.json:This is the configuration file used by vscode for debugging, such as specifying the debugging locale, specifying the debugging type, and so on. The most important thing here is  "preLaunchTask" the setting, indicating which preconditions need to be told to the compiler before running this debug. In fact, it is to  tasks.json bring in the connection.
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "g++-11 - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            // "program": "${fileDirname}/${fileBasenameNoExtension}",
            "program": "/Users/guanweipeng/Desktop/project/main",
            "args": [],
            "stopAtEntry": false,
            // "cwd": "${fileDirname}",
            "cwd": "/Users/guanweipeng/Desktop/project",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: g++-11 生成活动文件"
        }
    ]
}
  • tasks.json:This file is the configuration of the compilation. You can set which library files and header files you need to use during the compilation process, which compiler to use, and which compilation method to use. You can set them here. The key here is  "label" that the settings must be consistent with those in launch.json  "preLaunchTask" !
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++-11 生成活动文件",
			"command": "/usr/bin/clang++",
			"args": [
				"-std=c++17",//使用C++17
				"-stdlib=libc++",
				"-g",
				"${file}",
				"-o",
				// "${fileDirname}/${fileBasenameNoExtension}",
                "/Users/guanweipeng/Desktop/project/main",
				"`pkg-config",
				"--libs",
				"--cflags",
				"opencv4`"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "C/C++: g++-11 生成活动文件"
		}
	]
}

This time you can see that include opencv will not be marked in red

 Then enter 工作区the option, find it  C/C++ , and then find it on the right  C_Cpp > Default: Include Path , add the path of the header file and library file in it.

 

Then the file directory appears as follows:

 

The content in setting is:

{
    "C_Cpp.default.includePath": [
        "/usr/local/Cellar/opencv/4.5.3/lib/",
        "/usr/local/Cellar/opencv/4.5.3/include/opencv4/"
    ]
}

 add cpp standard

{
    "C_Cpp.default.cppStandard": "c++17",
    "C_Cpp.default.includePath": [
        "/usr/local/Cellar/opencv/4.5.3/lib/",
        "/usr/local/Cellar/opencv/4.5.3/include/opencv4/"
    ]
}

Can!

 

References

[OpenCV4] fatal error: opencv2/core.hpp: No such file or directory solution_AItrust's Blog-CSDN Blog

[OpenCV4] Using vscode to configure the c++ version of OpenCV4.5.4 successfully under the Mac system (January 22, 2022)_AItrust's Blog-CSDN Blog

Mac vscode configuration opencv-nuggets

macOS version homebrew+VScode configuration opencv (c/c++) pro-test available - Programmer Sought

Guess you like

Origin blog.csdn.net/gwplovekimi/article/details/131334798