"LNK1104 Cannot open file 'kernel32.lib' " when trying to Configure project in CMAKE

267 Views Asked by At

I'm trying to configure in CMAKE a C, C++, Fortran project in x64 with Visual Studio 17 2022, if I run it as it is, this error first will appear:

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
CMake Error at CMakeLists.txt:4 (project):
  No CMAKE_Fortran_COMPILER could be found.



Configuring incomplete, errors occurred!

I fix this error by setting on CMAKE the path where the compiler is located:

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

And then I tried to configure the project again (first deleting the cache):

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-yywg1p
    
    Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/devenv.com CMAKE_TRY_COMPILE.sln /build Debug /project cmTC_cd35e && 

    Microsoft Visual Studio 2022 Version 17.7.5.

    Copyright (C) Microsoft Corp. All rights reserved.

    Build started...

    1>------ Build started: Project: cmTC_cd35e, 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\test.core\build\CMakeFiles\CMakeScratch\TryCompile-yywg1p\Debug\cmTC_cd35e.exe

    /VERSION:0.0

    /MANIFEST

    /MANIFESTFILE:cmTC_cd35e.dir\Debug\cmTC_cd35e.exe.intermediate.manifest

    "/MANIFESTUAC:level='asInvoker' uiAccess='false'"

    /SUBSYSTEM:CONSOLE

    /IMPLIB:C:\workspace\test.core\build\CMakeFiles\CMakeScratch\TryCompile-yywg1p\Debug\cmTC_cd35e.lib

    /machine:x64

    cmTC_cd35e.dir\Debug\testFortranCompiler.obj

    cmTC_cd35e.dir\Debug\cmTC_cd35e.exe.embed.manifest.res

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

TL;DR: LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

When I open the fortran project that was created (.vfproj) on Visual Studio, and in there I go to 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'm not sure how to replicate this solution but in CMAKE, I saw that a possible solution is using a command called 'target_link_libraries(your_target_name kernel32.lib)' in CMakeLists.txt file, but I would prefer to not modify the CMakeLists.txt file, as it's used by a lot of people and I would have to save the changes. Maybe use a command in the console when running cmake.

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.

Thanks for your help!

0

There are 0 best solutions below