[c++] VC++ fatal error LNK1168: cannot open filename.exe for writing

Suddenly, my Visual Studio Express 2010 C++ stopped rebuilding my project. When I first hit F7 the project builds and runs fine (heck, it's a hello world example).

Then I make some changes and hit F7 again, then I get:

1>LINK : fatal error LNK1168: cannot open C:\Users\username\Documents\Visual Studio 2010\Projects\console\Debug\console.exe for writing**<br><br>

Now the funny thing comes:

  1. The app is not running and it's not shown in task manager.
  2. Going into the project directory and trying to remove it with hands comes with success but the file APPEARS AGAIN FROM NOWHERE.
  3. The system restore is disabled.
  4. I also tried to take the ownership of the whole damn drive.
  5. Every time I delete the file it recreates itself again but sometimes it stops doing that.
  6. If I delete the file (and it recreates after that), and then I start sysinternals procmon then the file dissapears.
  7. If I start procmon before - then the file keeps appearing after delete like always.

OS: W7 SP1 64-bit, with latest updates

Any ideas, please?

This question is related to c++ windows visual-c++

The answer is


The Reason is that your previous build is still running in the background. I solve this problem by following these steps:

  • Open Task Manager
  • Goto Details Tab
  • Find Your Application
  • End Task it by right clicking on it
  • Done!

In my case, cleaning and rebuilding the project resolved the problem.


well, I actually just saved and closed the project and restarted VS Express 2013 in windows 8 and that sorted my problem.


I've encountered this problem when the build is abruptly closed before it is loaded. No process would show up in the Task Manager, but if you navigate to the executable generated in the project folder and try to delete it, Windows claims that the application is in use. (If not, just delete the file and rebuild, which generates a new executable) In Windows(Visual Studio 2019), the file is located in this directory by default:

%USERPROFILE%\source\repos\ProjectFolderName\Debug

To end the allegedly running process, open the command prompt and type in the following command:

taskkill /F /IM ApplicationName.exe

This forces any running instance to be terminated. Rebuild and execute!


FINALLY THE BEST WAY WORKED PERFECTLY FOR ME

None of the solutions in this page worked for me EXCEPT THE FOLLOWING

Below the comment sections of the second answer, try the following :

Adding to my above comment, Task Manager does not display the filename.exe process but Resource Monitor does, so I'm able to kill it from there which solves the issue without having to reboot. – A__ Jun 19 '19 at 21:23


I also had this same issue. My console window was no longer open, but I was able to see my application running by going to processes within task manager. The process name was the name of my application. Once I ended the process I was able to build and compile my code with no issues.


I know this is an old question but thought I'd share how I resolved the issue.

If you're using Visual Studio and this error occurs, you can try to attach to process (CTRL+ALT+P) and find the "(program).exe" process. When you try to attach to it, an error will display stating that it failed to attach which removes the process from "running" (even though it's not...) You'll also be able to delete the (program).exe from your Debug folder.

Hope this helps someone! :)


The problem is probably that you forgot to close the program and that you instead have the program running in the background.

Find the console window where the exe file program is running, and close it by clicking the X in the upper right corner. Then try to recompile the program. In my case this solved the problem.

I know this posting is old, but I am answering for the other people like me who find this through the search engines.


Start your program as an administrator. The program can't rewrite your files cause your files are in a protected location on your hard drive.


This can also be a problem from the improper use of functions like FindNextFile when a FindClose is never executed. The process of the built file is terminated, and the build itself can be deleted, but LNK1168 will prevent a rebuild because of the open handle. This can create a handle leak in Explorer which can be addressed by terminating and restarting Explorer, but in many cases an immediate reboot is necessary.


Restarting Visual Studio solved the problem for me.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to visual-c++

How to install Visual C++ Build tools? Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat) How to install numpy on windows using pip install? How to use _CRT_SECURE_NO_WARNINGS How to play or open *.mp3 or *.wav sound file in c++ program? What is C# equivalent of <map> in C++? The program can't start because MSVCR110.dll is missing from your computer error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj Identifier is undefined error LNK2001: unresolved external symbol (C++)