c++gdbg++

GDB won't build an exe file when trying to separate header and implementation files in C++ VS Code


Am new to C++ and am having an issue with separating header and implementation files, when I just use an implementation file or include the function definition in the header file it works, however if I only use a function prototype in the header then I get an error "The preLaunchTask 'C/C++: g++.exe build active file' terminated with exit code -1.".

I've been trying different launch.json configurations and nothing seems to work so I'm lost, could anyone please tell me what I'm doing wrong here? I've made a simplified set of files to help isolate this issue and here they are:

From basicInput.h(header):

#include <iostream>

float inputPrompts(const std::string currentPrompt);

From basicInput.cpp(implementation):

#include "basicInput.h"
#include <iostream>

float inputPrompts(const std::string currentPrompt) {
    float inputReturn;
    std::cout << currentPrompt;
    std::cin >> inputReturn;

    return inputReturn;
}

From main.cpp:

#include "basicInput.h"
#include <iostream>

int main() {
    std::cout << inputPrompts("Enter a number: ");

    return 0;
}

All files are in the same folder and the compiler is g++.

I've tried putting the same exact code with the same file names in Replit to see if it's just an issue with my compiler, and it worked fine, so I know it has to be something with my configurations, I just have no idea what exactly the issue is. I've tried messing around with different launch.json configurations to see if that was the issue and nothing I've tried has worked, although regular single-file source code compiles just fine still. Each time no .exe is generated and the program doesn't even start running, have no idea what could be causing this.

From tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${workspaceFolder}/*.cpp"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Solution

  • First look at your tasks.json file

    "label": "C/C++: g++.exe build active file",
    "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
    "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
    ],
    

    Here ${file} tells VSCode to build your program from a single file. Specifically it is telling VSCode to build from the file you currently have open in the editor. Obviously that does not work when you are trying to build from multiple files.

    Here's how it should look

    "label": "C/C++: g++.exe build active file",
    "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
    "args": [
        "-fdiagnostics-color=always",
        "-g",
        "main.cpp",
        "basicinput.cpp",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
    ],
    

    See how both required files are mentioned explicitly.

    This issue is mentioned in the official documentation but even those who read the documentation often seem to miss it.

    The other thing to mention is the label

    "label": "C/C++: g++.exe build active file",
    

    obviously this is not longer accurate, so you should change it. Now this label should be referenced in your launch.json file as a preLaunchTask. So if you change the label here, make sure to make the same change in your launch.json file.