23

I installed Visual Studio 2010. I wrote a simple code which I'm sure is correct but unfortunately, when I run the code, I get the error below.

Here is my code:

#include<iostream>
using namespace std;
int main (){ 
  cout <<"Hello StackOverFlow ;)";
  return 0;
}

And here is the error:

Unable to start program 'C:\Users\Soheil\Desktop\New folder\sam\Debug\sam.exe The system cannot find the file specified

Would you help me solve the issue? Should I define the project in a specific directory? I've spent a ton of hours to solve this issue and have not had any success yet.

pmr
  • 58,701
  • 10
  • 113
  • 156
Sam
  • 4,357
  • 6
  • 36
  • 60
  • Please elaborate over what you tried while spending "a ton of hours" on this. – djechlin May 12 '13 at 20:53
  • I'm changing it right now -- thanks for pointing that out. – Sam May 12 '13 at 20:54
  • 8
    Did you *Build* the project before trying to *Run* it? Your build log, visible in the screenshot, shows no output. – Ben Voigt May 12 '13 at 20:54
  • Yes, I did ... I do New Project ==> Empty Project (cpp) – Sam May 12 '13 at 20:55
  • 6
    *Build*, as used in computer programming, means running the compiler and linker tools. Not the actual creative act of developing the code. – Ben Voigt May 12 '13 at 20:57
  • 2
    @Sam Building a project is not the same as creating a new project from your Files menu. To build a project you use F6 or F5 keys (or "Clean and build" from your debug menu). – Daniel May 12 '13 at 20:57
  • 3
    The most likely cause of this error is that you created a C++ library project instead of a C++ Win32 Console Application, which is what your code best fits into. – Ben Voigt May 12 '13 at 20:59
  • Please improve the title. As is, it is too vague to be useful to any future visitors with the same problem. – Raymond Chen May 12 '13 at 21:03
  • @BenVoigt Daniel, when I debug my code I get the error I mentioned in my question (I removed the screen shot and I typed the error). – Sam May 12 '13 at 21:04
  • @BenVoigt, I did with C++Win32 but I still get exactly the same error – Sam May 12 '13 at 21:05
  • @RaymondChen, I appreciate it if you change it with any better title that you would think of. – Sam May 12 '13 at 21:06
  • @Sam: What is the output when you compile it? You can't debug until after a successful compile and link. – Ben Voigt May 12 '13 at 21:08
  • Can you provide us with build log after rebuild. It is Output -> Build window context in the bottom of the IDe – Lol4t0 May 12 '13 at 21:09
  • @Lol4t0: A while ago there was a screenshot which showed the build log. Completely empty. – Ben Voigt May 12 '13 at 21:09
  • @BenVoigt, I've seen, but well, it might be erased. That's why I propose to rebuild first and then show it. – Lol4t0 May 12 '13 at 21:10
  • Maybe I'm doing something wrong in creating a new project? would that be a possibility? Should my project be saved in the folder that I have my visual studio installed? – Sam May 12 '13 at 21:12
  • @Sam: No. The directory that Visual Studio suggests "My Documents\Visual Studio Projects\" is reasonable. But that isn't your problem, a subdirectory of the desktop will work too. Either the commands you're using or your project settings are the issue. – Ben Voigt May 12 '13 at 21:13
  • @Sam I can replicate the error if I create an Empty Project and not add any files to it. Where/how are you adding the file containing the code you mention? – Andrei May 12 '13 at 21:17
  • @Andrei, see screenshot in rev.1. File is there. Probably Sam haven't built solution. – Lol4t0 May 12 '13 at 21:18
  • @Lol4t0 VS will ask you if you want to build the solution if you haven't already. – Andrei May 12 '13 at 21:20
  • 1
    @Andrei, if you haven't checked box "Do not ask again" – Lol4t0 May 12 '13 at 21:21

12 Answers12

16

This is a first step for somebody that is a beginner. Same thing happened to me:

Look in the Solution Explorer box to the left. Make sure that there is actually a .cpp file there. You can do the same by looking the .cpp file where the .sln file for the project is stored. If there is not one, then you will get that error.

When adding a cpp file you want to use the Add new item icon. (top left with a gold star on it, hover over it to see the name) For some reason Ctrl+N does not actually add a .cpp file to the project.

Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47
cdelsola
  • 407
  • 2
  • 8
  • 17
7

Encountered the same issue, after downloading a project, in debug mode. Searched for hours without any luck. Following resolved my problem;

Project Properties -> Linker -> Output file -> $(OutDir)$(TargetName)$(TargetExt)

It was previously pointing to a folder that MSVS wasn't running from whilst debugging mode.

EDIT: soon as I posted this I came across: unable to start "program.exe" the system cannot find the file specified vs2008 which explains the same thing.

Community
  • 1
  • 1
ReturnVoid
  • 1,106
  • 1
  • 11
  • 18
3

For me, I didn't have my startup project set in Solution Explorer.

Go to Solution Explorer on the left of VS, right click your unit test project, and choose "set as startup project".

I had just ported my code to a new workspace, and forgot that when I opened the project in VS in the solution there, that I needed to re-set my startup project.

Michele
  • 3,617
  • 12
  • 47
  • 81
2

I have recently not used VS 2010.

Does your application really build correctly? To get more control in VS 2010 C++ Express, you can check menu item "Expert Settings" under Tools>Settings to get a Build' menu.
After clicking Build->Build Solution (or Rebuild), you may verify in Output window (View->Outout), if your application is compiling and linking correctly.

Sources :

Hope it helps.

MatthewD
  • 2,509
  • 2
  • 23
  • 27
quick-
  • 1,425
  • 2
  • 9
  • 13
2

I know this is an old thread, but for any future visitors, the cause of this error is most likely because you haven't built your project from Build > Build Solution. The reason you're getting this error when you try to run your project is because Visual Studio can't find the executable file that should be produced when you build your project.

Ethan Bierlein
  • 3,353
  • 4
  • 28
  • 42
  • 2
    This is a good first step to take, but unfortunately the error can come up **even in the case of a successful build**. Depending on the setup, if the linker output file is not matching the target file's path, name, and extension, it may well build a file - even showing "build / rebuilt succeeded" - but it will be unable to run in debug. – Aaron Thomas Aug 19 '16 at 15:17
2

As others have mentioned, this is an old thread and even with this thread there tends to be different solutions that worked for different people. The solution that worked for is as follows:

Right Click Project Name > Properties
Linker > General 
Output File > $(OutDir)$(TargetName)$(TargetExt) as indicated by @ReturnVoid
Click Apply

For whatever reason this initial correction didn't fix my problem (I'm using VS2015 Community to build c++ program). If you still get the error message try the following additional steps:

Back in Project > Properties > Linker > General > Output File > 

You'll see the previously entered text in bold

Select Drop Down > Select "inherit from parent or project defaults"
Select Apply

Previously bold font is no longer bold

Build > Rebuild > Debug

It doesn't make since to me to require these additional steps in addition to what @ReturnVoid posted but...what works is what works...hope it helps someone else out too. Thanks @ReturnVoid

Chris
  • 934
  • 1
  • 17
  • 38
  • for me I don't see an option for inherit from parent or project defaults after I click the drop down arrow for output file – MATH ASKER May 17 '19 at 20:21
2

I came across this problem and none of these solution worked 100%

In addition to ReturnVoid's answer which suggested the change

Project Properties -> Linker -> Output file -> $(OutDir)$(TargetName)$(TargetExt)

I needed to changed

Project Properties -> C/C++ -> Debug Information Format -> /Zi

This field was blank for me, changing the contents to /Zi (or /Z7 or /ZI if those are the formats you want to use) allowed me to debug

rtpax
  • 1,687
  • 1
  • 18
  • 32
1

I know this thread is 1 year old but I hope this helps someone, my problem was that I needed to add:

    #include "stdafx.h"

to my project (on the first line), this seems to be the case most of the time!

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
Windows65
  • 57
  • 1
  • 7
  • 1
    Nah, that's unrelated to the problem here. You do need to include `stdafx.h` (or whatever it is named, this is the default) if you are using precompiled headers. But you'll get a specific compiler error if you forget—hard to misunderstand. – Cody Gray - on strike May 28 '14 at 04:26
  • 1
    Not true. If you are doing TWL/MFC programming you HAVE to include stdafx or weird things like this error will happen. – wonton Oct 25 '14 at 18:45
0

I got this problem during debug mode and the missing file was from a static library I was using. The problem was solved by using step over instead of step into during debugging

misty
  • 11
  • 1
  • 4
-1

if vs2010 installed correctly

check file type (.cpp)

just build it again It will automatically fix,, ( if you are using VS 2010 )

ANJi
  • 27
  • 8
-1

I had a same problem and i could fixed it! you should add C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64 for 64 bit system / C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib for 32 bit system in property manager-> Linker-> General->Additional library Directories

maybe it can solve the problem of somebody in the future!

hani89
  • 21
  • 1
-2

Since this thread is one of the top results for that error and has no fix yet, I'll post what I found to fix it, originally found in this thread: Build Failure? "Unable to start program... The system cannot find the file specificed" which lead me to this thread: Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt' after installing Visual Studio 2012 Release Preview

Basically all I did is this: Project Properties -> Configuration Properties -> Linker (General) -> Enable Incremental Linking -> "No (/INCREMENTAL:NO)"

Community
  • 1
  • 1
jon.kerlinger
  • 15
  • 1
  • 5