What is the command to configure Intellisense in VSCode for C++? - c++

I have a C++ project, which builds and runs fine on Ubuntu 22.04.1 with VSCode 1.74.3 and gcc 12.1.0. The editor shows a number of squiggly lines, though:
How can I make Intellisense correctly parse my source file? When I open a command palette and search for "intellisense", I'm getting only these options:
none of which helps. There was a popup asking about configuring Intellisense for this project, which I disabled, because it prevented the project from building. How can I get "configure Intellisense" option?

Related

VS Code C++ debugging

I try to debug my C++ programs but it shows me this error:
Could not find the task 'g++ build active file'.
I tried adding a configuration as Visual Studio Code says in its website, by choosing C/C++: (gdb) Attach, but nothing changes.
I struggled for days to setup a running and debugging environment for C++ in VS Code. Finally I found a way to solve the problem. Please follow the below steps
Prerequisites- Have Mingw32 installed on your PC.
Install C++ extension for VS code
Install Competitive programming helper extension for VS code. Something like
this will appear on the left panel.
Open your .cpp file and open the test runner by clicking on the highlighted are in the above image. Enter the inputs and click on the runall button in the above image you will be able to see the output.
For debugging :-
1.Click on the debug button in the below image . You would get an option stating “Run and Debug file”. Click on that.
It would create a launch.json file and task.json file. Delete the task.json file as we are using competitive programming helper extension for running our cpp files.
Below is the content of launch.json to setup the debugger environment
Paste the same exact configuration mentioned above in your launch.json file.
For the “miDebuggerPath”, please provide the location of the gcc.exe file inside mingw32 folder on your pc.
In the args key I have used "args": ["<", "${fileDirname}\input.txt",">", "${fileDirname}\output.txt"],
The above code is use to take input from input.txt file and write output to output.txt file while debugging for the easy input and output. Please create a input.txt and output.txt in the same folder where you have all your C++ files.
Add a breakpoint in your .cpp file and run the debugger by click on the green RUN button , your debugger would start and it would take all the input from input.txt and write the output in the output.txt after the debugging session ends.
Happy Coding.
Unfortunately, cannot add a comment, so I have to submit an "answer", which is merely a guess. Did you download "mingw64" with g++? Probably, you did not add mingw64/bin to your system path. Control panel -> Edit path -> Environment variables -> Path -> New.. (it is also in the tutorial link below)
To really make sure the things run, check this tutorial, by Microsoft: https://code.visualstudio.com/docs/cpp/config-mingw
Perhaps, uninstall your downloaded g++ and install this mingw64 repository. Quite helpful. I use it myself and for a quick debugging - works like clocks!

Visual Studio Code Running Environment

I'm mostly using VSCode. I have two questions:
In the case of having a compiler error, VSCode warns you while you are writing the code (before you compile/run your program.) How I can do this?
How I can have the environment so that when I click on run button it compiles, runs and shows me the output in the terminal section?
Maybe this extension could be of help: C/C++ Advanced Lint
Install Microsoft's C/C++ extension and have a look at the documentation. Especially the topic "Create a build task" should be of interest for you (after you've done all the prerequisites steps.

Setup C++ on Visual Studio Code [duplicate]

I am programming in C in Visual Studio Code, but I can't compile, as VSC only offers three compilers built in - Node.js, C# Mono, and Extension development. After a little bit of digging I came across the Visual Studio Marketplace. This seemed like the right sort of thing, but only four uncommon languages were there.
I can only assume that C debugging support is built in, I just can't find it or I am going the wrong way about doing it. I attempted to create a new launch.json (the manifest that seems to hold the compiling/debugging settings for each file) and manually entering the GCC binaries that I have, but that didn't end up working. I'm currently stuck manually compiling the C source file I am working on through command prompt.
Would really help if someone could point me in the right direction on what to do.
tl;dr - Help from anyone debugging C in Visual Studio Code
Windows 8, if that matters
Cheers!
Caution
A friendly reminder: The following tutorial is for Linux user instead of Windows
Tutorial
If you want to debug your c++ code with GDB
You can read this ( Debugging your code ) article from Visual Studio Code official website.
Step 1: Compilation
You need to set up task.json for compilation of your cpp file
or simply type in the following command in the command window
g++ -g file.cpp -o file.exe
to generate a debuggable .exe file
Step 2: Set up the launch.json file
To enable debugging, you will need to generate a launch.json file
follow the launch.json example or google others
Step 3: Press (Ctrl+F5) to start compiling
this launch.json file will launch the configuration when you press the shortcut (Ctrl+F5)
Enjoy it!
ps. For those who want to set up tasks.json, you can read this from vscode official (-> TypeScript Hello World)
Press Ctrl + Shift + P to pull up the Command Pallette, and Type ext install cpptools. It will install everything you need to debug C and C++.
Debugging in VS code is very complete, but if you just need to compile and run:
https://code.visualstudio.com/docs/languages/cpp
Look in the debugging section, and it will explain everything.
There is a much easier way to compile and run C code using GCC, no configuration needed:
Install the Code Runner Extension
Open your C code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.
Moreover you could update the config in settings.json using different C compilers as you want, the default config for C is as below:
"code-runner.executorMap": {
"c": "gcc $fullFileName && ./a.out"
}
For Windows:
Install MinGW or Dev C++
Open Environment Variables
In System Variable select Path -> Edit -> New
Copy this C:\Program Files (x86)\Dev-Cpp\MinGW64\bin to the New window.
(If you have MinGW installed copy its /bin path).
To check if you have added it successfully: Open CMD -> Type "gcc" and it should return:
gcc: fatal error: no input files compilation terminated.
Install C/C++ for Visual Studio Code && C/C++ Compile Run || Code Runner
If you installed only C/C++ Compile Run extension you can compile your program using F6/F7
If you installed the second extension you can compile your program using the button in the top bar.
Screenshot: Hello World compiled in VS Code
Just wanted to add that if you want to debug stuff, you should compile with debug information before you debug, otherwise the debugger won't work. So, in g++ you need to do g++ -g source.cpp. The -g flag means that the compiler will insert debugging information into your executable, so that you can run gdb on it.
You need to install C compiler, C/C++ extension, configure launch.json and tasks.json to be able to debug C code.
This article would guide you how to do it: https://medium.com/#jerrygoyal/run-debug-intellisense-c-c-in-vscode-within-5-minutes-3ed956e059d6
EDIT: As of ~March 2016, Microsoft offers a C/C++ extension for Visual Studio Code and therefor the answer I originally gave is no longer valid.
Visual Studio Code doesn't support C/C++ very well. As such it doesn't >naturally support gcc or gdb within the Visual Studio Code application.
The most it will do is syntax highlighting, the advanced features like >intellisense aren't supported with C. You can still compile and debug code >that you wrote in VSC, but you'll need to do that outside the program itself.

Running the C++ app from the Eclipse IDE

I am using eclipse IDE. I have here some basic source code including header file. Have been using cygwin as C++ compiler. It compiles my app and it does not produce any error. But I cannot see the output on IDE's console. When I run the executable file on its own it produces output. How to configure Eclipse IDE in order to redirect output to the IDE's console?
This is all I get:
15:40:11 **** Incremental Build of configuration Debug for project GetCompiled ****
make all
Building target: GetCompiled.exe
Invoking: Cygwin C++ Linker
g++ -o "GetCompiled.exe" ./Compiling.o
cygwin warning:
MS-DOS style path detected: D:\Aristotelis\C++\GetCompiled\Debug
Preferred POSIX equivalent is: /cygdrive/d/Aristotelis/C++/GetCompiled/Debug
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Finished building target: GetCompiled.exe
15:40:12 Build Finished (took 702ms)
Selected app to run:
I tried to run the exe file from the IDE by selecting to run as an C++ app, but it does not produce any output.
Best regards
Eclipse/CDT does not automatically run your application after each build. You need to either go trough the menu Run->Open Run->Run or use the keyboard shortcut Crtl+F11, and then check the Console window again.
An advanced feature is to set your application as a post-build event. For that, go to your Build Settings and find the appropriate submenu and type the name of your application.exe there (haven't got Eclipse at my fingertips right now). With this setting, the application will be launched automatically after each successful build. This comes in handy if you do automated unit testing.

Eclipse c++ never run when build has errors

When programming in eclipse (c++), I would like to just hit f5 (run) and have the project I'm working on save, build and (if there are no errors) run. If there are errors I want it to show the problems window and stop.
This all works at the moment except for the part where it shouldn't run when there are errors.
Is there a way to make eclipse never run the project when there are errors? Perhaps with an addon?
EDIT: Forgot to mention the prompt... The prompt does show but I want it to not show at all. If you look at the preference window you'll see that there's no 'never' option, there is one for all the other options but not for the 'launch if project contains errors'.
By default, Eclipse CDT does not run code with build errors, but maybe you have checked the option Always launch without asking checkbox.
You should go to Windows menu->Preferences->Run/Debug->Launching->Continue launch if project contains errors and check Prompt option instead of Always. Using this, Eclipse CDT will prompt you if errors exists during building or launch your binary if it has been compiled without errors.