C++ 运行C++程序时“系统找不到指定的文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16511925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:25:55  来源:igfitidea点击:

"The system cannot find the file specified" when running C++ program

c++visual-studio

提问by Sam

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.

我安装了 Visual Studio 2010。我写了一个简单的代码,我确信它是正确的,但不幸的是,当我运行代码时,我收到以下错误。

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

Unable to start program 'C:\Users\Soheil\Desktop\New folder\sam\Debug\sam.exe 系统找不到指定的文件

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.

你能帮我解决这个问题吗?我应该在特定目录中定义项目吗?我花了很多时间来解决这个问题,但还没有取得任何成功。

回答by cdelsola

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

这是初学者的第一步。同样的事情发生在我身上:

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

查看左侧的解决方案资源管理器框。确保那里确实有一个.cpp文件。您可以通过查看存储项目.cpp文件的.sln文件来执行相同操作。如果没有,那么您将收到该错误。

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

添加 cpp 文件时,您要使用添加新项目图标。(左上角有一个金星,将鼠标悬停在它上面以查看名称)出于某种原因,Ctrl+N实际上并未将.cpp文件添加到项目中。

回答by ReturnVoid

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)

项目属性 -> 链接器 -> 输出文件 -> $(OutDir)$(TargetName)$(TargetExt)

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

它以前指向一个文件夹,MSVS 在调试模式下没有运行。

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

编辑:当我发布这个时,我遇到了:无法启动“program.exe”系统找不到指定的 vs2008 文件,它解释了同样的事情。

回答by Chris

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:

无论出于何种原因,这个初始更正并没有解决我的问题(我正在使用 VS2015 社区来构建 C++ 程序)。如果您仍然收到错误消息,请尝试以下附加步骤:

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

除了@ReturnVoid 发布的内容之外,对我来说并不需要这些额外的步骤,但是......有效的就是有效的......希望它也能帮助其他人。谢谢@ReturnVoid

回答by quick-

I have recently not used VS 2010.

我最近没有使用 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>Settingsto get a Build' menu.
After clicking Build->Build Solution(or Rebuild), you may verify in Outputwindow (View->Outout), if your application is compiling and linking correctly.

您的应用程序真的构建正确吗?要获得更多控制权VS 2010 C++ Express,您可以检查菜单项“ Expert Settings”下Tools>Settings以获取Build' menu.
单击Build->Build Solution(或Rebuild)后,您可以在Output窗口(View->Outout)中验证您的应用程序是否正确编译和链接。

Sources :

来源:

Hope it helps.

希望能帮助到你。

回答by Ethan Bierlein

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 shouldbe produced when you build your project.

我知道这是一个旧线程,但对于任何未来的访问者,此错误的原因很可能是因为您尚未从Build > Build Solution. 尝试运行项目时出现此错误的原因是 Visual Studio 找不到生成项目时应生成的可执行文件。

回答by Michele

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".

转到VS左侧的解决方案资源管理器,右键单击您的单元测试项目,然后选择“设置为启动项目”。

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.

我刚刚将我的代码移植到一个新的工作区,忘记了当我在那里的解决方案中的 VS 中打开项目时,我需要重新设置我的启动项目。

回答by rtpax

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

我遇到了这个问题,但这些解决方案都没有 100% 有效

In addition to ReturnVoid's answer which suggested the change

除了建议更改的 ReturnVoid 的回答之外

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

项目属性 -> 链接器 -> 输出文件 -> $(OutDir)$(TargetName)$(TargetExt)

I needed to changed

我需要改变

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

项目属性 -> C/C++ -> 调试信息格式 -> /Zi

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

这个字段对我来说是空白的,将内容更改为/Zi/Z7或者/ZI如果这些是您要使用的格式)允许我进行调试

回答by Windows65

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

我知道这个线程已有 1 年历史,但我希望这对某人有所帮助,我的问题是我需要添加:

    #include "stdafx.h"

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

对于我的项目(在第一行),大多数情况下似乎都是这种情况!

回答by misty

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

我在调试模式下遇到了这个问题,丢失的文件来自我正在使用的静态库。问题是通过在调试过程中使用step over而不是step into来解决的

回答by ANJi

if vs2010 installed correctly

如果 vs2010 安装正确

check file type (.cpp)

检查文件类型 (.cpp)

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

再次构建它会自动修复,(如果您使用的是 VS 2010)