Search code examples
c++compilationlinkerstatic-libraries

LINK : fatal error LNK1104: cannot open file 'library.lib'


My original source code is a more complex, but I reduced it to one header.hxx and one impl.cxx file, because It's a question about MS Visual Studio link.exe.

I wrote a batch for a static library building. Here are the commands I used:

call "c:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat" x64 10.0.22621.0
cl /c /EHsc /nologo /W3 /D "_DEBUG" impl.cxx
link /OUT:"library.lib" /DEBUG /SUBSYSTEM:CONSOLE /NOLOGO /MACHINE:X64 impl.obj

The compilation works fine, but the linker shows this error:

Creating library library.lib and object library.exp
LINK : fatal error LNK1104: cannot open file 'library.lib'

What is wrong/missing in my commandline calls?

Here is the source code:

header.hxx

#pragma once

extern __declspec(dllexport) int abc1();

impl.cxx

#include "header.hxx"

extern __declspec(dllexport) int abc1()
{
    return 0;
}

Solution

  • I was using wrong tool. I was supposed to use lib.exe (https://learn.microsoft.com/en-us/cpp/build/reference/overview-of-lib?view=msvc-170) instead of link.exe.

    See the comments of John.

    Thanks a lot John.