Organizing Functions in Files in VSC

Hello! I’m relatively new to learning C++ and coding in general. I use Visual Studio Code and am on the lesson about organizing functions in files, where you create a greet function header/hpp file and a greet cpp

To my notice, everything I’m aware of matches the lesson, but when I try to run it, I get the error pictured along with my main code function below:

(Please forgive poor phone photo. Wi-Fi is out atm. Can provide clearer screenshots when it’s back up)

Under greet.cpp, I have the following:

#include

using namespace std;

void greet(string name) {
cout << “Hello “ << name;
}

And for greet.hpp:

#ifndef UTILS_GREET
#define UTILS_GREET

#include

void greet(std::string name);

#endif

When I try to run and debug it, I get the following error:

launch: program ‘C:\Users\wfall\OneDrive\Documents\Coding Projects\Lessons\HelloWorld\main.exe’ does not exist

Followed by the options to open ‘launch .json’ or cancel.

I’ve never encountered this before :confused: any guidance would be greatly appreciated, as I want to make sure I know why what I can only assume will be an important feature isn’t working for me

Ty!!!

Oh weird, it got rid of some of my notes??

For the greet.cpp, it’s #include < iostream >

For greet.hpp it’s #include < string >

Look at these links. They might help.

  1. Probably for your issue:
    Configure launch.json for C/C++ debugging in Visual Studio Code

  2. Complete setup of VS Code for C++:
    Introductory Videos for C++ in Visual Studio Code

  3. Or look at this post:
    C++ Project setup VSCode

1 Like

Using these bits of code, it compiles fine for me.

main.cpp

#include "utils/greet.hpp"

using namespace std;

int main() {
    greet("Allan");
    return 0;
}

greet.hpp

#ifndef UTILS_GREET
#define UTILS_GREET

#include <string>

void greet(std::string name);

#endif

greet.cpp

#include "greet.hpp"
#include <iostream>

using namespace std;

void greet(string name) {
    cout << "Hello " << name;
}

Typically when something fails, there is a typo somewhere. Because when you copy/pasted, some things didn’t come through correctly, I can’t tell if that’s your issue. But these code sections I posted do compile just fine.

If that doesn’t compile for you, it must be related to the support files like launch.json or makefile. I use CLION, so my config is a little different than yours. I just wanted to post correct code to help you and/or others.

Jerry

PS: When you post code, use the CODE markup. It helps a ton. :). In your case, you used QUOTE, not CODE for markup. It’s the icon to the right for code.

Thank you for these resources. They are helping me understand at least the purpose of launch.json files, but I’m still encountering the same error trying out different configurations both by the system’s Intellisense and suggested launch.json code on the website.

Would you by chance be able to provide an example of launch.json code that works for you so I can backwards-formulate where the issue is?

thank you for the tip about using the code option. My code syntax was correct thankfully with no typos. From the looks of it, it’s def my launch.json that probably has the issue.

I presume CLION is a different program? (I’m a BEGINNER beginner so a lot of this is jargon I’m not familiar with). I asked the same of PomPomBalls, but any chance you could provide a sample of launch.json code that should work? Help me find what works and what doesn’t?

Provide your current launch.json.

Nvm all, my friend helped me solve it. It was complications with my tasks.json file, not launch.json.

Here’s my code for the poor soul trying to solve this in the future:

main.cpp

#include <iostream>
#include "utils/greet.hpp"

using namespace std;

int main(){
    greet("Alan");

    return 0; 
}

greet.cpp under my “utils” subfolder:

#include <iostream>

using namespace std;

void greet(string name){
    cout << "Hello " << name;

}

greet.hpp under “utils” subfolder:

#ifndef UTILS_GREET
#define UTILS_GREET


#include <string>

void greet(std::string name);

#endif

tasks.json in “args”: [ ]

"args": [
                "-fdiagnostics-color=always",
                "-g",
                "-std=c++20",
                "${workspaceFolder}\\*.cpp",
                "${workspaceFolder}\\utils\\*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],

and launch.json wasn’t needed, but in case anyone would still like to see:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": []
    
}