C++ 无法打开输出文件,权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6875403/
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-28 20:48:06  来源:igfitidea点击:

Cannot open output file, permission denied

c++compiler-errorscodeblockspermission-denied

提问by GarrickW

So I keep getting this error when trying to compile C++ code using CodeBlocks.

因此,在尝试使用 CodeBlocks 编译 C++ 代码时,我不断收到此错误。

cannot open output file [filename.exe] permission denied

无法打开输出文件 [filename.exe] 权限被拒绝

It only started today, and it's sporadic and inconsistent. It usually goes away if I shut CodeBlocks down and restart the project, but not always. Sometimes it even goes away when I just press F9 (build & run) several times. I always check Task Manager, but there are never any .EXEs running with my file's name, or anything related to it.

它今天才开始,而且是零星的和不一致的。如果我关闭 CodeBlocks 并重新启动项目,它通常会消失,但并非总是如此。有时,当我多次按 F9(构建并运行)时,它甚至会消失。我总是检查任务管理器,但从来没有任何 .EXE 以我的文件名或与之相关的任何内容运行。

I've also noticed that if this problem occurs and I then try to delete the .EXE manually or otherwise interact with it, Windows tells me I need administrator permission to do so (this is a private PC, and as far as I know I am an administrator on it).

我还注意到,如果出现此问题,然后我尝试手动删除 .EXE 或以其他方式与之交互,Windows 会告诉我我需要管理员权限才能这样做(这是一台私人 PC,据我所知我我是它的管理员)。

The first time it occurred, the only thing I had added to the code beyond what was in the previous version was a debugging report sent to cout - hardly anything arcane. It sometimes occurs even when no code was changed from a previous version.

第一次发生时,我在代码中添加的唯一内容是发送给 cout 的调试报告——几乎没有任何神秘的东西。即使没有从以前的版本更改代码,有时也会发生这种情况。

What could this be? It's not game-breaking, yet, but it's irritating and I'd rather it didn't get worse.

这可能是什么?这还不是破坏游戏,但它很烦人,我宁愿它没有变得更糟。

EDIT: This is old by now, but just in case anyone else is having similar problems, the workaround I currently use is just to consistently delete [filename.exe] before trying to run the code again. This avoids the problem, though it is annoying.

编辑:这已经很旧了,但以防万一其他人遇到类似问题,我目前使用的解决方法只是在尝试再次运行代码之前始终删除 [filename.exe]。这避免了这个问题,尽管它很烦人。

回答by willll

check that "filename.exe" is not running, I guess you are using Microsoft Windows, in that case you can use either Task Manager or Process Explorer : http://technet.microsoft.com/en-us/sysinternals/bb896653to kill "filename.exe" before trying to generate it.

检查“文件名.exe”没有运行,我想你正在使用微软的Windows,在这种情况下,您可以使用任务管理器或Process Explorer:http://technet.microsoft.com/en-us/sysinternals/bb896653到在尝试生成它之前杀死“filename.exe”。

回答by Sam

I have encountered the same problem you have. I found that it may have some relationship with the way you terminate your run result. When you run your code, whether it has a printout, the debugger will call the console which print a "Press any key to continue...". If you terminate the console by pressing key, it's ok; if you do it by click the close button, the problem comes as you described. When you terminate it in the latter way, you have to wait several minutes before you can rebuild your code.

我遇到了和你一样的问题。我发现它可能与您终止运行结果的方式有关。当你运行你的代码时,无论它是否有打印输出,调试器都会调用控制台打印“按任意键继续...”。如果您通过按键终止控制台,则可以;如果您通过单击关闭按钮来执行此操作,则会出现您所描述的问题。当您以后一种方式终止它时,您必须等待几分钟才能重新构建代码。

回答by Laiebi

I just had the same issue. And i experienced that it always happens when i run the programm and change some code without finishing the programm still running. After that the "cannot open ..." message appears.

我只是有同样的问题。而且我经历过,当我运行程序并更改一些代码而没有完成仍在运行的程序时,它总是会发生。之后出现“无法打开...”消息。

However i got rid of it by clicking the "Terminate" button at the very top-right side of the console window (red button) and after that "remove all terminated launches" (two x'es right next to the terminate button). This seems to close the running programm and everything works fine after :) hope this may help anyone

但是,我通过单击控制台窗口右上角的“终止”按钮(红色按钮)并在此之后“删除所有终止的启动”(终止按钮旁边的两个 x )来摆脱它。这似乎关闭了正在运行的程序,之后一切正常:) 希望这可以帮助任何人

回答by Laiebi

This error usually occurs when the IDE has a problem due to a crash or other failure and it still has a hold on the EXE, preventing the user (yourself) from overwriting / deleting the EXE during a rebuild.

当 IDE 由于崩溃或其他故障而出现问题时,通常会发生此错误,并且它仍然保留在 EXE 上,防止用户(您自己)在重建期间覆盖/删除 EXE。

回答by user2203361

Hello I realize this post is old, but here is my opinion anyway. This error arises when you close the console output window using the close icon instead of pressing "any key to continue"

你好,我知道这篇文章很旧,但无论如何这是我的意见。当您使用关闭图标而不是按“任意键继续”关闭控制台输出窗口时会出现此错误

回答by Jorgesys

The problem is related to Sam′s response:

问题与 Sam 的回应有关:

"have encountered the same problem you have. I found that it may have some relationship with the way you terminate your run result. When you run your code, whether it has a printout, the debugger will call the console which print a "Press any key to continue...". If you terminate the console by pressing key, it's ok; if you do it by click the close button, the problem comes as you described. When you terminate it in the latter way, you have to wait several minutes before you can rebuild your code."

”遇到了和你一样的问题。我发现这可能与你终止运行结果的方式有一定的关系。当你运行你的代码时,不管它是否有打印输出,调试器都会调用控制台打印“按任意key to continue..."。如果你按key来终止控制台,没关系;如果你通过点击关闭按钮来完成,问题就如你所描述的那样。当你以后一种方式终止它时,你必须等待在您可以重建代码之前几分钟。”

Avoid kill processes, and we have two choices, wait until the process release the .EXE file or this problem will be solved faster restarting the IDE.

避免杀死进程,我们有两个选择,等待进程释放 .EXE 文件,否则这个问题将更快地重新启动 IDE 解决。

回答by user2776580

I re-installed C::B in drive D, whereas my program files folder is in drive C

我在驱动器 D 中重新安装了 C::B,而我的程序文件文件夹在驱动器 C

I don't know the reason , but it works :)

我不知道原因,但它有效:)

回答by Chris Card

You can use process explorer from sysinternals to find which process has a file open.

您可以使用 sysinternals 的进程资源管理器来查找哪个进程打开了文件。

回答by Namratha

Try restarting your IDE. It worked for me. Although I tried to end the process in the task manager, the process never got killed.

尝试重新启动您的 IDE。它对我有用。尽管我试图在任务管理器中结束该进程,但该进程从未被终止。

回答by Firefish

I was having the same problem too and it was driving me crazy:

我也遇到了同样的问题,这让我发疯了:

Windows7 64 bit, MinGW, Eclipse, CDT

Windows7 64 位、MinGW、Eclipse、CDT

Permission denied errors, executable disappearing, ld.exe errors. It would work once, but never again. Statements like: "cannot open output file ***.exe: Permission denied"

权限被拒绝错误、可执行文件消失、ld.exe 错误。它会起作用一次,但不会再起作用。语句如:“无法打开输出文件 ***.exe:权限被拒绝”

Check your ANTIVIRUS Software. Once I turned off my Antivirus software in workspace then cleaned the workspace and started the project over again I never had the problem again.

检查您的 ANTIVIRUS 软件。一旦我在工作区中关闭了我的防病毒软件,然后清理了工作区并重新启动了项目,我就再也没有遇到过这个问题。