LNK1104: cannot open file .lib error in CMAKE

I only have installed VS2022 (with desktop development with C++ checked, and Windows 11 SDK 10.0.22621.0), along with Intel Fortran Compiler (IFORT) from intel’s oneapi HPC Toolkit.

In CMAKE 3.26.3 GUI, when I try to Configure a project (C&C++&Fortran project), the following error appears:

LINK : fatal error LNK1104: cannot open file ‘kernel32.lib’

If I create a fortran project in VS2022, and try to build it, this same error appears, I fixed it directly in Vs2022 in Project Properties > Configuration Properties > Linker > General > Additional Library Directories, and I add the folder where kernel32.lib is located at, and I try to build the project in VS again, it works and the LNK1104 error disappears.

I want to add the folder path in CMAKE, but I’m not sure how to do it without directly modifying the CMakeLists.txt file for the project (this file is used by a lot of people and it works for them, so I would prefer to use command line for cmake to fix this issue)

What command can I use to fix this problem?

I also want to add that I tried uninstalling and installing VS2022 several times, also the Intel Fortran compiler, but the same error happens (kernel32.lib error). And I do have installed Desktop Development with C++ option in the VS Installer. Also the Windows 11 SDK.

Does anyone know how to fix this?

Thanks for your support!

I typically use oneAPI on Windows from the oneAPI command prompt. If you don’t see that under Windows Start menu, then do one of the following:

Command Prompt:

"C:\Program Files (x86)\Intel\oneAPI\setvars.bat"

OR if using PowerShell:

cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'

My experience is that the VS IDE integration requires specifically matching MSVC versions, which are often old. So I just use the Command Prompt and oneAPI essentially always works that way without the tight MSVC version requirements.

Hi!
So I ran Intel oneAPI from the command prompt, and I ran CMAKE inside of this command prompt.

I used this command:

cmake -S C:/workspace/siesta.core -B C:/workspace/siesta.core/build -DCMAKE_Fortran_COMPILER=“C:/Program Files (x86)/Intel/oneAPI/compiler/2023.2.0/windows”

(I have to set the CMAKE_Fortran_COMPILER path because if I don’t, another error first appears that tells me CMAKE_Fortran_COMPILER wasn’t found).

After I did this, the following output appears in the console:

– Building for: Visual Studio 17 2022
– Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19044.
– The CXX compiler identification is MSVC 19.37.32825.0
– The C compiler identification is MSVC 19.37.32825.0
– The Fortran compiler identification is unknown
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe - skipped
– Detecting CXX compile features
– Detecting CXX compile features - done
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe - skipped
– Detecting C compile features
– Detecting C compile features - done
– Detecting Fortran compiler ABI info
– Detecting Fortran compiler ABI info - failed
– Check for working Fortran compiler: C:/Program Files (x86)/Intel/oneAPI/compiler/2023.2.0/windows
– Check for working Fortran compiler: C:/Program Files (x86)/Intel/oneAPI/compiler/2023.2.0/windows - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.26/Modules/CMakeTestFortranCompiler.cmake:59 (message):
The Fortran compiler

"C:/Program Files (x86)/Intel/oneAPI/compiler/2023.2.0/windows"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: C:/workspace/siesta.core/build/CMakeFiles/CMakeScratch/TryCompile-qdfcoq

Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/devenv.com CMAKE_TRY_COMPILE.sln /build Debug /project cmTC_3620b &&
Microsoft Visual Studio 2022 Version 17.7.5.
Copyright (C) Microsoft Corp. All rights reserved.
Build started...
1>------ Build started: Project: cmTC_3620b, Configuration: Debug x64 ------
Compiling with Intelr Fortran Compiler Classic 2021.10.0 [Intel(R) 64]...
testFortranCompiler.f
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.10.0 Build 20230609_000000
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.
Compiling manifest to resources...
Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
Copyright (C) Microsoft Corporation.  All rights reserved.
Linking...
Microsoft (R) Incremental Linker Version 14.37.32825.0
Copyright (C) Microsoft Corporation.  All rights reserved.
/OUT:C:\workspace\siesta.core\build\CMakeFiles\CMakeScratch\TryCompile-qdfcoq\Debug\cmTC_3620b.exe
/VERSION:0.0
/MANIFEST
/MANIFESTFILE:cmTC_3620b.dir\Debug\cmTC_3620b.exe.intermediate.manifest
"/MANIFESTUAC:level='asInvoker' uiAccess='false'"
/SUBSYSTEM:CONSOLE
/IMPLIB:C:\workspace\siesta.core\build\CMakeFiles\CMakeScratch\TryCompile-qdfcoq\Debug\cmTC_3620b.lib
/machine:x64
cmTC_3620b.dir\Debug\testFortranCompiler.obj
cmTC_3620b.dir\Debug\cmTC_3620b.exe.embed.manifest.res
LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

Build log written to  "file://C:/workspace/siesta.core/build/CMakeFiles/CMakeScratch/TryCompile-qdfcoq/cmTC_3620b.dir/Debug/BuildLog.htm"
cmTC_3620b - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build started at 8:14 AM and took 00.877 seconds ==========

Same error related to kernel32.lib

I tested using Visual Studio 2022 and building a simple Hello World program in Fortran, and the same error happens inside of VS2022:

Inside of VS2022, I fixed it in Configuration Properties > Linker > General,
by adding the path where the .lib files are located (in the ‘Additional Library Directories’ section)

(“C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64”;“C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64”):

and after building the project again in Debug x64, it succeeds
VS2022 Output window shows this message:

Build started…
1>------ Build started: Project: Console4 (IFORT), Configuration: Debug x64 ------
Linking…
Embedding manifest…

Build log written to “file://C:/Users/223080702/source/repos/Console4/Console4/x64/Debug/BuildLog.htm”
Console4 - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build started at 8:21 AM and took 00.922 seconds ==========

So I have to replicate this solution but in CMAKE, I need to include those directories in CMAKE but I’m not sure how, I don’t want to modify the CMakeLists.txt file

Thanks