Compiling a C++ program with a command-line command or through VS Code does not run properly unless libstdc++-6.dll is present

Viewed 451

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"
        }
    ]
}
1 Answers

Often when people can't find the output to a program in VSCode, it's because they're looking in the wrong tab. This happens fairly often.

When you run a program in VSCode using F5 (or the "Start Debugging" command), the active tab switches to Terminal.

But, when you're in debugging mode, output will be shown in the "Debug Console" tab (the tabs could do with better names).

Here's your programming running in VSCode. Note that output at the bottom:

enter image description here

Related