windowsopensslvisual-studio-2017conan

How to fix fatal error C1083 and LNK1104 on windows while building openssl 3 using conan?


When I try to build my app, which depends on openssl3, with conan, I get the following error while conan is trying to build openssl3:

> conan install . --build=missing --profile:build=default_windows_x64_release --profile=default_windows_x64_debug
...
openssl/3.0.8: Generating aggregated env files
openssl/3.0.8: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
openssl/3.0.8: Calling build()
openssl/3.0.8: RUN: C:/Users/username/.conan2/p/straw898fb56546adc/p/bin/perl.exe ./Configure "VC-conan-Debug-Windows-x86_64-msvc-191" no-shared --prefix=/ --libdir=lib --openssldir="C:\Users\username\.conan2\p\b\opens4a3ff4f862506\p\res" no-unit-test threads PERL=C:/Users/username/.conan2/p/straw898fb56546adc/p/bin/perl.exe no-tests --debug enable-fips no-md2 zlib --with-zlib-include="C:/Users/username/.conan2/p/b/zlib69dc6f0d4df93/p/include" --with-zlib-lib="C:/Users/username/.conan2/p/b/zlib69dc6f0d4df93/p/lib/zlib.lib"
conanvcvars.bat: Activating environment Visual Studio 15 - amd64 - winsdk_version=None - vcvars_ver=14.1
[vcvarsall.bat] Environment initialized for: 'x64'
Configuring OpenSSL version 3.0.8 for target VC-conan-Debug-Windows-x86_64-msvc-191
Using os-specific seed configuration
Created configdata.pm
Running configdata.pm
Created makefile.in
Created makefile
Created include\openssl\configuration.h

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL.md file first)      ***
***                                                                ***
**********************************************************************

openssl/3.0.8: RUN: nmake
conanvcvars.bat: Activating environment Visual Studio 15 - amd64 - winsdk_version=None - vcvars_ver=14.1
[vcvarsall.bat] Environment initialized for: 'x64'

Microsoft (R) Program Maintenance Utility Version 14.16.27051.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        "C:/Users/username/.conan2/p/straw898fb56546adc/p/bin/perl.exe" "-I." -Mconfigdata "util\dofile.pl" "-omakefile" "include\crypto\bn_conf.h.in" > include\crypto\bn_conf.h
        ...
        "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe" /                   depend

Microsoft (R) Program Maintenance Utility Version 14.16.27051.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe" /                   _build_sw

Microsoft (R) Program Maintenance Utility Version 14.16.27051.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        "cl"  /Zi /Fdossl_static.pdb /MTd /Zl /Gs0 /GF /Gy -MTd -Zi -Ob0 -Od -FS -MTd -Zi -Ob0 -Od -FS -I"." -I"include" -I"apps\include" -D"L_ENDIAN" -D"OPENSSL_PIC" -D"OPENSSLDIR=\"C:\\Users\\username\\.conan2\\p\\b\\opens4a3ff4f862506\\p\\res\"" -D"ENGINESDIR=\"\\lib\\engines-3\"" -D"MODULESDIR=\"\\lib\\ossl-modules\"" -D"OPENSSL_BUILDING_OPENSSL" -D"ZLIB" -D"OPENSSL_SYS_WIN32" -D"WIN32_LEAN_AND_MEAN" -D"UNICODE" -D"_UNICODE" -D"_CRT_SECURE_NO_DEPRECATE" -D"_WINSOCK_DEPRECATED_NO_WARNINGS" -D"DEBUG" -D"_DEBUG" -I"C:/Users/username/.conan2/p/b/zlib69dc6f0d4df93/p/include"   -c /Foapps\lib\libapps-lib-app_libctx.obj "apps\lib\app_libctx.c"
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27051 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

app_libctx.c
C:\Users\username\.conan2\p\b\opens4a3ff4f862506\b\src\include\openssl/crypto.h(25): fatal error C1083: Cannot open include file: 'stdlib.h': No such file or directory
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\cl.EXE"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

openssl/3.0.8: ERROR:
Package 'df45d737e7817e09557f98b927968632469772f8' build failed
openssl/3.0.8: WARN: Build folder C:\Users\username\.conan2\p\b\opens4a3ff4f862506\b\build-debug
ERROR: openssl/3.0.8: Error in build() method, line 520
        self._make()
while calling '_make', line 513
        self._run_make()
while calling '_run_make', line 490
        self.run(" ".join(command), env="conanbuild")
        ConanException: Error 2 while executing

I suspect this happens because MSVS doesn't set all of the includes and paths correctly. I think so, because when I execute "set INCLUDE" in Developer Command Prompt for VS 2017, I get this:

>set include
INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include;C:\PROGRA~1\IBM\SQLLIB\INCLUDE;C:\PROGRA~1\IBM\SQLLIB\LIB

On the other hand, when I manually add missing includes in my Developer Command Prompt, like this:

set INCLUDE=%INCLUDE%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\winrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\VS\UnitTest\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\ucrt;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\VS\include;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\atlmfc\include;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include;C:\3coreserver\commlib2a\openssl\windows\vs2012\include;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\PlatformSDK\include;C:\Program Files (x86)\Windows Kits\10\Source\10.0.16299.0\ucrt;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\VS\src;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\crt\src;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\atlmfc\src\atl;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\atlmfc\src\mfcm;C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\atlmfc\src\mfc;

I get different error:

legacyprov.c
        cmd /C ""cl" /Zs /showIncludes  /Zi /Fddso.pdb /Gs0 /GF /Gy -MTd -Zi -Ob0 -Od -FS -MTd -Zi -Ob0 -Od -FS -I"include" -I"providers\implementations\include" -I"providers\common\include" -D"OPENSSL_BUILDING_OPENSSL" -D"ZLIB" -D"OPENSSL_SYS_WIN32" -D"WIN32_LEAN_AND_MEAN" -D"UNICODE" -D"_UNICODE" -D"_CRT_SECURE_NO_DEPRECATE" -D"_WINSOCK_DEPRECATED_NO_WARNINGS" -D"DEBUG" -D"_DEBUG" -I"C:/Users/username/.conan2/p/b/zlib69dc6f0d4df93/p/include"   "providers\legacyprov.c" > providers\legacy-dso-legacyprov.d 2>&1"
        "C:/Users/username/.conan2/p/straw898fb56546adc/p/bin/perl.exe" "util\mkrc.pl" legacy > providers\legacy.rc
        "rc"  /foproviders\legacy-dso-legacy.res "providers\legacy.rc"
Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
Copyright (C) Microsoft Corporation.  All rights reserved.

        "C:/Users/username/.conan2/p/straw898fb56546adc/p/bin/perl.exe" "util\mkdef.pl" --type dso --ordinals util\providers.num --name legacy --OS windows > providers\legacy.def
        IF EXIST providers\legacy.dll.manifest DEL /F /Q providers\legacy.dll.manifest
        cmd /C ""link" -debug /dll -debug -debug @C:\Users\username\AppData\Local\Temp\2\nm17C5.tmp || (DEL /Q legacy.* providers\legacy.* & EXIT 1)"
Microsoft (R) Incremental Linker Version 14.16.27051.0
Copyright (C) Microsoft Corporation.  All rights reserved.

providers\legacy-dso-legacyprov.obj
/out:providers\legacy.dll
providers\liblegacy.lib
providers\libcommon.lib
libcrypto.lib
C:/Users/username/.conan2/p/b/zlib69dc6f0d4df93/p/lib/zlib.lib ws2_32.lib gdi32.lib advapi32.lib crypt32.lib user32.lib
/def:providers\legacy.def providers\legacy-dso-legacy.res
LINK : fatal error LNK1104: cannot open file 'ws2_32.lib'
Could Not Find C:\Users\username\.conan2\p\b\opensdbeefee112c1f\b\src\legacy.*
NMAKE : fatal error U1077: 'cmd' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

I have Microsoft SDK (10.0.16299.0) installed for VS 2017 but its include and libs paths are not set by default.

I don't understand how do I fix this, please help.

Edit 1. Some versions:

>python --version
Python 3.10.10
>conan --version
Conan version 2.4.1
>cmake --version
cmake version 3.25.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Edit 2. What I've already tried to fix it:

  1. reinstalling Windows SDK through Visual Studio Installer with reboots in between (didn't fix the issue)
  2. removing MSVS 2022, which was also installed alongside MSVS 2017 (didn't fix the issue)
  3. repairing MSVS 2017 (didn't fix the issue)
  4. adding Windows SDK location (C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0*) to PATH (didn't fix the issue)
  5. reinstalling MSVS 2017 completely (didn't fix the issue)

Solution

  • I'm answering my own question since I have found a root cause and a temporary workaround.

    In my case, the cause of this issue is that I don't have the privileges to read the Windows registry using the reg query command and don't have regedit enabled for my user due to security policies established at my workplace.

    Explanation:

    Microsoft Visual Studio sets up the dev environment by calling a script called vcvarsall.bat, which depends on the script called winsdk.bat, which uses the reg query command to set some dev environment variables required to build some of the packages (openssl 3.0.8 in my case).

    The REAL solution would be to get at least read-only access to the Windows registry.

    Since this can be unachievable, I have found a workaround:

    1. Locate a winsdk.bat of your Microsoft Visual Studio (it should be somewhere in C:\Program Files (x86)\Microsoft Visual Studio\2017\*\Common7\Tools\vsdevcmd\core\winsdk.bat, the * in my case was Professional but it can also be Community or something else, depending on the version of MSVS that you have installed)
    2. Open this file in a notepad with admin privileges (since it is protected from modification by a regular user; I achieved this by opening Windows PowerShell as admin, yes, that is allowed for me, and running notepad winsdk.bat)
    3. Find the functions where reg query is called. These 2 functions: GetWin10SdkDirHelper and GetUniversalCRTSdkDirHelper.
    4. Right after their calls you should set the variables, that are usually set by these functions, "manually":
    call :GetWin10SdkDirHelper HKLM\SOFTWARE\Wow6432Node > nul 2>&1
    set WindowsSdkDir=C:\Program Files (x86)\Windows Kits\10\
    echo "debug 1" %WindowsSdkDir%
    ...
    call :GetUniversalCRTSdkDirHelper HKLM\SOFTWARE\Wow6432Node > nul 2>&1
    set UniversalCRTSdkDir=C:\Program Files (x86)\Windows Kits\10\
    echo "debug 2" %UniversalCRTSdkDir%
    

    In your particular case, the Windows SDK path can be different. I made sure that on my system those particular registry entries are set to those values first but you can try providing the path to your Windows 10 SDK and see if it works.

    This workaround requires some level of admin privileges, which allows you to modify winsdk.bat, so it may not work for you.

    There is also a second workaround - setting dev env variables manually.

    Open a "Developer Command Prompt for VS 2017" and input next commands (your Windows SDK versions can be different but the idea is the same):

    set INCLUDE=%INCLUDE%;C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\cppwinrt
    
    set LIB=%LIB%;C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\ucrt\x86;C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\um\x86
    
    set PATH=%PATH%;C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86;C:\Program Files (x86)\Windows Kits\10\bin\x86
    

    After you do that run conan build.

    Warning: this workaround is not permanent and you have to set those env variables every time you need to build packages, which require Windows SDK.