MSVC + ninja | NMake: generation fails when target arch is x86 but host arch is x64 (cross compilation)
Steps to reproduce:
- Initialize environment
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat" -arch=x86 -host_arch=x64 -vcvars_ver=14.16
- Call cmake generationwith Ninja
cmake -G"Ninja" -DCMAKE_BUILD_TYPE="Debug" -S "your dummy project" -B "build\Ninja Win32\Debug"
I get following output:
-- The CXX compiler identification is MSVC 19.16.27045.0
-- The C compiler identification is MSVC 19.16.27045.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.21/Modules/CMakeTestCXXCompiler.cmake:62 (message):
The C++ compiler
"C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: your dummy project/Ninja Win32/Debug/CMakeFiles/CMakeTmp
Run Build Command(s):C:/PROGRA~1/CMake/bin/ninja.exe cmTC_bf7f1 && [1/2] Building CXX object CMakeFiles\cmTC_bf7f1.dir\testCXXCompiler.cxx.obj
[2/2] Linking CXX executable cmTC_bf7f1.exe
FAILED: cmTC_bf7f1.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_bf7f1.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~1\2019\PROFES~1\VC\Tools\MSVC\1416~1.270\bin\HostX64\x64\link.exe /nologo CMakeFiles\cmTC_bf7f1.dir\testCXXCompiler.cxx.obj /out:cmTC_bf7f1.exe /implib:cmTC_bf7f1.lib /pdb:cmTC_bf7f1.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\PROFES~1\VC\Tools\MSVC\1416~1.270\bin\HostX64\x64\link.exe /nologo CMakeFiles\cmTC_bf7f1.dir\testCXXCompiler.cxx.obj /out:cmTC_bf7f1.exe /implib:cmTC_bf7f1.lib /pdb:cmTC_bf7f1.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_bf7f1.dir/intermediate.manifest CMakeFiles\cmTC_bf7f1.dir/manifest.res" failed (exit code 1120) with the following output:
testCXXCompiler.cxx.obj : error LNK2001: unresolved external symbol _RTC_InitBase
testCXXCompiler.cxx.obj : error LNK2001: unresolved external symbol _RTC_Shutdown
LINK : error LNK2001: unresolved external symbol mainCRTStartup
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\kernel32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\user32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\gdi32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\winspool.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\shell32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\ole32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\oleaut32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\uuid.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\comdlg32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\advapi32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.16.27023\lib\x86\MSVCRTD.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
cmTC_bf7f1.exe : fatal error LNK1120: 3 unresolved externals
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
It should use MSVC/14.16.27023/bin/HostX64/x86/cl.exe
NMake Makefiles
generator is also affected. Maybe something other as well.
To make Visual Studio
generator work correctly I have to use -A option. Env initialization is not enough for it. But maybe it's by design..
CMake version -> 3.21.2