VSCode not recognizing includes from includepath - c++

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.

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!

Error during build of cpp file in VSC: "Errors exist after running preLaunchTask 'C/C++:cpp.exe build active file"

I am unable to run my code and execute the build of the .exe file in the cpp language in Visual Studio Code.
I have downloaded and installed GCC, MinGW software, MinGW-w64 GCC, etc. according to the instructions here and have successfully verified that gcc g++ and gdb are all installed (by checking 'gcc --version' etc in the commad prompt). My intent is to use this compiler to compile my code but it seems I cannot find the correct compiler under the options listed here to simply 'build and debug the active file'.
I also (and perhaps consequentially) have run into a problem with launch.json not being able to build the executable file.
Can someone please help me with this? This is incredibly frustrating and I simply want to be able to run my code. I downloaded MinGW along with gcc is a good compiler for this very purpose.
Image of error message 1: here, to which I click 'debug anyway' and get the next error:
Image of error message 2: here.
Image of launch.json with the configuration that I thought would be appropriate (I found this from another source online): here.
Thank you!!
I recommend deleting all the configurations under the .vscode directory, as they can be regenerated again. After deletion, follow the steps:
Go to your program file, and press F5 to launch debug.
Select g++.exe build and debug active file or a convenient option.
A tasks.json under the .vscode will be generated with the appropriate instructions and the program will be executed instantly.
For example, in my case, it was this:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\TDM-GCC-64\\bin\\g++.exe",
"args": [ // ----- given by me in C/C++ extension settings
"-fdiagnostics-color=always",
"-Wall",
"-O3",
"-pedantic",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Run C++ in Visual Studio Code without opening it with `code .` from CMD

I set up VSCode to compile C++, but in order to be able to run it, I first have to open CMD, navigate to the location of the .cpp file I want to open and then run code . (This opens VSCode and then I can compile the file with Ctrl+Shift+B.) This is tedious and it would be wonderful if I had a script that enabled me to run C++ without having to do the above procedure every time.
Thank you for your help. :)
EDIT
This is my tasks.json file:
{
"version": "2.0.0",
"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"
],
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
For your particular case with a single .cpp file and g++ installed, you should be able to compile from the command line using a command similar to below:
g++ -fdiagnostics-color=always -g <.cpp file name> -o <output binary name>
Make sure to replace <.cpp file name> with your actual filename and <output binary name> with whatever you want to name your executable.
It appears you are first starting to learn how to develop in C++. This solution above should work for now.
When you start to write bigger programs and decide to split your source code into multiple files, you will eventually want to learn how to use a build system that can help automate the compilation of multiple source files. A popular build system is GNU Make, this would probably be a good tool to learn. You can write makefiles which instruct how to build the code, and then VSCode can be configured to use make to read the makefiles and build the code.

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

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.

I have a build error with MinGW and VS Code "g++ not recognized as a cmdlet..."

I am trying to set up build and run a c++ file in VS Code 2019. I am having build errors after editing the tasks.json file. The environment variable is set to g++ as it should be. So far, I have been following this tutorial.
I attempted changing "command" to"C:\MinGW\bin\g++.exe" as recommended in a question thread on GitHub. However, because my c++ file is not in this file path, the program was not able to find it when I built the code. This is what the "command" portion of the tasks.json file should look like:
"label": "build calculator adventure",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"Calculator-Adventure",
"Calculator Adventure.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
The "Calculator-Adventure" part is my filename. The expected output is for the code to build and create a .exe file for my code, as stated in the tutorial and said in the VS Code Docs.
However, it currently outputs the following into the terminal:
> Executing task: ‪‪g++ -g Calculator Adventure.cpp -o Calculator-Adventure <
g++ : The term 'g++' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The terminal process terminated with exit code: 1"
OK, I finally figured it out. What worked for me was adding the file path to the git bash shell (C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git) to the System Environment Variables in the Control Panel (how to do that here). Make sure you also have the file path to the MinGW bin folder added to the Environment Variables as well (32bit installer: C:\MinGW\bin) (64bit installer: C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin) Then, restart VS Code and build (Ctrl+Shift+B) again.
Here's my final code for the .json files:
c_cpp_properties.json:
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build calculator adventure",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"Calculator-Adventure",
"Calculator Adventure.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
For more information, check out this page. It is a really detailed step-by-step guide for using the MinGW compiler for C++ in VS Code (read it carefully). If you have any other trouble, take a look at this tutorial (same tutorial linked in the question). Hope this helps!
Note: in the docs page I linked, they use the 64bit version of MinGW. It should still work with the 32bit version though. Thanks, to #drescherjm for posting the VS Code Docs!