VS Code doesn't find my #include files - tried all possible ways - c++

I know that this issue has been raised several times, but even trying all possible suggestions I could find on the Internet, I couldn't find a way to make my simple program work.
Here is the story: I am starting a C++ program with Visual Studio Code, and I want to use the openCV libraries. Since I'm a beginner at these things, I started by cutting & pasting some simple program from an opencv tutorial. When I try to build, VSC doesn't find the openCV files and throw an error.
C:\Users\Roberto\Documents\Program Data Files\C++\SVM\Test1.cpp:5:10:
fatal error: opencv2/core.hpp: No such file or directory #include
<opencv2/core.hpp>
The program starts with these #include (none of the opencv2 files is found):
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
This is my launch.json:
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"MIMode": "gdb",
"miDebuggerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gdb.exe",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
]
This is c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:/Users/Roberto/Documents/Program Data Files/C++/opencv/build/include/*",
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x86"
}
],
"version": 4
}
The idea is that the openCV files are in the directory in the includePath.
I have read in several places that I shouldn't use includePath but only compilePath. Now, I'm not sure what it means, but I also tried to copy the entire folder of the openCV include files in the coplier directory, but with no success.
A couple of notes. Intellisense "finds" the files, because if I start typing "#include <op..." it immediately suggests me the opencv2 folder, and after that the core.hpp etc... And, of course, the files ARE in the right directory.
I also tried to bypass this problem by adding the -I instruction in the task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-IC:/Users/Roberto/Documents/Program Data Files/C++/opencv/build/include"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In this case, I get a different error, i.e., all openCV functions calls are flagged as "undefined reference to cv::...'
Any suggestion on how to make this thing work?

After many trials, I came up with some explanation and solution, in case anybody else has the same problem.
Recap: my problems where:
find the header files listed as #include
link the proper opencv library
For 1), it seems that using the "includePath" setting available in the c_cpp_properties.json DOES NOT do what anyone would logically expect (in fact, I don't know if it is used at all). The only way I found was to specify an Include path is to explicitly use the argument "/I" in the tasks.json
For 2), first I found somewhere someone claiming that VS Code has to use the Microsoft C++ compiler, and is not compatible with mingw. I am pretty sure it is not the case, but I decided nevertheless to switch to Microsoft's cl.exe
Second, it seems that to make VS Code "find" the compiler you need to have some specific environmental variable set. I didn't found out exactly which variable, but the trick is to:
a) launch the "x64 Native Tool Command Prompt for VS2019". This opens a cmd window and executes a number of variable settings and other stuff. This file can be obtained from the download page (https://visualstudio.microsoft.com/downloads/) of Visual Studio, going to the bottom to "Tools for Visual Studio 2019" and then "Build Tools for Visual Studio 2019"
b) from this command prompt, navigate to your working directory, and from there launch the instruction "code .", which, assuming the path to your VS Code installation is in the PATH variable, will launch VS Code
c) if I instead launch VS Code directly from window, it won't work (doesn't find cl.exe)
Third, there are different cl.exe, depending on whether one is compiling from a 32 or 64 bit platform onto a 32 or 64 bit target ... I wasted some tie figuring out this, and the key is using the correct "x64 Native Tool Command Prompt for VS2019"
Fourth, the opencv library has to just be listed in tasks.json preferably AFTER all other arguments (if I remember correctly, I got a mistake earlier because I called it before the actual file being compiled)
It is perfectly possible that alle these problems above were only specific to my PC, or to the fact that I am yet a very beginner with this stuff ... but if not, glad if my experience might of any help

"undefine reference to cv ..." it means that the linker could not find the functions you are calling in your code. Make sure you link the libraries properly.

Related

Problems getting OpenGL to work in VSCode on Mac

I am trying to complie a basic openGL program in VSCode on mac. I am using glad and GLFW, and I have the Glad files in the same folder as the test.cpp file I am trying to run. However, the include statement throws an error no matter how I type it. This is the program I am writing
#include <iostream>
#include "glad.h"
However, it throws the errors:
screenshot of errors
VSCode cannot find the header files the program needs to run, and the compiler throws errors as a result. I should add, that autocomplete for these libraries is working, however, the file cannot be found by the editor.
Here is the c_cpp_properties.json code
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
Perhaps the problem could be realted to how I am trying to compile the file? Here is my tasks.json file
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Any guidance on how to set this project up correctly, any advice would be greatly appreciated. For reference, here is the tutorial I am trying to follow: https://learnopengl.com/Getting-started/Hello-Window.
Thank you so much!
I'll start by pointing out a few things that aren't really important.
VSCode is not a compiler, nor an IDE. VScode is a text editor. As a text editor it may have some extensions that help you with developing (thus turning it into a make-shift IDE).
What is happening here is that the compiler (gcc) doesn't know where to look for include files. It is even hinting you that you need to update the includePath in a way that you point it to the correct folder with the header files. In the CLI this is done via the -I flag i.e. g++ main.cpp -Ipath/to/your/include_folder/.
If you look into the Getting Started section they even go through these steps and go as far as to suggest the usage of CMake (which is a build tool that helps you generate the makefile with which you build your binaries).
By looking at the tasks.json I can conclude that the extension which you are using in VSCode uses this file to pass down the correct arguments to g++. So the quickest solution for you right now is to specify another element in args that points to the header files i.e.:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"-Ipath/to/your/include_folder/with_the_headers", <----
"${fileDirname}/${fileBasenameNoExtension}",
],
Hope it helps!

VSCode not recognizing includes from includepath

I am having an issue where VSCode will recognize my include of zipper.h and then out of nowhere flip on me and tell me that there is no such file or directory. I am not sure if this is an issue with my code or includes or vs code.
https://i.gyazo.com/2d35a31abf83546d8633d991bcb4752a.png
https://i.gyazo.com/96ad7825e8d1c390035a4db2f789bbcf.png
I have tried adding it both to my include path and windows environment path. it keeps failing for the same reason. I am very confused on what I'm doing wrong. Is it not recognizing those links? Should I be linking the libraries through g++ when compiling?
#include <zipper.h>
void zipFolder()
{
zipper::Zipper zipFile("logs.zip");
zipFile.add("C:\\Cycling");
zipFile.close();
}
int main(int argc, char const *argv[])
{
return 0;
}
c:\Users\Desk\Desktop\Code\Cycling>cd "c:\Users\Desk\Desktop\Code\Cycling\" && g++ test.cpp -o test && "c:\Users\Desk\Desktop\Code\Cycling\"test
test.cpp:1:10: fatal error: zipper.h: No such file or directory
#include <zipper.h>
^~~~~~~~~~
compilation terminated.
"includePath" property both in c_cpp_properties.json and settings.json relates only to the internal editor's IntelliSense feature and has nothing to do with compilation.
In order to tell the compiler the necessary include paths, you need to specify a correspondent compiler option in your build task (in tasks.json), namely "-Ipath/to/my/include/files".
Here is a build task example from my tasks.json file (look at "args" property - it contains compiler option "-I${workspaceFolder}/../..", i.e. two levels up from the current directory):
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file ver(1)",
"command": "/usr/bin/g++-9",
"args": [
"-std=c++17",
"-I${workspaceFolder}/../..",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++-9"
}
]
}
You did not tell your compiler anything about a file called Zipper.h or where it is loacted, or anything related to it. "g++ test.cpp -o test" just tells the compiler to compile a source file called test.cpp and link it. You have to understand that Visual Studio Code is not an IDE and can't compile by itself. You should have an file called c_cpp_properties.json file located in your .vscode directory. The one that i use for example looks like this and is configured for mingw64.
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/Source/**"
],
"compilerPath": "C:\\mingw-w64\\mingw64\\bin\\gcc.exe",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
This tells Visual Studio Code where your source files and libraries are. This is what is used for IntelliSense (Syntax Highlights, Error Squiggles, Code Completion, etc). However this has absolutly nothing to do with building your project. Your compiler doesn't now know about the include path's you set in Visual Studio Code. So to compile your project you have to tell your compiler everything he needs to know. Visual Studio Code simply executes what you specify in the task. It's the same as going to that directory and type in the same thing in your command promt. So i recommend you to read up on how to compile a c++ project with g++, your problem is not related to Visual Studio Code at all. If youre planning on doing something thats a bit bigger than just a single source file i strongly suggest you to learn CMake. Compiling by manually calling gcc get's really complicated once you have more source files and includes / libraries to link. Once you have set up your Cmake you can just specify a task in Visual Studio Code similar to this one to build your project:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "cmake --build Build",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I also recommend you to read this:
https://code.visualstudio.com/docs/cpp/config-mingw
This is a really good explanation of basicly exactly what you are trying to do by Microsoft and helped me understanding this when i started to use Visual Studio Code for my c++ work.
Visual Studio Code not changes build command itself, even if includePath changes. You should change build command yourself in .vscode/tasks.json. See this tutorial.

Visual Studio Code C++: unordered_map not found

I was given some C++ files that I need to compile. I'm using Visual Studio Code with the C/C++ and Code Runner extensions on Windows 10. With the following "include" statements:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <unordered_map>
I get the following error:
unordered_map: No such file or directory
I am very new to C++, and haven't been able to find a solution to this problem. I've updated the "includePath" in my c_cpp_properties.json file as follows. I have also tried compiling with Cygwin and Visual Studio Community, but I get the same error. I know the unordered_map .hpp file exists, but the compiler doesn't seem to be finding it.
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17134.0",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
If it's relevant, this is what my tasks.json file looks like:
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
Are my .json files configured properly? I apologize if I'm missing something basic; I've done a lot of searching on how to compile C++ on Windows, and haven't had any success. Thank you in advance for any help.
EDIT:
Here is the full file I'm trying to compile. The executable is meant to be called by a python script.
https://github.com/jorpjomp/sierra-hotel/blob/master/location_routing.cpp
Unordered map is not supported in VS Code by default Microsoft ms-vscode.cpptools. Follow these steps to get over the problem:
Download MinGW from ( https://sourceforge.net/projects/mingw/ ). MinGW is a native Windows port of the GNU Compiler Collection (GCC), with freely distributable import libraries and header files for building native Windows applications.
Mark all the packages for installation.
ss to mark all the packages for installation
Click on the Apply Changes option under the Installation tab
ss of where to click on apply changes
Now, the Environment Variable’s Path is to be updated. Go to Advanced System Settings->Environment Variables.
Edit Path in System Variables Tab.
ss of how to edit Path
Copy the path of the bin folder of MinGW. By default, the path is: C:\MinGW\bin
Paste this new path in the list and click OK.
ss after pasting the bin path into the list
Run the C++ code in VS Code. It will work fine.
ss of vs code working fine at last
You are using msbuild at the moment to build your project. Is this intentional? If you just have "some C++ files" you want to compile, msbuild is an overkill, compile the source directly by either using Mingw's g++ or the Microsoft CL.exe compiler.
So I recommend:
1) Go to http://mingw-w64.org/doku.php/download, download and install mingw and add the path to g++ into your PATH environment variable.
2) In Visual Studio Code create a task.json with the following content:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json (assuming you store mingw here: C:\mingw\mingw64\bin):
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw/mingw64/bin",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}

How to enable C++17 support in VSCode C++ Extension

I keep on getting error squiggles on std::string_view, but I am able to build just fine. Is there a way to tell intellisense or the C++ linter to use C++17?
The specific error I get is:
namespace "std" has no member "string_view"
This has become much easier now. Search for cppstandard in your vs code extension settings and choose the version of C++ you want the extension to use from the drop down.
In order to make sure your debugger is using the same version, make sure you have something like this for your tasks.json, where the important lines are the --std and the line after that defines the version.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++17",
"-I",
"${fileDirname}",
"-g",
"${fileDirname}/*.cpp",
"-o",
"${workspaceFolder}/out/${fileBasenameNoExtension}.o"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
Note that if you're copying the above tasks.json directly, you'll need to have a folder named out in your workspace root.
There's a posting in their GitHub issue tracker about this: std::string_view intellisense missing (CMake, VC++ 2017).
In another issue, it is said that the extension defaults to C++17, but does not yet support all of C++17 features: Setting C++ standard.
This is confirmed by c_cpp_properties.json Reference Guide, where an option is listed cppStandard which defaults to C++17. (To edit this file, press Ctrl + Shift + P and type in C/CPP: Edit Configurations).
It appears, then, they just don't have full support yet.
Just an updated. I got this issue as well.
I solve it by adding c_cpp_properties.json
Ctrl + Shift + P then select C/C++:Edit Configurations (JSON)
Adjust the content for cStandard and cppStandard:
"cStandard": "gnu17",
"cppStandard": "gnu++17",
For people trying out on Linux and having GCC 7.5.0 installed, this worked for me.
Do these two steps to enable the linter to acknowledge the c++17 writings and for the compiler to pick up the c++17.
Open up the C/C++:Edit Configurations (JSON), and change the default values for these two fields to:
"cStandard": "gnu18", "cppStandard": "gnu++17",
Open up the tasks.json file inside .vscode directory and add the following statements to the args key:
"--std", "c++17"
If you're unable to enable even after trying the solutions by #Marc.2377 and #W Kenny, do the following
Open tasks.json in the .vscode folder
Add "--std","c++17" under "args:"
Save tasks.json
After trying many things I have found probably a solution for people using CMake and willing to edit the CMakeLists.txt file.
I just put the following line at the beginning of my CMakeLists.txt
set (CMAKE_CXX_STANDARD 17)
You can check your c++ version by doing:
cout << __cplusplus ;
and the 3rd and 4th number gives you the version of c++ you are using.
For example:
cout << __cplusplus ;
201703
means you are using c++ 17
and
cout << __cplusplus ;
201402
means you are using c++ 14
I think that there must be an easier solution but I could not find it yet.
Additional to set cppStandard to gnu++17 in c_cpp_properties.json mentioned in other posts, you need to change the __cplusplus-define to the corresponding value (e.g. 201703L)
Like that:
{
"version": 4,
"configurations": [
{
// ...
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"defines": [
// ...
"__cplusplus=201703L"
// ...
]
}
]
}
Check your version of g++ using g++ --version on the command line. If it is like version 6 or 7 then you need to update to a newer version with mingw. I used mysys2 to do this and now I do not have the same problem.

How do I install PDCurses in Windows for use with C++?

I want to use it in some of my programs instead of the standard IOStream.
Also, does NCurses work on Windows, and if so, any better?
Download the zip file, unpack it wherever you typically put external libraries, and check the readme, which tells you the following:
PDCurses has been ported to DOS, OS/2, Win32, X11 and SDL. A directory containing the port-specific source files exists for each of these platforms. Build instructions are in the README file for each platform.
The readme file in the Win32 directory tells you that there are makefiles for several different compilers. In short, you run make:
make -f makefilename
It tells mentions a couple of options you can set, including WIDE and UTF8.
To then use the library, add the directory that contains curses.h to your include path and link with the pdcurses.lib file that make generates for you. How you modify your include path and your linked libraries depends on your development environment and is largely irrelevant to PDCurses.
On VSCode
[Step 1] Install MinGW :
MinGW installation steps
^(make sure you followed the steps carefully)
[Step 2] BUILD PDCurses:
Download PDCurses-master.zip and extract the content
Open\Run MSYS2 MinGW 64-bit (or MSYS2 MinGW 32-bit^1)
cd into the wincon folder and run make -f Makefile WIDE=Y DLL=Y source
[Step 3] Copy Files:
If you followed the steps above so far correctly, there should be 2 specific files inside wincon folder called pdcurses.a and pdcurses.dll
rename pdcurses.a to libpdcurses.a
copy pdcurses.dll into C:\msys64\mingw64\bin
copy libpdcurses.a into C:\msys64\mingw64\lib
copy curses.h and panel.h form PDCurses-master folder into C:\msys64\mingw64\include
[Step 4] Build an example:
Install the C/C++ extension
Follow those steps to create a working enviroment inside VSCode
Add "-lpdcurses" under "args": into tasks.json
and you are Done (at least those steps worked for me)
Extra
you can also just manually build an example by runing g++ your_example.c -o your_example -lpdcurses inside MSYS2 MinGW 64-bit terminal if you want so [...]
^1 if you want to build for 32 systems a good rule is to follow all steps above but wherever you see 64 replace it with 32
demos / examples
how things should look like:
c_cpp_properties.json
{
"configurations": [
{
"name": "Win64",
"includePath": [
"${default}"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lpdcurses"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
I finally made it. First Build/Compile the Source according to docs.
make -f Makefile # did for me, Windows 10
Copy curses.h and panel.h into your include folder. And, Copy wincon/pdcurses.a into your lib folder. Rename pdcurses.a to libpdcurses.a. (Because it's the standard).
Now, You can include curses.h and compile it like this.
g++ main.cpp -lpdcurses