0

I'm following this tutorial to create a C++ Hello World app for Visual Studio.

https://tutorials.visualstudio.com/cpp-console/install

I've installed the software, selected "Windows Console Application" and copy/pasted the Hello World program from the tutorial:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()  
{  
    cout << "Hello, world!\n";
    return 0;
}

However, when I try to run the Local Windows Debugger, I get this error:

Unable to open program. \repos\HelloWorld\Debug\HelloWorld.exe The system cannot find the file specified.

How would I include an .exe file?

Also, I have an error "cannot open stdafx.h", but I assume that's connected to this.

EDIT:

I removed "#include "stdafx.h"" and got the same error. It also said there was an unexpected end of file and suggested I #include "pch.h", so I did.

#include <iostream>
#include "pch.h"
using namespace std;

int main()
{
    cout << "Hello, world!\n";
    return 0;
}

I'm still getting the error that it can't find .exe ...also 'cout' is an undeclared identifier.

asendjasni
  • 963
  • 1
  • 16
  • 36
Jazael
  • 51
  • 2
  • 9
  • 2
    Yes the `"stdafx.h"` error is *very* much the reason for your problem. It's a build error, and if there's a build error, then no executable program can be generated, and there's nothing to run. Try a simple workaround: Just remove that `#include "stdafx.h"` line and see what happens when you try to build. – Some programmer dude Mar 02 '19 at 20:24
  • 2
    Unfortnuately I'm guessing the next error will be 'unable to find precompiled header directive' (or however Visual Studio phrases it). – john Mar 02 '19 at 20:36
  • You ninja'd me...yeah, it was something like that. Now it's something else. I editted the OP. – Jazael Mar 02 '19 at 20:42
  • 2
    If you use a precompiled header it **must** be the first include directive. The compiler will ignore everything that appears before it. Move `#include "pch.h"` to the first line. – Blastfurnace Mar 02 '19 at 20:45
  • Ahh yes, that fixed it. Thank you! Although I am curious, what is stdafx.h for and what's the different for pch.h? – Jazael Mar 02 '19 at 20:50
  • 3
    Related: [What is “pch.h” and why is it needed to be included as the first header file?](https://stackoverflow.com/questions/54121917/what-is-pch-h-and-why-is-it-needed-to-be-included-as-the-first-header-file) – Blastfurnace Mar 02 '19 at 20:50
  • "stdafx.h" is the old default filename for a precompiled header. I think it changed to "pch.h" with Visual Studio 2017. Note that you can turn off precompiled headers in the project options. They aren't very useful for small projects. – Blastfurnace Mar 02 '19 at 20:52

0 Answers0