opencv.hpp file not found vscode Mac M1 - c++

I have been trying to set up opencv on my M1 chip Mac in Vscode, I used home-brew and followed all of the necessary steps to install it on my Mac. I created a vscode project and included it in the c_cpp_properties.json and task.json files. When I wrote up a simple test program I built the program using g++ but the build failed and threw an error stating:
fatal error: 'opencv2/opencv.hpp' file not found
I tried a bunch of different ways of including the libraries but there is still a squiggled line in front of #include <opencv2/opencv.hpp> in my program which wasn't there prior to building the program. Intellisense provides me with suggestions in the program and everything. I went through the few existing questions on stack overflow and none of them had a working solution for me.
I tried a bunch of different solutions that I found online and in and around the forums but nothing seems to be working
These are my c_cpp_properties.json and task.json files:
Task.json:
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I",
"/Users/user/Desktop/OpenCV/opencv/include/opencv2",
"/usr/local/include/opencv4"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/g++"
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/Users/user/Desktop/OpenCV/opencv/include/opencv2",
"/usr/local/include/opencv4"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
the code for my sample code is as follows:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
///////////////// Images //////////////////////
int main() {
string path = "/Users/brettmylek/Desktop/OpenCV/Img/TestImg.jpg";
Mat img = imread(path);
imshow("Image", img);
waitKey(0);
return 0;
}
it is simply there to open an image to test whether or not the install was successful so I could go on developing my actual project.

I'll start with c_cpp_properties.json more precisely the includePath element. This element is only and only for IntelliSense. It has no other use whatsoever. This should answer why IntelliSense is working.
If you then look into tasks.json one thing that caught my eye is this:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
--> "-I",
--> "/Users/user/Desktop/OpenCV/opencv/include/opencv2",
--> "/usr/local/include/opencv4"
],
This most likely doesn't work the way you think it does. What you want instead is:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/Users/user/Desktop/OpenCV/opencv/include/opencv2",
"-I/usr/local/include/opencv4"
],
EDIT: You might have to link against built opencv libraries (last time I checked it's not a header-only library)
EDIT2: Also are you sure about this path: /Users/user/Desktop/OpenCV/opencv/include/opencv2"
Last time I checked there is no /Users on Linux
EDIT3: I've checked their github. If the error persists it's most likely a wrong include path i.e. instead of
"-I/Users/user/Desktop/OpenCV/opencv/include/opencv2"
Maybe it should be (based on their github) this:
"-I/Users/user/Desktop/OpenCV/opencv/include"

Related

How can I include .dylib files in ".json" configuration files? - VS Code on Mac iOS - Clang++ compiler - Language: c++

I unsuccessfully was looking 2 days on google to find a clear paragraph to describe how to include dynamic libraries (files with .dylib extension on Mac iOS) in order to be compiled by clang++ when someone is setting up the task.json and/or c_cpp_properties.json files - (prior to press F5 in order to launch the task and execute the source code)
Particularly I would like to include next two .dylib files:
/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib;
/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib;
Have to mention that I successfully succeeded to make the clang++ to compile in the OpenGL main.cpp file both glew.h and glfw3.h headers as per the following snippet:
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
...
This task was accomplished writing the next c_cpp_properties.json file:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "macos-gcc-x64"
}
],
"version": 4
}
where the magic is done by "macFrameworkPath" instruction.
But yet is not enough.
I still have this error when the clang++ compiles:
clang: error: linker command failed with exit code 1 (use -v to see invocation).
And this - as I understood after googling the solution - happens because is necessary to include those two dynamic libraries which I mentioned earlier.
But as I said in the beginning I didn't find how to do it.
Maybe someone can come up with a clear and appropriate solution.
My best guess is to add a new argument in task.json script, which by the way looks like that:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
"-o",
"${fileDirname}/src/${fileBasenameNoExtension}",
"-Wno-deprecated",
"-Wno-pragma-once-outside-header"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Best regards to all readers.
It seems you are indeed need to pass the libs into arguments of clang++.
Try to do this with -l flag, pointing to target library.
Add
"-l /usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib"
and
"-l /usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib" to the args list of strings.
The solution to this particular problem is to add in task.json the next command arguments:
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
"/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib",
"/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib",
"-o",
"${fileDirname}/src/${fileBasenameNoExtension}",
"-Wno-deprecated",
"-Wno-pragma-once-outside-header"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
As everyone can see, simply writing the full path of the file as additional argument will be accepted by compiler

VS code c++ for macOS: compilation works but VS code detects errors

I followed the tutorial on this webpage: https://code.visualstudio.com/docs/cpp/config-clang-mac to be able to compile c++ programs using VS Code. After doing all things requested, I am able to compile and to debug the c++ file.
However, it seems that there is an issue. In the box "PROBLEMS", I have the two following errors:
expected ';' at end of declaration [9, 23]
range-based for loop is a C++11 extension [-Wc++11-extensions] [11,
29]
My code is exactly the same as the one reported in the VS code website. I also checked all *.json files and c++17 is the default compiler.
The tasks.json is as follows:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The c_cpp_properties.json is as follows:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
When compiling, everything is fine:
> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/stephane/Documents/c++/causality/run_mainfile.cpp -o /Users/stephane/Documents/c++/causality/run_mainfile <
Terminal will be reused by tasks, press any key to close it.
Here is the screenshot:
I am using macOS 10.15.6, VS Code 1.47.1, and C/C++ extension v0.29.
Any ideas?
#RangerBob The solution is simply what #brc-dd said but you don't have to create a new project/directory. Go to Preferences > Search > "c++ standard". Choose C++17, and VS Code will create a new file "settings.json" with that standard causing the 3 warnings to disappear.

Visual studio code SFML no such file or directory

Hello i have been trying to learn c++ SFML by using visual studo code.
After watching a tutorial how to install sfml in c++ i had everything set.
But, the problem is when i try to compile it gives me this error:
"main.cpp:2:10: fatal error: SFML/Graphics.hpp: No such file or directory".
I have been throught many guides but none of them appeared to be working.
Here is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(1280,720),"Nareszcie");
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type==sf::Event::Closed)
{
window.close();
}
window.clear();
}
}
return 0;
}
Hope someone will help me in solving this problem.
tasks.json:
{
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:/mingw32/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw32/bin"
}
},
{
"type": "shell",
"label": "cpp.exe build active file",
"command": "C:\\mingw32\\bin\\cpp.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw32\\bin"
}
}
],
"version": "2.0.0"
}
c_cpp_prperties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/SFML-2.5.1/include/**",
"C:/SFML-2.5.1"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/mingw32/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
You will need to add the include directory to your build instructions too.
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:/SFML-2.5.1/include/"
],
However, you will also need to link your executable to the SFML libraries.
"args": [
"-g",
"${file}",
"C:/path/to/sfml/libsfml-graphic.a" // something like that
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:/SFML-2.5.1/include/",
],
My recommendation would be to use a proper build system such as CMake, meson or others, which will do this kind of things automatically. Here's a CMake example:
cmake_minimum_required(VERSION 3.14)
project(your-project CXX)
# creates your executable with two cpp file in it
add_executable(my-exe main.cpp otherfile.cpp)
# find sfml. Assume the command line argument -DCMAKE_PREFIX_PATH="C:/SFML-2.5.1"
find_package(SFML 2.5.1 COMPONENTS graphic REQUIRED)
# configure your project correctly. Take care of include directories and linking
target_link_libraries(my-exe PUBLIC sfml-graphic)
VSCode also has a nice plugin for these build systems.

Is it possible to fix this in VScode? #pragma once in main file [-Wpragma-once-outside-header]

With VScode, how can this error be fixed?
#pragma once in main file [-Wpragma-once-outside-header]
Update:
Showing in VScode:
Update Again:
Here are my current VScode settings in c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Given that there's no answer, and given that I also had some hard hours trying to fix this, here it goes.
In Visual Studio Code the compile settings are built by default in tasks.json (Terminal > Configure Default Build Task > g++.exe ()). And in VS Code 2020 is this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
(I am using mingw-64 to support gcc compiler so maybe "command" and "cwd" have different paths depending on the compiler you are using.)
The key part is this: "${file}", which is the name of the active file (active tab) in your Editor.
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
If you are working with several files, one header(.h or .hpp) and one main(.cpp) files at least, VS Code will take that active file (.h or.hpp) as it were the main file (.cpp). So you need to change it with this: "${workspaceFolder}\*.cpp".
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
I fixed it by moving the .h/.hpp files to include folder.
Maybe there's a setting somewhere not to compile files in include.

How do i link the SFML libraries in Visual Studio Code?

I've been trying for hours and I can't seem to do it I've downloaded extensions and asked for help around but everything is just confusing me at this point.
I want to include the SFML libs in my project and I'm trying to use the the Visual Studio Code editor for it but it just won't comply for some reason.
A picture of what it currently looks like.
http://imgur.com/qJPlJua
I've been trying this for hours yesterday also but it just doesn't want to work.
I know the topic is a couple years old now but since I was searching for a way to link the sfml lib in vs code and I first ended up here, I thought I would share this git repo I found, which works pretty well for me so far:
https://github.com/andrew-r-king/sfml-vscode-boilerplate
I'm not using SFML 2.5.1 though, so I had to bring a small change in the c_cpp_properties.json file (I am on Ubuntu 18.04 and installed sfml through package manager)
here my c_cpp_properties.json file:
{
"configurations": [
{
"name": "Linux",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}/src",
"/usr/local/include/**",
"/usr/include/**"
],
"defines": [],
"cStandard": "c11",
"cppStandard": "c++17",
"forcedInclude": [
"${workspaceFolder}/src/PCH.hpp"
]
}
],
"version": 4
}
I know this question is about two years old, but after fiddling with my own tasks to solve this problem, and came up with something. This shouldn't be the best way to do it, but this should be good for anyone that finds this answer in the future.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"type": "shell",
"group": "build",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-IC:\\SFML-2.5.1\\include",
"-LC:\\SFML-2.5.1\\lib",
"-lsfml-graphics",
"-lsfml-window",
"-lsfml-system",
],
"problemMatcher": [
"$gcc"
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
//"showReuseMessage": true
}
}
This should work the same as the above answer. Hit CTRL+SHIFT+B to bring up the Task prompt, or look up Run task in the Command Palette (CTRL+SHIFT+P). Remember to have the .dlls of each library used in the root of the project.
Hope this helps.
I searched and I have found the solution.
In the tasks.json file, define two tasks :
"tasks": [
{
"taskName": "Compilation",
"isBuildCommand": true,
"args": ["-c", "${workspaceRoot}\\main.cpp", "-IC:\\SFML-2.4.0\\include"]
},
{
"taskName": "Liaison du fichier compilé aux bibliothèques SFML",
"args": ["${workspaceRoot}\\main.o", "-o", "sfml-app.exe", "-LC:\\SFML-2.4.0\\lib", "-lsfml-graphics", "-lsfml-window", "-lsfml-system"]
}
],
and add "suppressTaskName": true,
So it's like on Linux.
You compile with CTRL + SHIFT + B. To create the .exe file : CTRL+SHIFT+P --> then "run task" then click on the "Liaison du fichier compilé aux
bibliothèques SFML" task.
the entire file is as (for me):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "Compilation",
"isBuildCommand": true,
"args": ["-c", "${workspaceRoot}\\main.cpp", "-IC:\\SFML-2.4.0\\include"]
},
{
"taskName": "Liaison du fichier compilé aux bibliothèques SFML",
"args": ["${workspaceRoot}\\main.o", "-o", "sfml-app.exe", "-LC:\\SFML-2.4.0\\lib", "-lsfml-graphics", "-lsfml-window", "-lsfml-system"]
}
],
"showOutput": "always"
}
well there is nothing more to say, except all it's written on official web-site:
https://code.visualstudio.com/docs/cpp/config-linux
the only stuff I needed to do is to add additional library links for the compiler, which can be done in tasks.json part:
...
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lsfml-graphics",
"-lsfml-system",
"-lsfml-window"
],
...