Visual studio code C++ on Linux Manjaro: IncludePath issue - c++

I am trying to configure my Visual Studio Code to developing C++ code on Linux Manjaro (last release), but I have a little bit of a problem.
Under the green line I had this description:
#include errors detected. Please update your includePath. IntelliSense features for this translation unit (/home/waski/myTest/myTest.cpp)
will be provided by the Tag Parser. cannot open source file "stddef.h"
(dependency of "iostream")
In c_cpp_properties.json file, section Linux, I have this config:
{
"name": "Linux",
"includePath": [
"/usr/include/c++/7.1.1",
"/usr/include/c++/7.1.1/x86_64-pc-linux-gnu",
"/usr/local/include",
"/usr/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include/c++/7.1.1",
"/usr/include/c++/7.1.1/x86_64-pc-linux-gnu",
"/usr/local/include",
"/usr/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
I also installed the c/c++ extension.
In my opinion, includePath is fully complex, I have no idea, which patch is required also.

I had exactly same problem today. Here's how I fixed it:
Find where on your system do you have stddef.h for example by running sudo find / -name stddef.h
Mine for example returns:
/usr/include/linux/stddef.h
/usr/lib/clang/4.0.1/include/stddef.h
/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/include/stddef.h
Pick any of these paths and add it to c_cpp_properties.json file, into includePath. You should be good to go then.

Your c_cpp_properties.json is missing the compilerPath attribute, so VSCode might be using the wrong compiler. To check, in the Command Palette (Ctrl+Shift+P), run "C/C++: Log Diagnostics". That will show the compiler path.
Also compare the output you see there to the output of:
$ touch empty.c
$ gcc -v -E -dD empty.c
At a minimum, you want the #include search paths to agree.
In this answer I have written up generic instructions on how to troubleshoot C++ compiler configuration in VSCode.

Related

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.

Can't manage to set up environment and path to libraries under Visual studio for c++

After having installed the MongoDB drivers for c++ following this tutorial, I wrote a little bit of code that I managed to compile and run with the command:
c++ --std=c++11 main.cpp -o app $(pkg-config --cflags --libs libmongocxx) && D_LIBRARY_PATH=/usr/local/lib ./app
Now, I try to debug and launch my code with Visual code. I don't use Neither Visual studio because I'm under Linux nor NetBeans because I don't have the proper JDK nor Code::Blocks because it is too complicated to set up with the proper environment.
My problem is that Visual Code can't manage to find the several #include I need to work with MongoDB and Bsoncxx:
I tried setting up a launch.json file with the parameter
"environment": [
{
"name": "LD_LIBRARY_PATH",
"value": "/usr/local/lib"
}
]
But I'm sure I'm doing totally wrong and I can't manage to find any solution over the internet. I could continue to write code then compile and run it via Batch files but an IDE is way more convenient.
To get Intellisense working you'll need to tell Vscode where your includes are. To do this, do the following:
Install the C/C++ extension for VSCode
Enter your include paths to in c_cpp_properties.json
Here is an example of the c_cpp_properties.json file I've used for a small project on WSL.
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/deps/fmt/include/",
"${workspaceFolder}/deps/spdlog/include/",
"${workspaceFolder}/deps/CLI11/include/",
"${workspaceFolder}/deps/pugixml/include/"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Follow this link for a full tutorial on how to set this up. How to set up VSCode
Pay special attention to the Configure the compiler path section and the Create a build task sections

Visual Studio Code: C/C++: Multiple definitions for a header/include file shown; how to fix?

I am using VSCode 1.37.1 on Windows with mingw64-5.4.0. I have followed the VSCode C++ configuration tutorial at https://code.visualstudio.com/docs/cpp/config-mingw but I have a problem resolving header file locations. In the code editor, if I put the cursor on "<iostream>", then press F12, I go to that file. But if I put the cursor on "<vector>" and press F12, I get a little pop-up showing three different possible resolutions. See screenshot below.
I have the option of manually choosing the correct file and opening it, but the next time I open the same file via F12 it asks again.
VSCode has already detected the correct include paths (see below), and the correct file only exists in one of them, so it should already have all of the required information.
My question is: how can I tell VSCode to automatically open the correct file, as determined by the include paths, rather than prompting me like this?
Screenshot:
By using the "C/C++: Log Diagnostics" palette command, I see that VSCode has detected the proper include paths (comparing to "g++ -v empty.c" at a command prompt). The complete diagnostics are:
-------- Diagnostics - 8/28/2019, 10:32:51 PM
Version: 0.25.0
Current Configuration:
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.15063.0",
"compilerPath": "D:/opt/mingw64-5.4.0/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"compilerArgs": [
"-m64"
],
"browse": {
"path": [
"${workspaceFolder}/**"
],
"limitSymbolsToIncludedHeaders": true
}
}
Translation Unit Mappings:
[D:\wrk\learn\vscode\cpphello\helloworld.cpp]:
D:\WRK\LEARN\VSCODE\CPPHELLO\HELLOWORLD.CPP
[D:\opt\mingw64-5.4.0\x86_64-w64-mingw32\include\c++\debug\vector]:
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE\C++\DEBUG\VECTOR
Translation Unit Configurations:
[D:\wrk\learn\vscode\cpphello\helloworld.cpp]:
Process ID: 9088
Memory Usage: 19 MB
Compiler Path: D:/opt/mingw64-5.4.0/bin/g++.exe
Includes:
D:\OPT\MINGW64-5.4.0\LIB\GCC\X86_64-W64-MINGW32\5.4.0\INCLUDE
D:\OPT\MINGW64-5.4.0\LIB\GCC\X86_64-W64-MINGW32\5.4.0\INCLUDE-FIXED
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE\C++
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE\C++\X86_64-W64-MINGW32
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE\C++\BACKWARD
Defines:
_DEBUG
UNICODE
_UNICODE
Standard Version: c++17
IntelliSense Mode: gcc-x64
Other Flags:
--g++
--gnu_version=50400
[D:\opt\mingw64-5.4.0\x86_64-w64-mingw32\include\c++\debug\vector]:
Process ID: 10728
Memory Usage: 33 MB
Compiler Path: D:/opt/mingw64-5.4.0/bin/g++.exe
Includes:
D:\OPT\MINGW64-5.4.0\LIB\GCC\X86_64-W64-MINGW32\5.4.0\INCLUDE
D:\OPT\MINGW64-5.4.0\LIB\GCC\X86_64-W64-MINGW32\5.4.0\INCLUDE-FIXED
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE\C++
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE\C++\X86_64-W64-MINGW32
D:\OPT\MINGW64-5.4.0\X86_64-W64-MINGW32\INCLUDE\C++\BACKWARD
Defines:
_DEBUG
UNICODE
_UNICODE
Standard Version: c++17
IntelliSense Mode: gcc-x64
Other Flags:
--g++
--gnu_version=50400
--header_only_fallback
Total Memory Usage: 52 MB
I filed CppTools Issue 4186: Multiple definitions shown for header/include file but only one is on includePath for this, which was closed as a duplicate of Issue 2564: Go to Definition on a #include path doesn't use the IntelliSense info.
So it appears this is simply a known bug with no known workaround.

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.

Setup VSCode for C++ on Windows (MSVC)

I'm a little befuddled that I'm not able to setup Visual Studio Code to do C++ development on Windows using MSVC. All over the web people say how pleased they are with how easy everything is to set up and use, but I don't find any straightforward guides; most of them just skip the setup part and show how nice everything works including code completion/intellisense and debugging support.
I have installed Visual Studio 2015 Community Edition (including the debugging tools etc.), Visual Studio Code and the C++ extension by Microsoft.
What do I need to do next?
Edit:
Intellisense works out of the box these days, that's great. But my auto-generated tasks.json doesn't seem to do the trick for building, here's what it looks like:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "build",
"type": "process",
"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": "always"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
When I run this task it seems to run infinitely and only outputs to the following:
Executing task: msbuild /property:GenerateFullPaths=true /t:build <
Any ideas?
For MSVC 2017 version:
add these to your include path:
"D:/Program Files/Microsoft/MSVC2017/VC/Tools/MSVC/14.12.25827/include/*",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.10240.0/ucrt",
"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.10240.0/ucrt/x64",
You may change the drive name and folder name accordingly.
2.change your shell setting in settings.json:
"terminal.integrated.shell.windows": "C:\WINDOWS\System32\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/k", "D:/Program Files/Microsoft/MSVC2017/Common7/Tools/VsDevCmd.bat"]
which will enable the cl command in your integrated terminal.
3.Press ctrl+` to enable integrated terminal, type cl /EHsc your_cpp_program.cpp to compile.
I think easiest way to get it - open visual studio 2017 command prompt console and run Visual Studio Code IDE from there so it will pick all necessary compiler environment variables