Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 28, 2017 4:24 PM
THE PROBLEM
I build the following code, copied from the visual studio tutorial online.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!\n";
return 0;
}
It works. I remove the ";" from the line which begins "cout", attempt to build, it fails, giving error code 2: fine. I add the ";" back in to the proper place, to correct the error, attempt to build, but it fails again, giving code 1168 "LINK : fatal error LNK1168: cannot open C:\Users\..\source\repos\HelloWorld\Debug\HelloWorld.exe for writing".
To work around, I can close Visual Studio and reopen, but this is a pain.
To add a bit more: the console window is definitely closed, but if i look in task manager, "HelloWorld.exe" is still running under "background processes", and sometimes multiple times if i have attempted to build multiple times.
Am I making a simple error, or is something else wrong? I'd be grateful for any advice.
Not sure if this info requested for bug reports is useful or necessary, but here it is.
TOOLSET VERSION
C:\Users\..\source>cl /Bv /CLR
Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25508.2
for Microsoft (R) .NET Framework version 4.07.2110.0
Copyright (C) Microsoft Corporation. All rights reserved.
Compiler Passes:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\cl.exe: Version 19.11.25508.2
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\c1.dll: Version 19.11.25508.2
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\c1xx.dll: Version 19.11.25508.2
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\c2.dll: Version 19.11.25508.2
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\link.exe: Version 14.11.25508.2
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\mspdb140.dll: Version 14.11.25508.2
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\1033\clui.dll: Version 19.11.25508.2
Common Language Runtime: Version 4.07.2110.0
cl : Command line error D8003 : missing source filename
COMMAND LINE
^C:\USERS\..\SOURCE\REPOS\HELLOWORLD\HELLOWORLD\HELLOWORLD.CPP
/c /ZI /nologo /W3 /WX- /diagnostics:classic /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Yu"stdafx.h" /Fp"DEBUG\HELLOWORLD.PCH" /Fo"DEBUG\" /Fd"DEBUG\VC141.PDB" /Gd /TP /analyze- C:\USERS\..\SOURCE\REPOS\HELLOWORLD\HELLOWORLD\HELLOWORLD.CPP
All replies (7)
Saturday, September 30, 2017 9:49 AM ✅Answered | 1 vote
Have you tried just doing a repair install of Visual Studio?
It is obvious that Visual Studio is keeping the process open. Since you have tested the AV side of things then really it can only be the Visual Studio debugger itself.
This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
Thursday, September 28, 2017 5:57 PM | 1 vote
Maybe it is caused by too rigorous or inefficient antivirus?
Friday, September 29, 2017 7:48 AM
Tried disabling my anti-virus temporarily and the issue remained, unfortunately. Thanks for the suggestion though.
Friday, September 29, 2017 7:54 AM | 1 vote
Hi,
Welcome to MSDN forum.
Linker Tools Error LNK1168: The linker can’t write to filename. The file may be in use and its file handle locked by another process, or you may not have write permission for the file, or for the directory or network share it is located in. This error is often caused by a transient condition—for example, a lock held by an anti-virus program, a file search indexing process, or a delay in releasing a lock held by the Visual Studio build system.
Close all instance of visual studio including HelloWorld.exe in task manager, if it is an executable, verify that it is not already running and reopen visual studio to check if the issue persists. If you still meet the error, disable any Anti-Virus or Anti-Spyware software on your computer firstly and make sure you have write permission for the file with the current user.
You can use the Windows SysInternals utilities Handle or Process Explorer to determine which process has a file handle lock on filename.
The more information, please have a look at /en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk1168
Best regards,
Joyce
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Friday, September 29, 2017 9:09 AM
Thanks for the reply.
Firstly I'll repeat that closing Visual Studio and reopening does fix the problem, until there is another build error, of course. I'll add that closing only the solution and reopening does not fix it.
Disabling my anti-virus while going through the whole process described above (opening Visual Studio, successfully building the correct code, deleting the ";", unsuccessfully attempting to build, then adding the ";" back in, and still successfully attempting to build) does not fix the problem.
If i try to end the "HelloWorld.exe" process within Task Manager, with Visual studio still open, it doesn't end. There is no error message, or message of any kind: it simply stays there. If I try to kill the process using Process Explorer, it gives an error message "Error terminating process: Access is denied." Here is a picture of Process Explorer, if that helps (image link: https://ibb.co/gTboBG).
I've checked the permission of the folder and the HelloWorld.exe file, and I have all permissions. This is my personal PC, so I have permissions for everything. Can't think why there would be an issue here.
Also, after failing to build, I can wait quite a while (10 mins+), and the same error (1168) appears, and I am still unable to build, so it doesn't appear to be temporary. Only restarting Visual Studio solves the problem.
Think I covered everything, but let me know if I missed something.
Wednesday, October 4, 2017 4:19 PM
Sorry for taking a while to get back. I had assumed that this couldn't be a solution, as I had only installed Visual Studio a day or two before this for the first time, and had barely done anything on it. However, I am reminded that one should avoid unnecessary assumptions, as this turned out to fix the problem.
Thanks for the suggestion. Problem solved.
Sunday, December 10, 2017 3:58 AM
I had similar issues with an additional twist. On launching the application in the debugger after an initial build I would get "Unable to start program/Access denied". From this point forward Visual Studio was clearly holding onto the process as I could neither rebuild, delete the existing binary manually, nor close the process in Task Manager.
I noticed your compiler settings. In my case I was able to work around the problem by changing the compiler setting /ZI to /Zi. This came at the cost of losing Edit and Continue but at least the debugger would not lock up the process and I could rebuild. I haven't sorted out the underlying cause since /ZI causes no problems on another project. I am currently using Visual Studio Community 2017 v15.5.0.