microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.47k stars 452 forks source link

clang-cl program build issues #3666

Closed cctv130 closed 4 months ago

cctv130 commented 7 months ago

Brief Issue Summary

How to run vcvars64.bat before using clang-cl? I want to debug cmake right click cmakelists.txt select debug, but I want to run vcvars64.bat before debugging the project.

CMake Tools Diagnostics

No response

Debug Log

No response

Additional Information

No response

### Tasks
gcampbell-msft commented 7 months ago

@cctv130 To get more context on this, what is the full scope of your scenario? What are you needed vcvars.bat for?

We currently have some level of support (and are planning on improving it) for including the Visual Studio development environment with your presets, which may enable this. However, depending on your scenario, this may be a feature request.

THanks!

cctv130 commented 7 months ago

As far as I know many people use clang-cl for build projects on windows

Almost 99% percent, will get the following error [cmake] -- Check for working C compiler: C:/Program Files/LLVM/bin/clang-cl.exe [cmake] -- Check for working C compiler: C:/Program Files/LLVM/bin/clang-cl.exe - broken [cmake] CMake Error at C:/Program Files/CMake/v3.24.0/share/cmake-3.24/Modules/CMakeTestCCompiler.cmake:69 (message): [cmake] The C compiler [cmake] [cmake] "C:/Program Files/LLVM/bin/clang-cl.exe" [cmake] [cmake] is not able to compile a simple test program. [cmake] [cmake] It fails with the following output: [cmake] [cmake] Change Dir: D:/tests/cmakepreset/build/win32-clang-x64-ninja-debug-clangcl/CMakeFiles/CMakeTmp [cmake]
[cmake] Run Build Command(s):C:/PROGRA~2/MICROS~4/2019/ENTERP~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe cmTC_da11a && [1/2] Building C object CMakeFiles\cmTC_da11a.dir\testCCompiler.c.obj [cmake] [2/2] Linking C executable cmTC_da11a.exe [cmake] FAILED: cmTC_da11a.exe [cmake] cmd.exe /C "cd . && "C:\Program Files\CMake\v3.24.0\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_da11a.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\mt.exe --manifests -- C:\PROGRA~1\LLVM\bin\lld-link.exe /nologo CMakeFiles\cmTC_da11a.dir\testCCompiler.c.obj /out:cmTC_da11a.exe /implib:cmTC_da11a.lib /pdb:cmTC_da11a.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 ." [cmake] LINK Pass 1: command "C:\PROGRA~1\LLVM\bin\lld-link.exe /nologo CMakeFiles\cmTC_da11a.dir\testCCompiler.c.obj /out:cmTC_da11a.exe /implib:cmTC_da11a.lib /pdb:cmTC_da11a.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_da11a.dir/intermediate.manifest CMakeFiles\cmTC_da11a.dir/manifest.res" failed (exit code 1) with the following output: [cmake] lld-link: error: : undefined symbol: mainCRTStartup [cmake] ninja: build stopped: subcommand failed. [cmake]
[cmake]
[cmake] [cmake]
[cmake] [cmake] CMake will not be able to correctly generate this project. [cmake] Call Stack (most recent call first): [cmake] CMakeLists.txt:5 (project) [cmake] [cmake] [cmake] -- Configuring incomplete, errors occurred! [cmake] See also "D:/tests/cmakepreset/build/win32-clang-x64-ninja-debug-clangcl/CMakeFiles/CMakeOutput.log". [cmake] See also "D:/tests/cmakepreset/build/win32-clang-x64-ninja-debug-clangcl/CMakeFiles/CMakeError.log". [proc] The command: "C:/Program Files/CMake/v3.24.0/bin/cmake.EXE" -DCMAKE_C_COMPILER=clang-cl.exe -DCMAKE_CXX_COMPILER=clang-cl.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=D:/tests/cmakepreset/install/win32-clang-x64-ninja-debug-clangcl -SD:/tests/cmakepreset -BD:/tests/cmakepreset/build/win32-clang-x64-ninja-debug-clangcl -G Ninja exited with code: 1 and signal: null [extension] [3060] cmake.cleanConfigure finished (returned 1)

The best solution is not to set the cmake environment variable set(DCMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

It's about getting ahead of the vs environment "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat"

We can put a similar statement in launch.json "environmentSetupScript": "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" This statement is automatically invoked before right-clicking the mouse to debug, after which the debug session begins

gcampbell-msft commented 7 months ago

@cctv130 Could you test the workaround that I suggested here: https://github.com/microsoft/vscode-cmake-tools/issues/3502#issuecomment-2026064274

cctv130 commented 7 months ago

@gcampbell-msft Sorry, I already commented https://github.com/microsoft/vscode-cmake-tools/issues/3502 here, no need to test again, the problem is that we execute vcvars64.bat before compilation, If you want to test again, you can use cmake-tools to create a simple vscode project, and then compare using clang++.exe and clang-cl.exe to compile the simple demo project Using clang++ or clang.exe directly will not cause similar errors. This error will occur when using clang-cl.exe (not dependent on vcvars64.bat)

To solve this error, find the start menu and search for "x64 Native Tools Command Prompt for VS 2019", double-click the mouse to enter this command window.Follow the steps below cd project_root/build cmake -GNInja .. ninja build successful.

cctv130 commented 7 months ago

In other words, we can call cmd /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" similar statements to enter the vs environment

cctv130 commented 7 months ago

The goal of this clang-cl is to simulate MSVC cl.exe, so it needs vcvars64.bat

cctv130 commented 7 months ago

vcvars64.bat will load some environment variables and runtime libraries and the clang-cl.exe try to be compatible with windows ABI

cctv130 commented 7 months ago

The generated ninja.build file has more information.

gcampbell-msft commented 7 months ago

@cctv130 Are you using kits or presets?

gcampbell-msft commented 7 months ago

@cctv130 The thing that I'm not understanding with your explanations, is that I've tested in multiple different scenarios, running vcvars64.bat, and then opening the repro project from the other bug from that environment, and it still isn't able to configure. That's why I asked about testing the suggestion I made in #3502.

I found that the environment variables were the same as the ones that are included by our execution of vcvarsall.bat in the cmake tools extension, and the only difference was the CXXFLAGS and the CFLAGS. See here

Could you provide a zip of exactly your repro? I'm wondering if there is something I'm missing from your explanations or maybe something specific to your scenario. Thanks!

cctv130 commented 7 months ago

Now I don't have a Windows environment, I don't use presets, LLVM 18.1.2, the problem should be that cmakelist needs to manually set the compiler path instead of relying on clang-cl of vs,I can compile without following your suggestion#3502. Or you can send me the error log and I will check what the problem is tomorrow.

cctv130 commented 7 months ago

Do we have a discord? It's 12 o'clock at night, go to bed, good night💤💤💤

cctv130 commented 7 months ago

Why can I compile without using CXXFLAGS and the CFLAGS.?

cctv130 commented 7 months ago

I don’t have a windows environment now,Please execute clang-cl.exe in llvm18 in the vs environment. set (CMAKE_C_COMPILER "c:/dev/llvm18/bin/clang-cl.exe") set (CMAKE_CXX_COMPILER "c:/dev/llvm18/bin/clang-cl.exe") Obviously vs2022 or vs2019 will not come with llvm18, here is a problem ninja >> 1.txt 2>&1 please send 1.txt to me,or we can build a discord,thank you.

gcampbell-msft commented 4 months ago

Closing this in favor of other issues #3497 that are related to the VS environment, we plan to improve our "magic" which will resolve this issue.