add libraries to VSCODE (linux), dont understand JSON files - c++

I'm trying to run a cpp program that needs the matplotlibcpp.h header and some libraries. I copied this header file and save it into my cpp file folder.
My matplotlib and python libraries are here:
"/usr/include/eigen3",
"/usr/include/python3.8"`
My problem is that when I compile the program it returns that nor matplotlibcpp.h and python.h exist and as far as I have being searching in internet is because paths are not linked. I have seen many solutions where they tell to edit the c_pp_properties.json and task.json files but, there is no explanation about why should I change that, and I dont understand (mainly, because my json code is not the same as other post answers)
Please can someone tell me what I have to do to compile properly my code? And explain me how and why do I have to edit those json files? I really don't understand anything
this is my include part in my code:
#include <iostream>
#include <eigen3/Eigen/Dense>
#include <vector>
#include "LSPB.h"
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
using namespace std;
using namespace Eigen;`
my task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
// Ask msbuild to generate full paths for file names.
"${workspaceFolder}/**",
"/usr/include/eigen3",
"/usr/include/python3.8"
],
"group": {"kind": "build", "isDefault":true},
"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"
}
]
}
my_cpp_properties.json:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"-I",
"/usr/include/eigen3",
"/usr/include/python3.8"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
I just want to compile my program with no errors and learn how to use VSCODE when it comes to link libraries

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!

Visual Studio Code Intellisense doesn't show function documentation for C++

I followed this C/C++ for Visual Studio Code article from Microsoft to write C++ using Visual Studio Code. Unlike the article showing that intellisense provides documentation for member functions, it doesn't show me any. Same thing also happens in Visual Studio 2019 too. Here is a screenshot of how my intellisense looks.
What I tried to fix it so far:
Reinstall VSCode (deleted the app data as well)
Edit*
Configuration Files
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"intelliSenseMode": "windows-msvc-x64",
"cppStandard": "c++14"
}
],
"version": 4
}
settings.json
{
"files.associations": {
"xstring": "cpp"
}
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The documentation shown in the mouseover tooltip is generated from the standard library source files configured to be used. The screenshot from the VS Code docs is from a setup using gcc's c++ standard library implementation, libstdc++, which has c++ comments containing that documentation. Since you are using Visual Studio, you are using Microsoft's implementation of the c++ standard library. Browsing through their headers, it seems that the Microsoft STL doesn't include such documentation comments.
You could ask a question on their discussion board to ask if there are good workarounds/alternatives, or (politely) ask why they don't maintain such documentation comments, or if they have plans to in the future.
I haven't used this, but you may be interested to try this VS Code extension: Guyutongxue.cpp-reference, which
is a tool to browse cppreference.com from within vscode, instead of going to the browser to do so. You can use this extension to search for library and methods documentation of the C++ standard.

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!

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
}

Building a C++ Program in Visual Studio Code

To my knowledge, I have followed all of the steps to build a C++ program in Visual Studio Code on Windows 10. I have gcc-7.1.0-64 installed under C:/MinGW, the C/C++ extension installed in VS Code, and have configured a build task for my HelloWorld.cpp.
The issue:
When I try to build the program by opening the Command Palette and then typing Tasks: Run Build Task, It displays the error:
No Build Task found. Press 'Configure Build Task' to define one.
despite the fact that I have already done exactly that. I am probably missing something simple, but no tutorial or documentation I could find anywhere explains how to make this work. Any help is appreciated.
HelloWorld.cpp
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello world\n";
}
c_cpp_properties.json (irrelevant Mac/Linux setup omitted from snippet)
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/MinGW/include/c++/7.1.0/*"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"C:/MinGW/include/c++/7.1.0/*"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 2
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "HelloWorld.cpp",
"command": "c++",
"args": ["-g", "HelloWorld.cpp"],
"type": "shell"
}
]
}
Thanks to #RobLourens's comment for the answer.
The easiest way for tasks 2.0 is, run "Configure default build task", which will let you pick the build command, and set the group property for you.
I soon realized that it adds a line under the "group" section of the specified build task in tasks.json: (may be added manually to circumvent the formal method)
"isDefault": true,
Just add "isBuildCommand": true inside your task.