MarkSchofield / WindowsToolchain

A repository containing a CMake toolchain for using MSVC
MIT License
106 stars 19 forks source link

clangcl error: lld-link: error: <root>: undefined symbol: mainCRTStartup #97

Closed aboelens closed 7 months ago

aboelens commented 7 months ago

First of all, thank you very much for creating these tool chains. They have been very helpful.

In addition to the windows-msvc-x64 preset, I recently tried out the windows-clangcl-x64 preset in the provided example. Unfortunately it is not working for me in a Developer PowerShell for VS 2022. The windows-msvc-x64 preset works just fine in both a Developer PowerShell for VS 2022 and a x64 Native Tools Command Prompt for VS 2022. However, the windows-clangcl-x64 preset only works in the x64 Native Tools Command Prompt for VS 2022. In the PowerShell I get the error message below. I am not sure I am missing something or whether this is a bug.

Preset CMake variables:

CMAKE_CXX_COMPILER_FRONTEND_VARIANT="MSVC" CMAKE_C_COMPILER_FRONTEND_VARIANT="MSVC" CMAKE_SYSTEM_PROCESSOR="x64" CMAKE_SYSTEM_VERSION="10.0.19041.0" CMAKE_TOOLCHAIN_FILE="../Windows.Clang.toolchain.cmake" CMAKE_VS_VERSION_PRERELEASE="ON" CMAKE_VS_VERSION_RANGE="[16.0,18.0)" CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM="10.0.22621.0"

-- The C compiler identification is Clang 17.0.3 with MSVC-like command-line -- The CXX compiler identification is Clang 17.0.3 with MSVC-like command-line -- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/Llvm/x64/bin/clang-cl.exe -- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/Llvm/x64/bin/clang-cl.exe - broken CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.28/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler

"C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/Llvm/x64/bin/clang-cl.exe"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: 'C:/Users/A879833/source/repos/WindowsToolchain/example/__output/windows-clangcl-x64/CMakeFiles/CMakeScratch/TryCompile-2w3ecu'

Run Build Command(s): C:/PROGRA~1/MIB055~1/2022/PROFES~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe -v cmTC_c2857
[1/2] C:\PROGRA~1\MIB055~1\2022\PROFES~1\VC\Tools\Llvm\x64\bin\clang-cl.exe  /nologo -DCMAKE_INTDIR=\"Debug\" -imsvc"C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" -imsvc"C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared" -imsvc"C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um" -imsvc"C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt" -imsvc"C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\cppwinrt" /DWIN32 /D_WINDOWS  /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\cmTC_c2857.dir\Debug\testCCompiler.c.obj /FdCMakeFiles\cmTC_c2857.dir\Debug\ -c -- C:\Users\A879833\source\repos\WindowsToolchain\example\__output\windows-clangcl-x64\CMakeFiles\CMakeScratch\TryCompile-2w3ecu\testCCompiler.c
[2/2] C:\WINDOWS\system32\cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_c2857.dir\Debug --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MIB055~1\2022\PROFES~1\VC\Tools\Llvm\x64\bin\lld-link.exe /nologo CMakeFiles\cmTC_c2857.dir\Debug\testCCompiler.c.obj  /out:Debug\cmTC_c2857.exe /implib:Debug\cmTC_c2857.lib /pdb:Debug\cmTC_c2857.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100226~1.0\ucrt\x64   -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100226~1.0\um\x64   -LIBPATH:"C:\Program Files (x86)\Windows Kits\10\References\x64" kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
FAILED: Debug/cmTC_c2857.exe
C:\WINDOWS\system32\cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_c2857.dir\Debug --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MIB055~1\2022\PROFES~1\VC\Tools\Llvm\x64\bin\lld-link.exe /nologo CMakeFiles\cmTC_c2857.dir\Debug\testCCompiler.c.obj  /out:Debug\cmTC_c2857.exe /implib:Debug\cmTC_c2857.lib /pdb:Debug\cmTC_c2857.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100226~1.0\ucrt\x64   -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100226~1.0\um\x64   -LIBPATH:"C:\Program Files (x86)\Windows Kits\10\References\x64" 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~1\MIB055~1\2022\PROFES~1\VC\Tools\Llvm\x64\bin\lld-link.exe /nologo CMakeFiles\cmTC_c2857.dir\Debug\testCCompiler.c.obj /out:Debug\cmTC_c2857.exe /implib:Debug\cmTC_c2857.lib /pdb:Debug\cmTC_c2857.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100226~1.0\ucrt\x64 -LIBPATH:C:\PROGRA~2\WI3CF2~1\10\lib\100226~1.0\um\x64 -LIBPATH:C:\Program Files (x86)\Windows Kits\10\References\x64 kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_c2857.dir\Debug/intermediate.manifest CMakeFiles\cmTC_c2857.dir\Debug/manifest.res" failed (exit code 1) with the following output:
lld-link: error: <root>: undefined symbol: mainCRTStartup
ninja: build stopped: subcommand failed.

CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:6 (project)

-- Configuring incomplete, errors occurred!

MarkSchofield commented 7 months ago

@aboelens - thanks for reporting the problem. I can repro it, too - running cmake --preset windows-clangcl-x64 from a Developer PowerShell for VS 2022 prompt does fail as you call out. I'll take a look and see what's going on.

TBH, though, running from a Developer PowerShell for VS 2022 for any of the Toolchain files might not be a good idea. Those prompts set environment variables - e.g. IFCPATH, INCLUDE, LIBPATH - that MSVC (and clang, I guess..?) will use, and that might make things precarious. One reason I like the 'toolchain' approach, is because I can switch between targeting different architectures, without launching a new prompt. But when I launch Developer PowerShell for VS 2022 it seems to have set, say, LIB to x86 paths; so it may appear to work on x86 builds, but I don't know if it's picking up the environment-configured paths, or the Toolchain-configured paths.

MarkSchofield commented 7 months ago

mainCRTStartup should be found in msvcrt[d].lib, which should be found in a "VC\Tools" folder, like "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\lib\x64". But Windows.Clang.toolchain.cmake isn't actually adding that as a library path. In a 'plain' command prompt, lld-link.exe appears to be finding it by itself (through some mechanism/heuristics I haven't looked at yet). But in a Developer PowerShell for VS 2022 prompt, the LIB environment variable changes lld-link.exe behavior, and the LIB environment variable paths are used instead. As I mentioned in my first response, a Developer PowerShell for VS 2022 prompt is initialized for x86 targets by default, and so lld-link.exe ends-up looking for msvcrt[d].lib in an x86 folder, and can't consider that lib, because it's compiling for x64. And so you end-up with an unresolved symbol.

Options:

  1. Somehow configure the Developer PowerShell for VS 2022 prompt to be x64
  2. Clear the LIB environment variable
  3. Fix Windows.Clang.toolchain.cmake to figure out the correct path (stealing/sharing code from/with Windows.MSVC.toolchain.cmake).