# MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running)

**Code:** `LNK1104` · **Category:** C/C++ · **Seen on:** C/C++, MSVC, Windows

The Visual C++ linker could not open an input library or the output file — the path is wrong, the library is missing, or the .exe is still running or locked.

## Meaning

Two different situations share this code: an input (`.lib`, `.obj`) that cannot be found in the library search paths, or an output (`.exe`, `.dll`, `.pdb`) that cannot be written because a debugger, a running instance, an antivirus scan or a sync tool holds it open. The file name in the message tells which one it is.

## Common causes

- Library directory not listed in Linker → General → Additional Library Directories
- Library name or `.lib` extension misspelled, or only the DLL is present
- Mixing 32-bit and 64-bit library folders
- Output `.exe` still running or locked by antivirus / OneDrive
- Path contains an unexpanded macro or invalid characters

## Quick fix

1. Add the folder under Additional Library Directories and the file name under Additional Dependencies
2. Close the running program (Task Manager) and rebuild
3. Use matching platform folders (x64 vs Win32) for the library and the project

## Detailed fix by platform

### Visual Studio

1. Project Properties → Linker → General → Additional Library Directories; Linker → Input → Additional Dependencies (`foo.lib`)
2. Make sure Configuration (Debug/Release) and Platform (x64/Win32) match the library build

### Windows

1. Find a locked output: `tasklist | findstr app.exe` then `taskkill /IM app.exe /F`

## Code examples

### Library path in CMake

```cmake
find_library(FOO_LIB NAMES foo PATHS "C:/libs/foo/lib/x64")
if(NOT FOO_LIB)
  message(FATAL_ERROR "foo.lib not found")
endif()
target_link_libraries(app PRIVATE ${FOO_LIB})
```

## How to diagnose

1. **File** — Library to read or output to write?
2. **Path** — Is the folder on the library search path?
3. **Platform** — Do x64/Win32 and Debug/Release match?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/msvc-lnk1104-cannot-open-file)
