Search code examples
cvisual-studiovisual-studio-2012fortranstatic-linking

Visual Studio inconsistently generates .lib


I have a Visual Studio 2012 solution with multiple projects, but I am only concerned with two at present. Project A is a static library written in C, and project B is an executable written in C. Project B depends on project A, and that is specified in the Project Dependencies settings.

The problem is that if I rebuild project B, I get error LNK1104: cannot open file 'projectA.lib'. However, if I rebuild project A first, and then build project B, there is no problem.

Oddly enough, the rebuild of project B does not actually create projectA.lib as I would expect, despite that I see this line in the Output window:

2> projectA.vcxproj -> C:\<build_location>\projectA.lib

I also see that line when I rebuild project A, and in that case it does actually get created. Why is the file not generated if I just rebuild project B, and how can I fix that?

EDIT:

I've discovered that this problem also exists for two other projects with a similar relationship. Let's call them project C (a static library) and project D (an executable). Both are written in Fortran and compiled with Intel Composer XE 2013. D depends on C. Again, rebuilding D does not create C as one would expect, but building C works fine. (Unlike A, it does not have to be rebuilt, just built.)

It is also worth noting that B also depends on C but that doesn't cause a problem. It's only when they have the same language that I see the problem.


Solution

  • The problem was that I had managed to put all four projects into the same directory. Apparently, certain parts of the build process use metafiles or temporary files that use compiler-specific names instead of project-specific names, and having multiple projects using the same compiler in one directory means that those files get reused and overwritten.

    The solution to the problem is to move each project to its own directory. You can get away with keeping one C and one Fortran project together in one directory, but if you have to start making new directories like I did, you may as well make one for each project.