C++ and VScode integration - c++

Tried my whole day to integrate between vscode and c++ with no success i get output
[Done] exited with code=1 in 0.124 seconds
in VScode when i try to write c++ code,
My input:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
{
I have downloaded VScode and worked through this guide https://code.visualstudio.com/docs/cpp/config-mingw
I have created a path as explained in the guide Where i added the path
Inside this i added a new Path
In the command prompt i typed
First : pacman -S --needed base-devel mingw-w64-x86_64-toolchain
in the command prompt when i type
g++ --version
gdb --version
i get this
""g++ (Rev1, Built by MSYS2 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.""
In additional i have download the extension in VScode (runner and c++/c
Would like to get help here so i can start using c++ in VScode
I am using windows 10

I solved the problem,
I had to delete runner, restart vscode, reinstall runner and restart vscode again, Hope it will help others.

Your code gets executed and program shuts itself. If you add getch() that means "Get Character", the program will wait to get a character before exiting
Note: Include conio.h when you use getch()
Hope this will solve your problem
#include <iostream>
#include <conio.h>
int main() {
std::cout << "Hello World!";
_getch();
return 0;
}

Related

gdb <error reading variable> for any string object

Lets take this very simple program here for example:
// test.cpp
#include <string>
#include <iostream>
using namespace std;
int main() {
string str = "Hello";
cout << str << endl;
return 0;
}
now I compile this code with g++ compiler:
g++ -g test.cpp -o test.exe
now I am trying to debug this with gdb:
gdb test.exe
after I set breakpoint on main and then reach the line return 0, I try to see what is in the string str. But I cannot print it in the console. It says <error reading variable>. Not only in gdb console, even Visual Studio Code UI using gdb gives the same output.
Here is a screenshot of my console:
I have searched for this everywhere and the only relevant question I found was this, which did not work.
I also found this post on github VS Code repo issues. The fix suggested there might work I am not sure, I cannot find the setting that he suggested on my Windows 11 machine.
How do I read the value in the string in debug mode?
Edit
After #ssbssa suggested me to update my gcc, I used MSYS2 to get the latest gcc, g++, and gdb versions. Now I have gdb 12.1. Now it is not showing the old error anymore but now it says "Converting character sets: Invalid argument". Still struggling to get it to work.
First run your program with gdb like so:
gdb test.exe
Now inside the command line interface run the command:
set charset UTF-8
This should temporarily fix your problem. The only inconvenience might be that you need to run this line every time you debug on your command prompt with GDB.
I noticed that you are also using Visual Studio Code. You can install C++ extensions for VS Code and there you can add the command set charset UTF-8 in the launch.json setupCommands array as shown here. This way you can debug your application faster.

C++ gdb pretty printing in Ubuntu 18.04 visual studio code

I am trying to make pretty printing to work on Ubuntu 18.04 from visual studio code 1.64.2.
I tried to follow instructions initially from here and then the answer by Devymex as detailed in here.
Then further digging up revealed that the gdb pretty printing itself is not working as I tried to build, make, and run my code outside of VSCode. I had gcc 7.5 preinstalled on Ubuntu 18.04 and then I installed 11.2. But nothing worked.
The code I am trying to run
#include <string>
#include <iostream>
std::string str = "hello world";
int main ()
{
std::cout << str << std::endl;
return 0;
}
The output I get while debugging with gdb
Additionally, I tried to check which pretty-printer is configured or set up by typing info pretty-printer from within (gdb). But it appears that the appropriate pretty printer is not configured.
I tried reinstalling gcc 11.2 from the source by configuring with python using both python 2.7 and python 3.6.9 using the command ./configure --with-python and ./configure --with-python3. But nothing worked!
Can anyone please help me out?
I found a solution as posted here. The gdb was not able to find the location where the python printers.py was located. The file was located under /usr/share/gcc/python/libstdcxx/v6/printers.py.
What I needed to do is create a .gdbinit file on my home directory including the following lines of code
python
import sys
sys.path.insert(0, '/usr/share/gcc/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
Next source the file with source .gdbinit and then try again the info pretty-print. All the alternate options are now available. Subsequently, gdb debugging and vscode was able to show the contents of the C++ STL containers.

g++ is recognized by cmd but not by vs code

I'm trying to set c/c++ environment in my visual studio code. I have installed the mingw and set the environmental variable of the bin folder. However upon running a code in vs code, it shows the following error:
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. At line:1 char:40
cd "e:\vsCodes\Cpp Codes" ; if ($?) { g++ threen1.cpp -o threen1 } ; ...
CategoryInfo : ObjectNotFound: (g++:String) [], CommandNotFound Exception
FullyQualifiedErrorId : CommandNotFoundException
But command prompt shows the following message when I enter g++ --version:
g++ (tdm64-1) 10.3.0 Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
I have tried solving the issue following the answers in this thread.
You are using powershell terminal, If you dont know what powershell is or you dont need powershell, I recommend you to run Command prompt terminal (cmd). It's available in terminal menu here:
In powershell you can verify that g++ in PATH running this command echo $env:path, it's powershell equivalent of echo %PATH%

how to compile c++ program in mac terminal

i wrote a program in my mac using sublime text as the plateform..
#include<iostream>
using namespace std;
int main()
{
cout<<"HELLOW WORLD";
return 0;
}
this was my program..
i saved it in desktop as hellow.cpp
while compiling on mac terminal as g++ hellow.cpp, i found an error
adarshs-MacBook-Air:Desktop adarshak$ g++ hellow.cpp
xcrun: error: invalid active developer path
(/Library/Developer/CommandLineTools), missing xcrun at:
/Library/Developer/CommandLineTools/usr/bin/xcrun
anyone plese help me to find out the error
It sounds like you don't have the command line developer tools installed. Run this command from Terminal once:
xcode-select --install
This will bring up the download & installation UI. Follow this through to the end. (It may take a while depending on the speed of your internet connection.)
From then on compiling should work.

Notepad++, NppExec, CreateProcess() failed with error code 2, Windows 8.1

I'm new to Notepad++ and C++ programming language. I couldn't figure out what has gone wrong, albeit, it might look simple to resolved to many.
Tried to search for solution, but to no avail.
While trying to config the application for C++ compiler on Windows 8.1, I encountered the below message.
NPP_SAVE: C:\Users\rolle_000\Desktop\HelloWorld.cpp
CD: C:\Users\rolle_000\Desktop
Current directory: C:\Users\rolle_000\Desktop
Compiled.exe -c -w "HelloWorld.cpp"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
================ READY ================
The C++ basic code, simple to testing only.
// A hello world program in C++
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
The NppExec script taken from
How to compile and run C files from within Notepad++ using NppExec plugin?
Below embedded mine, script hasn't change much.
NPP_SAVE
CD $(CURRENT_DIRECTORY)
Compiled.exe -c -w "$(FILE_NAME)"
Pls advice, thank you.
You're trying to execute a
Compiled.exe
which indeed doesn't exist (yet) instead of the
perl.exe -c -w "$(FILE_NAME)"
perl.exe is the perl's executable and is supposed to be used with a perl's program. To compile C++ programs you will need to use a C++ compiler.
Now: this all boils down to the compiler you want to use... which one are you going to use? MSVC (Microsoft Visual Studio) ? Bloodshed dev-cpp?
Example: if you have MSVC2010 installed you might use:
Execute Start->All Programs->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio Command Prompt (2010)
Digit cl (yourFileName).cpp
You're done, yourFileName.exe should now exist
So the above would have to be rewritten as:
cl.exe "$(FILE_NAME)"
after making sure the path to cl.exe is correctly available.