Visual studio ошибка lnk1168

The other answers to this question provide one-off, manual solutions to the problem. However, the root cause of the problem in many cases is that Visual Studio is leaving your previous debug session’s process running, or at least extant in a way that maintains a lock on the executable file. You can set Visual Studio to automatically kill such a process when you stop debugging. This would then typically prevent you from getting this error, as well as needing to take manual steps to rectify it.

To instruct Visual Studio to automatically kill the process when debugging stops, set the following option:

Tools—>Options—>Debugging—>General—>Automatically close the console when debugging stops

On Visual Studio 2022 this is very near the bottom of this list.

Personally, I will run a process under Visual Studio with F5 (Debug) which compiles and links the binary first if necessary.

Then the binary crashes, or hits a breakpoint, or I’m tired of waiting, so I will change the source then hit SHIFT-F5 F5. This is «Debug Stop» followed by «Debug» again. I just do that by reflex.

I’m not sure if this flag’s default value changes periodically between releases of Visual Studio, but I don’t recall ever having to set this option before. I generally create very few projects but work on them many years, so maybe I set this option manually every time then forget about it. But on 2022, it does need to be set. I’ve set it and now can work as I recall working in the past.

The other answers to this question provide one-off, manual solutions to the problem. However, the root cause of the problem in many cases is that Visual Studio is leaving your previous debug session’s process running, or at least extant in a way that maintains a lock on the executable file. You can set Visual Studio to automatically kill such a process when you stop debugging. This would then typically prevent you from getting this error, as well as needing to take manual steps to rectify it.

To instruct Visual Studio to automatically kill the process when debugging stops, set the following option:

Tools—>Options—>Debugging—>General—>Automatically close the console when debugging stops

On Visual Studio 2022 this is very near the bottom of this list.

Personally, I will run a process under Visual Studio with F5 (Debug) which compiles and links the binary first if necessary.

Then the binary crashes, or hits a breakpoint, or I’m tired of waiting, so I will change the source then hit SHIFT-F5 F5. This is «Debug Stop» followed by «Debug» again. I just do that by reflex.

I’m not sure if this flag’s default value changes periodically between releases of Visual Studio, but I don’t recall ever having to set this option before. I generally create very few projects but work on them many years, so maybe I set this option manually every time then forget about it. But on 2022, it does need to be set. I’ve set it and now can work as I recall working in the past.

The VC++ fatal error LNK1168 is a linking error that occurs when a program is unable to open the .exe file for writing. This error is often encountered during the building or debugging process of a VC++ project and can cause the build process to fail. There can be several reasons why this error might occur, including a locked or in-use .exe file, insufficient permissions, or a problem with the linker. In this article, we will explore some of the methods that can be used to resolve this error.

Method 1: Close the Application

To fix the VC++ fatal error LNK1168: cannot open filename.exe for writing, you can close the application that is currently using the file. Here are the steps to do it:

  1. Open Task Manager by pressing Ctrl+Shift+Esc.
  2. Switch to the Processes tab.
  3. Look for the process with the same name as the .exe file that you are trying to build.
  4. Select the process and click End Task.
  5. Try building the project again.

Here is an example code in C++:

#include <iostream>

int main()
{
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

Assuming that you are trying to build this code and getting the LNK1168 error, you can follow the steps above to close the application that is using the output file. Once the application is closed, you should be able to build the project successfully.

Note that this solution only works if the application that is using the file is not essential to your work. If you need the application to be running, you may need to find another solution, such as changing the output file name or location.

Method 2: Change the Build Output Directory

To fix the VC++ fatal error LNK1168: cannot open filename.exe for writing, you can change the Build Output Directory. Here are the steps:

  1. Open your project in Visual Studio.
  2. Right-click on your project in the Solution Explorer and select «Properties».
  3. In the left-hand menu, select «Configuration Properties» -> «General».
  4. In the «General» tab, find the «Output Directory» field and change it to a directory that you have write access to. For example, you can change it to «$(SolutionDir)bin$(Configuration)».
  5. Click «Apply» and then «OK» to save the changes.

Here is an example code snippet that shows how to change the Build Output Directory using CMake:

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

And here is an example code snippet that shows how to change the Build Output Directory using MSBuild:

<PropertyGroup>
  <OutputPath>$(SolutionDir)bin\$(Configuration)\</OutputPath>
</PropertyGroup>

By changing the Build Output Directory, you should be able to fix the VC++ fatal error LNK1168: cannot open filename.exe for writing.

Method 3: Delete the .exe File

To fix the VC++ fatal error LNK1168: cannot open filename.exe for writing, you can try deleting the .exe file. Here are the steps to do it:

  1. Open the folder where your project is saved.
  2. Look for the .exe file with the same name as your project.
  3. Right-click on the .exe file and select «Delete».
  4. Confirm the deletion by clicking «Yes» on the pop-up window.
  5. Rebuild your project.

Here is an example code in C++:

#include <iostream>

int main() {
  std::cout << "Hello World!";
  return 0;
}

In this example, the name of the project is «HelloWorld». So, the .exe file will be named «HelloWorld.exe». If you encounter the VC++ fatal error LNK1168: cannot open HelloWorld.exe for writing, you can follow the steps above to delete the file and rebuild the project.

Note: Make sure to save your work before deleting any files.

Method 4: Check File and Folder Permissions

If you encounter the error «fatal error LNK1168: cannot open filename.exe for writing» while trying to build your VC++ project, it means that the linker is unable to write the output file because it is already in use or does not have the necessary permissions.

One way to fix this issue is to check the file and folder permissions of the output file and its parent directory. Here’s how to do it:

Step 1: Open the Properties window of the output file

In the Solution Explorer, right-click on the output file (usually with the extension .exe) and select «Properties» from the context menu.

Step 2: Check the Read-only attribute

In the Properties window, go to the «General» tab and check if the «Read-only» attribute is enabled. If it is, uncheck it and click «Apply» to save the changes.

Step 3: Check the Security tab

In the Properties window, go to the «Security» tab and click on the «Edit» button. This will open the Permissions dialog box.

Step 4: Check the permissions

In the Permissions dialog box, make sure that the user account you are using to build the project has the necessary permissions to write to the output file and its parent directory. If not, click on the «Add» button to add the user account and grant it the necessary permissions.

Example code

Here’s an example code that sets the file and folder permissions of the output file and its parent directory:

#include 

int main()
{
    // Set the file and folder permissions of the output file and its parent directory
    const wchar_t* filename = L"C:\\Path\\To\\Output\\File.exe";
    const wchar_t* dirname = L"C:\\Path\\To\\Output";

    // Get the security descriptor of the file
    PSECURITY_DESCRIPTOR sd = NULL;
    GetNamedSecurityInfoW(filename, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, NULL, NULL, &sd);

    // Set the security descriptor of the file's parent directory
    SetNamedSecurityInfoW(dirname, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, NULL, sd, NULL);

    // Set the security descriptor of the file
    SetNamedSecurityInfoW(filename, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, NULL, sd, NULL);

    // Free the security descriptor
    LocalFree(sd);

    return 0;
}

This code uses the Windows API functions GetNamedSecurityInfoW and SetNamedSecurityInfoW to get and set the security descriptor of the output file and its parent directory. The SE_FILE_OBJECT parameter specifies that the object is a file, and the DACL_SECURITY_INFORMATION parameter specifies that the discretionary access control list (DACL) of the security descriptor is being set or retrieved.

Note that this code should be run with administrative privileges to have the necessary permissions to modify the file and folder permissions.

That’s it! By checking the file and folder permissions of the output file and its parent directory, you should be able to fix the «fatal error LNK1168: cannot open filename.exe for writing» error in your VC++ project.

Method 5: Reinstall Visual Studio

If you are facing the error «LNK1168: cannot open filename.exe for writing» in Visual Studio, one possible solution is to reinstall Visual Studio. Here are the steps to do so:

  1. Uninstall Visual Studio: Go to Control Panel > Programs and Features, select Visual Studio, and click Uninstall.

  2. Download the latest version of Visual Studio: Go to the Visual Studio website and download the latest version of Visual Studio.

  3. Install Visual Studio: Run the Visual Studio installer and follow the instructions to install Visual Studio.

  4. Create a new project: Open Visual Studio and create a new project.

  5. Build the project: Build the project by clicking Build > Build Solution.

  6. Run the project: Run the project by clicking Debug > Start Debugging.

Here is an example code to create a simple «Hello World» application:

#include 
using namespace std;

int main() {
    cout << "Hello World!" << endl;
    return 0;
}

This code will output «Hello World!» to the console when run.

That’s it! Reinstalling Visual Studio should fix the «LNK1168: cannot open filename.exe for writing» error.

  • Remove From My Forums
  • Вопрос

  • When I first compile and link a solution, everything works fine. However, often (about 60 — 70% of the time), subsequent link operations fail unable to open the .exe file for writing. I’ve checked the obvious things such as the application still running and even trying to delete the existing file via explorer fails saying the file is in use.

    If I wait long enough — sometimes up to 5 minutes — then all works well again. I haven’t worked out when this happens. It usually happens if I encounter an unhandled exception during a test run, but sometimes I get this even when the test run was clean.

    Any ideas?

Ответы

I was building a c++ project when Visual Studio gave me an error saying the main file cannot be opened for writing, even though my program does not write to any files.

Severity    Code    Description Project File    Line
Error   LNK1168 cannot open C:\Users\Nathan\documents\visual studio 2015\Projects\c++thing\Debug\c++thing.exe for writing   c++thing    C:\Users\Nathan\documents\visual studio 2015\Projects\c++thing\c++thing\LINK    1

asked Dec 23, 2015 at 20:43

guest1337's user avatar

4

Visual Studio (VS) 2015 is not managing to overwrite your application named «c++thing.exe».

It must got hanged while running by VS debugger (possibly on a windows 10 PC), and this debugger lose itself from main VS.

If SHIFT+F5 doesn’t unlock the file (and terminate the process):

  • try restarting VS
  • [OR] terminate VS and then check the running processes list for «c++thing.exe» and kill it if still existing.

answered Oct 10, 2016 at 16:03

Pedro Reis's user avatar

Pedro ReisPedro Reis

1,5871 gold badge19 silver badges19 bronze badges

Понравилась статья? Поделить с друзьями:
  • Visual studio ошибка c1083
  • Visual studio ошибка c1010
  • Vitesse vs 514 ошибка e0
  • Vitek vt 1198 st ошибка e2
  • Vitashell произошла ошибка c1 6775 5