views:

58

answers:

4

Problem Description:

Occassionally when debugging, I get the following error. I'm using visual studio 2010:

1>------ Build started: Project: projectName, Configuration: Debug Win32 ------
1>LINK : fatal error LNK1104: cannot open file 'C:\Projects\projectName\Debug\projectName.exe'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Note that projectName is the name of my project. The error occurs when I debug, make changes, and debug again (after doing all of that, the above error shows up instead of running the program a second time).

Steps to replicate:

  1. Create a new empty c++ project, and add a file called "main.cpp" to the sources folder
  2. Copy the following code into main.cpp:

    int main(){
        return 0;
    }
    
  3. Click the green debug arrow button, and note the successful run of the program. Ensure it is closed and that the debugging session is over. Open the process explorer and ensure the exe for the project is no longer running (if it is, wait until it closes).
  4. Erase the contents of main.cpp and replace it with this code (or any other code that will compile properly which is different than the code used above):

    #include<iostream>
    int main(){
        std::cout<<"hello\n";
        return 0;
    }
    
  5. Click the green debug arrow button. Instead of running the program, the IDE will show the fatal LNK1104 error. You've now replicated the problem.

Any ideas on how to fix this?


Additional Details:

  • If I try to change the permissions or delete projectName.exe after the error has occured, an error popup shows up which says:

    You need permission to perform this action

    You require permission from the computer's administrator to make changes to this file

  • I am using windows 7.

  • The account I'm using is an admin account, but this issue also occurs exactly the same when I use a non-admin account.

  • For 2-3 minutes after the error occurs, I cannot rebuild or debug the project, but after approximately that amount of time, I am able to start at the beginning of the repro steps again.


UPDATE: BOUNTY

Anyone who can offer a solution that fixes the problem gets 100 rep :)

I've tried stopping all services, processes and applications that could be interfering with VC++ accessing the file, and the issue is still occuring. Also, running vc++ as an admin does not help.

A: 

I've never seen that happen when there wasn't a lock on the file. Do you have any profiling or testing tools that might still be holding on to it?

note: I wasn't able to repro that.

edit> Have you tried opening process explorer while the program is running (as opposed to task manager)? It'll show you if your exe is running in any other processes.

Have you checked for malware? I recently had a case where some malware that would bootstrap every process that ran on a machine, and task manager wasn't very informative.

SnOrfus
No, I don't. I've edited the question with some additional details.
Cam
By task manager I actually meant the process explorer :) I've edited accordingly. I'm pretty sure I don't have malware - I'll run another virus check though to be sure...
Cam
A: 

My guess would be that something has a lock on the file. For whatever reason, VS cannot open the file to write the output of compilation. As SnOrfus suggests, make sure some sort of profiling or testing tools aren't open. I would also try to wait a few seconds between finishing an execution of the program (debug or otherwise) before attempting to rebuild. It's possible that you're building so fast that the debugger still has a lock on the file when VS attempts to access it.

Chris Thompson
That's what I thought too initially. However what's weird is that after the error occurs, I still can't rebuild or anything for a few *minutes*. If it was a few (1-3) seconds, that would be different.
Cam
That's weird, I wonder if there is some odd case that is causing the debugger to hang for several minutes then time out...when you open task manager, can you identify any process that would appear to be the debugger?
Chris Thompson
A: 

You can use Process Explorer to see if any process has a handle open to that file, even if the executable itself isn't running. Go to Find -> Find Handle or DLL... and type in projectName.exe, and it will give you a list of all processes that have it open.

mkarasek
+1  A: 

This is most likely a bugfeature of the Windows Explorer.

Make sure that the .exe file is NOT selected/focused in the Windows explorer. On Vista i often get LNK1104-errors, when the executable file is selected in the Windows Explorer during linking.

If that does not help, check that no other program has "selected" the file.

EDIT: This program can show you which process has locked your file (the pages contains some links to other "unlock" tools aswell)

smerlin
Thanks, that program worked perfectly!
Cam