The situation is is a problem in compiling and running C++ programs with MinGW on Windows, in VS Code or compiling from the command line in cmd.
The first example writes "Hello" to the standard output.
The second example runs and finishes but writes nothing.
The difference between the two examples is merely declaring a string in the second example.
When I compile the second example with Visual Studio 2019 on Windows, it runs properly.
UPDATE I discovered that when I copy Z:\Software\MinGW\bin\libstdc++-6.dll to the same directory as my source, it works, both when compiling through the VS Code default build task and when compiling from the command line.
Example #1 (runs properly):
#include <iostream>
#include <string>
int main()
{
// std::string str;
std::cout << "Hello" << std::endl;
}
Example #2: does not produce any output when run. Except in the case of compiling after copying libstdc++-6.dll to the same directory as the source.
#include <iostream>
#include <string>
int main()
{
std::string str;
std::cout << "Hello" << std::endl;
}
I have compiled the two examples with g++ on the command line and also through a default build task in VS Code.
First, the command line used to compile the two programs is
g++ my_program.cpp -lstdc++ -o my_program.exe
Second, the tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "Z:\\Software\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: Z:\\Software\\MinGW\\bin\\g++.exe"
}
]
}
