94

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?
Jonas
130k103 gold badges330 silver badges408 bronze badges
asked Jun 23, 2011 at 8:33
3
  • 7
    I have exactly the same issue. Solution with 3 native c++ projects. 1 exe and 2 static libs. Always getting mentioned error on Rebuild. After that I just do Build and it goes ok. Look like a bug. Commented Jan 15, 2012 at 20:07
  • 1
    I have the same issue. I type only 'abc' instead of 'abc.lib'. After correct it, everything is done. Commented Nov 25, 2016 at 7:59
  • In my case it needed a debug version of python, see the solution here Commented Aug 27, 2024 at 14:55

21 Answers 21

47

Right click on project Options > 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.

Yousuf Azad
4251 gold badge7 silver badges17 bronze badges
answered Jun 25, 2012 at 2:19
Sign up to request clarification or add additional context in comments.

Comments

14

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 )

answered Jun 23, 2011 at 8:51

4 Comments

Thelibrary.lib is always built first. I have correctly set the depencies, long ago.
Well in this case it may be a VS bug. Try to find which process keeps the thelibrary.lib opened.
To clarify, you need to right-click on your project, go to "Build Dependencies" -> "Project Dependencies"
My project was missing many <files>.lib "LINK : fatal error LNK1181: cannot open input file <file>.lib"
10

Go to:

Project properties -> Linker -> General -> Link Library Dependencies set No.
user
87.1k18 gold badges200 silver badges190 bronze badges
answered May 29, 2012 at 11:50

2 Comments

And what if I actually need the dependent library linked in? (Isn't that the main reason to specify the lib as a dependency in the first place)
You can add the dependent library explicitly in the 'Linker->Input' section.
9

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).

answered Nov 11, 2011 at 13:05

2 Comments

Thank you for your solution, I did face these kind of problems and what you mentioned is right.If we have spaces in the library search path it will fail to detect the libraries. I am using visual studio 2013
@Clippy I have encountered the same when I upgraded the visual studio. I literally wanted to cry when I found this solution.
5

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

answered Sep 29, 2013 at 10:50

2 Comments

This happened to me too when I was duplicating projects for a new solution of similar projects. Please note, you can still use the same output folder for your binaries, just not the same intermediate folder.
Looks as if link has moved on.
3

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).

Vikdor
24.2k10 gold badges66 silver badges86 bronze badges
answered Oct 7, 2012 at 4:43

Comments

3

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.

answered Feb 7, 2013 at 3:08

Comments

2

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.

answered Apr 25, 2013 at 5:26

Comments

2

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.

answered Dec 3, 2013 at 7:56

Comments

1

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.

answered Jun 24, 2011 at 8:09

Comments

1

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.

answered Oct 17, 2014 at 12:54

Comments

0

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.

answered Jun 4, 2016 at 22:15

Comments

0

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

0xdb
3,7071 gold badge23 silver badges39 bronze badges
answered Nov 4, 2017 at 16:47

Comments

0

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
answered Nov 1, 2018 at 14:31

Comments

0

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.

Ehsan Mohammadi
1,3261 gold badge17 silver badges22 bronze badges
answered May 8, 2019 at 11:32

Comments

0

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.

answered May 10, 2019 at 11:35

Comments

0

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?

answered Jun 17, 2021 at 0:36

Comments

0

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.

answered Feb 14, 2022 at 15:14

Comments

0

If the .lib is generated from another project of your visual studio solution, here is the answer (that works for me) :

  1. right click on the project that use the .lib : build dependencies > Project dependencies...

  2. then check the box related to the project that generate the .lib file.

This way you make sure that the .lib is compiled before the project that use it.

answered May 29, 2024 at 14:42

Comments

-1

In the solution were two projects A and B. Building B requires A.lib, and somewhere along the line, the solution was cleaned. Later, for testing purposes it was desired to build B alone, and the error was mistakenly taken as

cannot open input file 'B.lib'

instead of what it actually was

cannot open input file 'A.lib'

So burning the candles at both ends in order to comprehend why the project B build process would delete its own library!

answered Feb 23, 2023 at 5:21

Comments

-2

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.

Matt
75.4k26 gold badges156 silver badges181 bronze badges
answered Aug 19, 2013 at 17:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.