Visual Studio: LINK : fatal error LNK1181: cannot open input file

C++Visual StudioCompiler Construction

C++ Problem Overview


I've been encountering a strange bug in Visual Studio 2010 for some time now.

I have a solution consisting of a project which compiles to a static library, and another project which is really simple but depends on this library.

Sometimes, in the last days extremely frequent, after Rebuilding the Solution or just compiling it with 1-3 changed source files, I get the following error:

2>LINK : fatal error LNK1181: cannot open input file 'thelibrary.lib'
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

Where compiling thelibrary.lib was a success without any errors or warnings.

I have tried cleaning the solution, but that doesn't always work.

  • What is wrong here?

C++ Solutions


Solution 1 - C++

In Linker, general, additional library directories, add the directory to the .dll or .libs you have included in Linker, Input. It does not work if you put this in VC++ Directories, Library Directories.

Solution 2 - C++

I can see only 1 things happening here: You did't set properly dependences to thelibrary.lib in your project meaning that thelibrary.lib is built in the wrong order (Or in the same time if you have more then 1 CPU build configuration, which can also explain randomness of the error). ( You can change the project dependences in: Menu->Project->Project Dependencies )

Solution 3 - C++

Go to:

Project properties -> Linker -> General -> Link Library Dependencies set No.

Solution 4 - C++

I recently hit the same error. Some digging brought up this: http://support.microsoft.com/kb/815645

Basically, if you have spaces in the path of the .lib, that's bad. Don't know if that's what's happening for you, but seems reasonably possible.

The fix is either 1) put the lib reference in "quotes", or 2) add the lib's path to your Library Directories (Configuration Properties >> VC++ Directories).

Solution 5 - C++

I had the same issue in both VS 2010 and VS 2012. On my system the first static lib was built and then got immediately deleted when the main project started building.

The problem is the common intermediate folder for several projects. Just assign separate intermediate folder for each project.

Read more on this here

Solution 6 - C++

I solved it with the following:

Go to View-> Property Pages -> Configuration Properties -> Linker -> Input

Under additional dependencies add the thelibrary.lib. Don't use any quotations.

Solution 7 - C++

I had a similar problem in that I was getting LINK1181 errors on the .OBJ file that was part of the project itself (and there were only 2 .cxx files in the entire project).

Initially I had setup the project to generate an .EXE in Visual Studio, and then in the Property Pages -> Configuration Properties -> General -> Project Defaults -> Configuration Type, I changed the .EXE to .DLL. Suspecting that somehow Visual Studio 2008 was getting confused, I recreated the entire solution from scratch using .DLL mode right from the start. Problem went away after that. I imagine if you manually picked your way through the .vcproj and other related files you could figure out how to fix things without starting from scratch (but my program consisted of two .cpp files so it was easier to start over).

Solution 8 - C++

I'm stumbling into the same issue. For me it seems to be caused by having 2 projects with the same name, one depending on the other.

For example, I have one project named Foo which produces Foo.lib. I then have another project that's also named Foo which produces Foo.exe and links in Foo.lib.

I watched the file activity w/ Process Monitor. What seems to be happening is Foo(lib) is built first--which is proper because Foo(exe) is marked as depending on Foo(lib). This is all fine and builds successfully, and is placed in the output directory--$(OutDir)$(TargetName)$(TargetExt). Then Foo(exe) is triggered to rebuild. Well, a rebuild is a clean followed by a build. It seems like the 'clean' stage of Foo.exe is deleting Foo.lib from the output directory. This also explains why a subsequent 'build' works--that doesn't delete output files.

A bug in VS I guess.

Unfortunately I don't have a solution to the problem as it involves Rebuild. A workaround is to manually issue Clean, and then Build.

Solution 9 - C++

I don't know why, but changing the Linker->Input->Additional Dependencies reference from "dxguid.lib" to "C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86\dxguid.lib" (in my case) was the only thing that worked.

Solution 10 - C++

Maybe you have a hardware problem.

I had the same problem on my old system (AMD 1800 MHz CPU ,1GB RAM ,Windows 7 Ultimate) ,until I changed the 2x 512 MB RAM to 2x 1GB RAM. Haven't had any problems since. Also other (minor) problems disappeared. Guess those two 512 MB modules didn't like each other that much ,because 2x 512 MB + 1GB or 1x 512 MB + 2x 1GB didn't work properly either.

Solution 11 - C++

For me the problem was a wrong include directory. I have no idea why this caused the error with the seemingly missing lib as the include directory only contains the header files. And the library directory had the correct path set.

Solution 12 - C++

You can also fix the spaces-in-path problem by specifying the library path in DOS "8.3" format.

To get the 8.3 form, do (at the command line):

DIR /AD /X

recursively through every level of the directories.

Solution 13 - C++

I had the same problem. Solved it by defining a macro OBJECTS that contains all the linker objects e.g.:

OBJECTS = target.exe kernel32.lib mylib.lib (etc)

And then specifying $(OBJECTS) on the linker's command line.

I don't use Visual Studio though, just nmake and a .MAK file

Solution 14 - C++

I had the same error when running lib.exe from cmd on Windows with a long argument list. apparently cmd.exe has max line length of about 8K characters, which resulted that the filenames at the end of this threshold got changed, thus resulting in bad filename error. my solution was to trim the line. I removed all paths from filenames and added single path using /LIBPATH option. for example:

/LIBPATH:absolute_path /OUT:outfilename filename1.obj filename2.obj ... filenameN.obj

Solution 15 - C++

I found a different solution for this...

Actually, I missed comma separator between two library paths. After adding common it worked for me.

Go to: Project properties -> Linker -> General -> Link Library Dependencies At this path make sure the path of the library is correct.

Previous Code (With Bug - because I forgot to separate two lib paths with comma):

<Link><AdditionalLibraryDirectories>..\..\Build\lib\$(Configuration)**..\..\Build\Release;**%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>

Code after fix (Just separate libraries with comma):

<Link><AdditionalLibraryDirectories>..\..\Build\lib\$(Configuration)**;..\..\Build\Release;**%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>

Hope this will help you.

Solution 16 - C++

In my case I had the library installed using NuGet package (cpprestsdk) AND I falsely added the lib to the Additional Dependancies in the Linker settings. It turns out, the package does it all for you.

The linker then tried to find the library in the library path and of course could not find it.

After removing the library from the Additional Dependencies everything compiled and linked fine.

Solution 17 - C++

Not quite the answer to OP's question as I am using CMake with Visual Studio as a generator but I personally also just encountered the same issue (I am using Visual Studio toolchain, but not the IDE to build stuff).

My fix was target linking the directory of the directory of the libraries (I had a few) before target linking the library.

//Works
target_link_directories(MyExe PRIVATE /out/of/scope/path/to/lib)
foreach(X IN LISTS LIBSLISTNAMES)
    target_link_libraries(MyExe ${X})
endforeach()

//Throws cannot open cannot open input file error
 foreach(X IN LISTS LIBSLISTNAMES)
    target_link_libraries(MyExe /out/of/scope/path/to/lib/${X})
endforeach()

Not sure what is happening under the hood, but maybe VS IDE has equivalent setting somewhere?

Solution 18 - C++

I've also experienced this problem. For me the dependencies were properly set, but one of the projects in my solution wasn't selected for building in the configuration (VS 2022 pro).

I eventually figured out thanks to output in Build -> Clean Solution that mentioned one of the project in dependency chain being disabled. Interesingly enough, when trying to build the disabled project it wouldd not properly build its dependencies.

Solution 19 - C++

I created a bin directory at the project_dir level, then created a release/debug directory inside the bin folder, which solved the problem for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKomnView Question on Stackoverflow
Solution 1 - C++Chris ThorneView Answer on Stackoverflow
Solution 2 - C++SashaView Answer on Stackoverflow
Solution 3 - C++EkaYudaView Answer on Stackoverflow
Solution 4 - C++ClippyView Answer on Stackoverflow
Solution 5 - C++alexkrView Answer on Stackoverflow
Solution 6 - C++user2049230View Answer on Stackoverflow
Solution 7 - C++user1726157View Answer on Stackoverflow
Solution 8 - C++NickView Answer on Stackoverflow
Solution 9 - C++VicView Answer on Stackoverflow
Solution 10 - C++engf-010View Answer on Stackoverflow
Solution 11 - C++mgttlingerView Answer on Stackoverflow
Solution 12 - C++PierreView Answer on Stackoverflow
Solution 13 - C++Martin van RijenView Answer on Stackoverflow
Solution 14 - C++Haim ItzhayekView Answer on Stackoverflow
Solution 15 - C++ShubhamView Answer on Stackoverflow
Solution 16 - C++Bojan HrnkasView Answer on Stackoverflow
Solution 17 - C++Curious_StudentView Answer on Stackoverflow
Solution 18 - C++BarnabaView Answer on Stackoverflow
Solution 19 - C++Ravi SohalView Answer on Stackoverflow