Configure VS Code code runner extension to display CPP warnings - c++

I am wondering how I could configure the code runner extension on VS Code (MacOS 10.15) to display warnings (i.e. -Wall?)
Would there be a simple, almost permanent method so code runner will always display warnings no matter which CPP file I build?

Yes! This is possible.
Go to the Workspace Settings, search for code runner and click on Executor Map:
You may then use the following JSON code:
{
"code-runner.executorMap": {
// ...
"c": "cd $dir && gcc $fileName -Wall -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
} // used -Wall here
}
Save it and quit from the file and jump to the code you want to run. Press Ctrl+Alt+J to select the run type. Select C simply and you're good to go.

Related

How to run C++ program from terminal VS Code

I want to run a C++ program in VS Code. All I get from Google is that click on run and debug (the play button) on top right in VS Code and my program will be up and running. I don't want to do from that. I want to do it from terminal.
Example, to run:
A Python file I do: python3 fileName.py
A Flutter program: flutter run
A Java file: javac fileName.java
A Go file: go run fileName.go
Is there any command similar like this in C++?
Apologies, I know my question is a little naïve.
i guess the short answer would be :
$ g++ -o < name-you-want-to-give> source.cpp
In place of replace it by any name like myprogram, etc.
./myprogram
This mean you had to install gcc compiler beforehand.
I need to be in my project directory and then i need to run
g++ 01inputFromUser.cpp -o 01inputFromUser && "/home/aman/Desktop/arjun/cpp/"01inputFromUser
so this was what I was looking for
g++ fileName.cpp -o fileName && "/path/to/project/"fileName

VSCode ignores cpp standard when folder is opened in WSL: Ubuntu?

When I open a folder with WSL: Ubuntu and then try to build the project it seems to ignore the cpp standard set in VSCode's User settings. I can choose the standard I want by configuring a task.json file however I want to know how to change the default cpp standard for any folder opened in VSCode with WSL: Ubuntu. I have tried adding the following in my User settings in VSCode.
...
"C_Cpp.default.cppStandard": "c++17",
...
"code-runner.executorMap": {
...
"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
...
},
And yet when I build the following and execute the output I get 201402 indicating it is using c++14.
#include <iostream>
int main()
{
std::cout << __cplusplus << std::endl;
return 0;
}
Any suggestions on how to change the default c++ standard?
It seems that if I open the file in windows then it works fine as well so I don't know what the difference is here?

G++ Compiler warning when using c++ 17 updates

I'm running g++ compiler on windows 10 with mingw.
On checking compiler version in cmd I get the following:
g++ --version- g++ (MinGW.org GCC Build-2) 9.2.0
same with c++ --version
When I compiled a cpp program making use of structured bindings I got a warning:
warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
But otherwise the code ran fine.
Does everyone get this warning or am I running a lower version compiler?
I'm using an extension - Competitive programming helper, and this warning interrupts the process.
Hence, if it is the case that everyone gets this warning, is there a way I can block version specific warnings only without having to block all compiler warnings.
TIA.
As the warning itself says:
structured bindings only available with '-std=c++17' or '-std=gnu++17'.
Meaning, you are using cpp version 14 or lower as compiler. However, you also have cpp 17 or higher installed on your system to get such kind of warning.
Your program will still run but with the warning.
For Windows:
If you are using VS code (assuming that you are already using code runner) then,
Open Settings(UI) of VS code (Ctrl + ,)
Search for Code-runner: Executor Map
Click on Edit in settings.json
Something like this will appear:
In cpp section: change it to "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" as in the picture above.
It is finally done now.
Below steps are for linux.
Open .bashrc file using command sudo gedit ~/.bashrc in terminal.
In the .bashrc file, add alias g++="g++ --std=c++2a" at the end of the file.
This will always compile your file in cpp+20 version.
Once done, you will not get any warning.
Note:
It is not mandatory to use cpp+20. You can use any version you like.
Change the version according to this website
https://www.learncpp.com/cpp-tutorial/configuring-your-compiler-choosing-a-language-standard/

C++ Programs can't run from Notepad++

I've tried to compile the c++ programs from the Notepad++ editor.
I am using mingw64 g++ compiler here.
Added the execution script using NppExec plugin in notepad++, the script I have used is given below,
npp_save
cd $(CURRENT_DIRECTORY)
D:\mingw64\bin\g++.exe -g "$(FILE_NAME)"
Saved this script as a macro and executed to run a cpp program, but getting the System error The program can't start because libwinpthread-1.dll is missing from your computer.
The same mingw64 compiler am using in Code::Blocks and its working fine.
How we can solve this issue ?
cmd /k gcc -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe" "$(FULL_CURRENT_PATH)" && CLS && "$(CURRENT_DIRECTORY)\$(NAME_PART).exe" & PAUSE & EXIT
Press ctrl+F5 and input this command, then click 'run'. You can also save it as a shortcut.

how to test C or C++ snippet quickly?

I am using Ubuntu and Eclipse as an IDE for C/C++.
I currently have a big project in Eclipse. Sometimes, I want to test some small functions written in C/C++ but I don't want to re-create a new project in Eclipse. It is much time consuming and slow. I want to ask if there is any better way to do this ?
(In the past, I usually used a combination of GEDIT and GCC from the shell, but I really like the auto-completion or intellisense feature in Eclipse, which GEDIT does not have. I have also tried Scribes but it does not have a full intellisense feature like Eclipse)
Use online compiler like Ideone or Codepad.
Ofcourse, they dont provide you auto code completion feature & other fancy features but that is the price you pay for quick & easy way of checking stand alone functions.
This method works without an internet connection and without exposing your code.
<ctrl>+<alt>+T <-- 0) opens a terminal
vi test.cc <-- 1) hackery
...
g++ -Wall -Wextra test.cc && ./a.out <-- 2) compile + run
rm test.cc <-- 3) clean up (optional)
Replace vi with your favourite editor or cat. Can't be less obtrusive.
Some editors like SciTE have some very basic code completion (btw btw: SciTE has shortcuts to directly compile and run code from within the editor).
Btw: QtCreator gives some decent "intellisense", and the project files are minimal. A single project file line is enough for such one-function-test.
unkulunkulu points out that you can also replace step 2 like this (there should better be no Makefile in your try-out folder; could conflict with existing targets in that):
<ctrl>+<alt>+T <-- 0) opens a terminal
vi test.cc <-- 1) hackery
...
make test && test <-- 2) compile + run
rm test.cc <-- 3) clean up (optional)
It has the tiny disadvantage that telling g++ about extra arguments (like -Wall or -std=c++0x is a bit more obtrusive).
I will advise you to use gedit with the embeded terminal plugin.It allows quick compiling through the embeded terminal.Perfect for quick testing.
You can use tcc as a C script engine.
$ cat tcctest.c
#!/usr/bin/tcc -run
#include <stdio.h>
int main(void) {
printf("Hello, tcc!\n");
return 0;
}
$ chmod u+x tcctest.c
$ ./tcctest.c
Hello, tcc!
http://www.compileonline.com
I found this Site more useful than ideone or codepad because it supports more languages than codepad and you can see the output of you code on an adjacent window you can also provide Standard Inputs and command line arguments and you can also access a file input.txt in your program.