VS code c++: "exited with code=3221225785" - c++

I'm a complete novice to VS code, and I've only been coding with C++ for around a month. I tried this bare bones program to make sure things were set up correctly:
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "Hello world" << endl;
vector<int> v;
return 0;
}
Nothing shows up when running the executable. Removing the vector declaration causes the program to run normally.
I did find this which encountered a similar problem with declaring a string, and the solution (static linking with -static-libstdc++) works for me, though the author who gave the solution wasn't entirely sure why it worked either.
However, since I'm a noob, I don't understand that well why static linking fixed my problem, even after reading this, and am worried about some of the drawbacks mentioned (it recommends only linking statically if you absolutely have to since disadvantages outweight advantages), so I was wondering if there was some other solution besides static linking.
EDIT: Clarification--the program's outputs now show up normally in terminal, but in the output window, the same exit code still appears.

Configure VSCode as given below for "VS Code C++ : exited with code=3221225785"
Install the Code Runner Extension of Visual Studio Code.
Open the Settings(Seetings.json).
Search "code-runner.executorMap" in the Search bar.
modify
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
to
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -static && $dir$fileNameWithoutExt",
After that Right Click on source code file select the option Run Code.
For DEBUG:
add an extra parameter "-static" in "args" of tasks.json file.
Before:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
After:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-static"
],
"- static" is static linking parameter when we compiling and running.

I also encountered the same situation, due to the environment variable access error, because I installed MATLAB on my computer first,the environment variable D: matlab\bin also contains the libstdc++-6.dll link library, so the computer will first access D:\ matlab\bin instead of C: \mingw64\bin. So what we need to do is to move the C:\ mingw64\bin environment variable in front of the D:\ matlab\bin environment variable in the computer properties environment variable to solve this problem。

For me solved when I put the file libstdc++-6.dll on folder that I needed debug.
This file is in "\MinGW\bin".

Related

Node addon development - cannot open source file "napi.h"

Just started fiddling around with developing an addon for Node in C++ using Node API (for the first time, I might just add). Everything is working great for the most part - I'm using the built-in compiler in Visual Studio for starting it, debugging and playing around, and node-gyp for building it for requiring it in Node. I have an entrypoint for local testing, let's call it main.cpp, where my int main() function is defined - which is executed when I run/debug my solution in Visual Studio (using Ctrl+F5/F5).
I also have a source file where the 'node communication' is defined (NodeCommunication.cpp), and it is this file (among others) that is listed in the sources array in my binding.gyp file. It is here that napi.h is included as well, and its APIs are used. Mousing over the include however, there is error text saying "cannot open source file napi.h" - which makes sense, I guess (?) that header file is something that is only injected on build time in node-gyp somehow. However it'd be really nice, DX-wise, to be able to resolve those somehow, because right now I have to comment out all the source code in that file before compiling and running the solution in visual studio for testing purposes.
What are my options here? Might as well also attach my binding.gyp file for reference.
{
"targets": [
{
"target_name": "foobar",
"sources": [ "NodeCommunication.cpp", "Sample.cpp", "Sample.h" ],
"libraries": ["../SDK/Library/EDSDK.lib"],
'include_dirs': ["<!#(node -p \"require('node-addon-api').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'defines': [ 'NAPI_CPP_EXCEPTIONS' ]
}
]
}

Can I change the default location of output file in VS Code

Can I ask you that can I change the default output file location in VS Code when I am coding in C++ and using MingW debugger... when I debug the program... It will create the output file in the main folder... but can this output file be created in the subfolder(The P01 folder in the picture)... Thank you!
Short answer: Yes, you can.
Open the contents of your .vscode/tasks.json file. Just focus on the args:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/a.out",
],
When you hit F5 (by default setting in VS Code) to start the debugging mode, tasks.json is executed to compile the program. Here, you can change the value of the 4th line with your desired location.
Note that the mentioned line content (of tasks.json) must be equivalent to the content of "program" of .vscode/launch.json. Otherwise, the program will be compiled successfully, but it won't be launched because the location will be mismatched.

Visual Studio Code creates the executable file outside of the intended folder

I am new to Visual Studio Code and I am just starting to learn the basics of using it to compile my C++ projects. I have ran into an issue where when having a folder 'opened' in Vs code, and then having an inner folder with my 'main.cpp' file inside of it, when compiled creates the 'main.exe' outside of the innerfolder, I don't want this to happen and want the executable which belongs to that main.cpp, inside of the inner folder so that both are stored in the same folder, to me that makes more sense than it being outside of the intended folder. I really don't know where to look to change this behaviour.
Here is an example of what I mean:
Update: Will post an answer to my own problem soon
Update2: I answered it in full detail for anyone ever having this problem/question
I came up with an easy solution. You only have to hide the files with extension .exe. With your answer many people get confused so you have to follow some steps.
step 1: go to files
step 2 : click on preferences-->then settings
step 3 : then search for "exclude file"
step 4 : select add pattern
step 5 : "**/*.exe" add this -->then click ok
step 6 : close the visual studio code then open it again
The process is very simple.
The following process shows how to create/make executable/.exe file in your provided/specific folder
goto Command Palette... by pressing Ctrl + Shift + P
press Backspace to remove > symbol
search tasks.json file, and hit Enter
-. in tasks.json file, you have to look for the following code (Note: this code can be shown 2 or 3 times in the file. Perform the operation for everyone)
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
replace the code "${fileDirname}\${fileBasenameNoExtension}.exe" with your new path, for example, i have a new folder named as "myNewFolder" in my workspace foler, i will replace it as
"${fileDirname}\myNewFolder\${fileBasenameNoExtension}.exe"
hit Ctrl + s to save changes
Now, the following process shows how to run/launch your executable/.exe file provide in your allocated folder/location
goto Command Palette... by pressing Ctrl + Shift + P
press Backspace to remove > symbol
search launch.json file, and hit Enter
look for the key named as "program"
-. it will be as follows
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
change the filepath. in my case, i will change it as follows
"program": "${fileDirname}\myNewFolder\$fileBasenameNoExtension}.exe",
Save changes
If you're using Code Runner extension, then :
Press Ctrl + Shift + P
step1
delete the ">" symbol (press Backspace to delete)
search "settings.json" which in AppData directory
step3
then you can look at "code-runner.executorMap", in "cpp" line. You can type that arguments
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", // <-- This
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
//etc.
},
"code-runner.runInTerminal": true,
Note :
Actually the argument in step 4 is default settings. So, you don't have to edit it if it's your first time using it.
but if u want to customize the argument (in most cases is used to change the output directory), that is the place you'll be used for.
e.g. in my case, I want to separate the .cpp and .exe files to "src" and "bin" folder, so :
"cpp": "cd $dir && g++ $fileName -o ..\\bin\\$fileNameWithoutExt && ..\\bin\\$fileNameWithoutExt",
result :
The separate folder look like : folder_screenshot
Terminal :
PS C:\Coding\C++\src> cd "c:\Coding\C++\src\" ; if ($?) { g++ string.cpp -o ..\bin\string } ; if ($?) { ..\bin\string }
Hi
Hope it works
PLEASE READ IT IS A FULL AND VERY DETAILED EXPLANATON ON A POTENTIAL BUG, WHICH COULD AFFECT WORKFLOW, VISUAL STUDIO HAS YET TO FIX THIS! I WILL REPORT THIS AS SOON AS POSSIBLE
So I've been ripping my hairs out my head with this non-sense of a problem, but finally I have come to a conclusion that Visual Studio has a very hidden bug (since I was not able to find anyone with a similar question/problem), or very unclear documentation.
The initial problem was that if I were to have (I will give a clearer example below) an outer folder opened in Visual Studio Code with an inner folder inside which had the main.cpp file, Visual Studio Code would not budge and would put the main.exe file in the outer folder and not the inner one, you can imagine why this would become a problem if I had many other inner folders inside the outer one and all had their exe file called main.exe.
Let me give you an example of what I mean:
Suppose I have the following path (with main.cpp already having code inside) - outer-folder\inner-folder\main.cpp
Now I open the outer-folder in Visual Studio Code. The following is what it should look like if opened in Visual Studio Code for the first time, without having built or compiled anything
After opening outer-folder for the first time in Visual Studio Code
If I were to try and Ctrl + F5 this now, using the option given by Visual Studio Code of... C++ (GDB/LLDB) ---> g++.exe - build and debug active file, the following would be seen on screen:
After Ctrl + F5 and following options as stated in my explanation
In this case I am in my D: drive and in the example path stated at the beginning of my explanation. The error is saying that main.exe doesn't exist in the inner-folder, which is completely true, it exists inside the outer-folder. Why? Because we need to edit the tasks.json (already misleading information by Visual Studio Code, by inclining me to open the launch.json file, although they are correct to some extent since it is saying launch.json can't find the file to launch so maybe you got your path wrong in there, it would be helpful to also have another option for the tasks.json) which is located in the folder automatically made by Visual Studio Code .vscode along with launch.json.
When in the tasks.json file, there is a variable (if you may call that, though I think there is a different name for that), called args...inside that ([]) are the arguments that Visual Studio Code automatically passes in to the terminal to compile and build your C++ code. The details of all the arguments passed in aren't important for this problem, but the second one (${file}) is referring to the file which we are trying to run, as stated in https://code.visualstudio.com/docs/editor/variables-reference (keep this open as we will need it), the next one (-o) is just telling it to output it like and where the forth argument specifies. The forth one is the really important one here, if we go back to the link which has a reference of the variables that Visual Studio Code provides, ${fileDirname} "should" infact mean the same as saying outer-folder\inner-folder...right? NO! It is wrong, it is actually saying outer-folder, which is the reason why main.exe is being built outside of inner-folder, so we already have a red flag of some type of misinformation from the docs or some type of bug.
Now as the docs that Visual Studio Code provide about gcc on VSC (https://code.visualstudio.com/docs/cpp/config-mingw) we can change somethings, so that the program is built and output in a different location, to do so we need to go into the tasks.json and from there changing the forth argument in args to ${fileDirname}\\inner-folder\\${fileBasenameNoExtension}.exe, now don't reach for the Ctrl + F5 key yet, I need to explain why that won't work, but first let's try something out...press Ctrl + Shift + B, that will build your program but not run it (if you are following this make sure to be building the correct file and not trying to build tasks.json itself lol), now when we do that we can see that another main.exe file will have been built and output in the correct location output is in the correct location. Ok now the docs on configuring mingw in Visual Studio Code suggest than now you can run your file by going to the terminal and running it from there (no need for an explanation on how to do that). What is the fun in that though what if you want to debug it? Here is where we are going to find another potential bug or very bad documentation surrounding this. If we open our launch.json now (this is the file that is ran when you press Ctrl + F5) and then we look at the variable (again I'm nearly 100% sure that is not what it is called but I don't know what to call it at this point) program: here is where you tell launch.json where the location to the file you want to run is, in our case it is where our main.exe should be located. Since we have fixed the issue with in the tasks.json and can now build with Ctrl + Shift + B every time we want to rebuild in the right location, we can now run with Ctrl + F5, right? NOPE! This is where the next bug/bad documentation comes along! If you look at the very bottom of the file there is a "preLaunchTask:" this is the name of the value stored in "label": back in our tasks.json, and it has to match perfectly so that launch.json knows which task to run when invoked. The problem is that even though they match automatically already (as their names are the same by default), it still doesn't work, Visual Studio Code for some reason say, YOU KNOW WHAT SACK YOUR TASKS.JSON WE WILL OVERWRITE THAT AND USE THE DEFAULT ONE! Even if your the default name matches the default on launch.json, it doesn't run your tasks.json it overwrites it with the default one (not 100% sure what it does but that is my best guess, as it 100% doesn't run our one), now the interesting bit is that when we change both the "preLaunchTask": and the "label": to the same thing just anything else than the default one (even add one more character anywhere but make sure both match), when we do Ctrl + F5 it now runs our tasks.json! Yet... all of this for nothing because at the end of the day we had to hard code that tasks.json and we would have to do that for every file that we want stored in some other inner folder! You can fix this (as I later found out) by just opening that folder (inner-folder) as the main one instead of the outer-folder, and boom problems fixed, but then you can't see your whole project as you would have been able to, had your outer-folder been opened.

How to compile multiple files in sublime text 3

I'm using Sublime Text 3 to write some school projects and I have to write a program with some custom headers which have 1 function. The problem is that the build system (what sublime text writes in cmd to build) only puts the main.cpp file and not the headers and .cpp of the headers in the cmd, so it's not compiling properly.
I'm looking for a way to compile with headers just by writing the main.cpp file in the cmd ("g++ main.cpp -o main.exe") or to modify the build system so it adds all the headers in the cmd line ("g++ headersourcefile.cpp header.h main.cpp -o main.exe") (this one compiles ok)
The build system is written in JSON (which I have no idea about :))
---Edit---
My problem is that Sublime Text is only compiling the file that I'm writing on, and not the #includes that I made, f.e. #include "organiser.h". The build system that I'm using is a custom one that I wrote based on some of them found on the Internet (that's why I thought I've done something wrong). Now I'm trying the "make" build system and I have no idea how it works(it says: "no target specified and no makefile found. Stop.).
Thanks for you attention :)
Thanks to Andrew! You can make his build run with the code:
{
"cmd" : ["g++ *.cpp -o $file_base_name &./$file_base_name"],
"working_dir": "$file_path",
"shell":true
}
As sugested I got myself into makefiles and got one of them working, it's building the project and doing what I wanted. Needed to reinstall the mingw tho, to get one with msys2 so make could work correctly.
Thanks for the help
This is answered here: https://stackoverflow.com/a/21956602/1599699
{
"cmd" : ["g++ *.cpp -o executablename"],
"shell":true
}
or, as I mentioned in the comments:
"Use "cmd": ["g++ *.cpp -o test && gnome-terminal -- './test'"] (or equivalent terminal program like konsole or xterm instead of gnome-terminal) to then run it. I was using gnome-terminal -x sh -c './test', but then it said to just use -- to terminate the options and put the command afterwards."

How to compile and run .cpp files after writing them in sublime text?

I really prefer writing code in sublime text or anything else. So, naturally that's what I want to use. However, when I try to open the file in Netbeans, I get an error. So, I want to know how I can save a .cpp file from sublime text and then go about running it through the command prompt. I know I have to set up a path or something, but I'm not exactly sure how to do it. Thanks for any help at all. Also, I am new to C++ and programming in general(have dabbled in Python a bit).
EDIT: Really sorry, I meant how do I actually execute/run the file afterwards. Like if the program were to just print out "Hello World".
The following build system should suit your needs, assuming that you're using the GNU Compiler Collection and g++ for compiling your .cpp files:
{
"cmd": ["g++", "${file}", "-o", "${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_base_name}"]
}
]
}
Please note that the following instructions are for Sublime Text 2 only...
To use it, select Preferences -> Browse Packages... to open the Packages folder in Windows Explorer. It should be located in C:\Users\YourUserName\AppData\Roaming\Sublime Text 2. Or not, depending on your install. In either case, browse to the C++ directory and open the file C++.sublime-build in Sublime and set the syntax to JSON if you want it to look prettier. Replace its entire contents with the code above, then save the file. The old code is kind of convoluted, and also runs some commands needlessly.
Now, set the build system by going to Tools -> Build System and selecting Automatic. Assuming that g++ is in your PATH, you can build your executable using the CtrlB keyboard shortcut, also available via Tools -> Build. If your binary has already been compiled, you can run it by pressing CtrlShiftB.
One final note: if your program asks for any kind of input, or has a graphical user interface, this Run command won't work. Instead, replace it with the following:
"name": "Run",
"cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}"],
"shell": true
This will open a new instance of the command line and run your program from there, instead of inside Sublime. The /k switch means that the window will be kept open after your program has run, so you can examine output, errors, etc. If instead you want the window to close immediately, simply change the /k to /c.
Good luck!