Fix Windows build for cl.exe and clang targeting MSVC - #271
Open
zzyxyz666 wants to merge 1 commit into
Open
Conversation
Make the build and tests work on Windows with both MSVC cl.exe and clang targeting x86_64-pc-windows-msvc, in both C and C++ modes. - shared.h: for cl.exe add /utf-8 to the C (/TC) and C++ (/std:c++20 /TP) flag sets so UTF-8 sources compile correctly on Windows. For clang targeting MSVC use clang-style flags (-std=c99 / -x c++, -Wall, -Wswitch-enum, ...) instead of the MSVC-only flags that its driver rejects. - nob.h: make nob_cc_output append ".exe" when building with clang targeting MSVC (-o<path>.exe) so the produced binary matches the "../<test>.exe" run command. cl.exe already emits "<path>.exe" through /Fe, and non-Windows builds are unchanged. - nob.h: include <consoleapi2.h> and undef the private SDK guards (_WINUSER_, _WINGDI_, _IMM_, _WINCON_) and WIN32_LEAN_AND_MEAN after the system includes so SetConsoleOutputCP() is declared even when compiling with cl.exe /TP. Also undef UNICODE so the generic WinAPI names keep resolving to the ANSI variants the rest of the code uses. - nob.c: remove the extern "C" workaround for SetConsoleOutputCP(), no longer needed with the header fix above.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make the build and tests work on Windows with both MSVC
cl.exeandclang targeting
x86_64-pc-windows-msvc, in both C and C++ modes.shared.h: forcl.exeadd/utf-8to the C (/TC) and C++(
/std:c++20 /TP) flag sets so UTF-8 sources compile correctly onWindows. For clang targeting MSVC use clang-style flags (
-std=c99/-x c++,-Wall,-Wswitch-enum, ...) instead of the MSVC-onlyflags that its driver rejects.
nob.h: makenob_cc_outputappend.exewhen building with clangtargeting MSVC (
-o<path>.exe) so the produced binary matches the../<test>.exerun command.cl.exealready emits<path>.exethrough
/Fe, and non-Windows builds are unchanged.nob.h: include<consoleapi2.h>and undef the private SDK guards(
_WINUSER_,_WINGDI_,_IMM_,_WINCON_) andWIN32_LEAN_AND_MEANafter the system includes soSetConsoleOutputCP()is declared even when compiling withcl.exe /TP. Also#undef UNICODEso the generic WinAPI names keepresolving to the ANSI variants the rest of the code uses. Undefing
the guards is also required for downstream code that includes
<windows.h>afternob.hto still get thewinuser.h/wingdi.h/imm.h/wincon.hdeclarations (<windows.h>cannotbe included before
nob.hbecause of theERRORmacro clash).nob.c: remove theextern "C"workaround forSetConsoleOutputCP(),no longer needed with the header fix above.
Testing
cl.exe /TCandcl.exe /std:c++20 /TP: all tests passclang -x candclang -x c++: all tests passclang-cl /TCandclang-cl /std:c++20 /TP: all tests passgcc -x candgcc -x c++: all tests passclang version 21.1.0cl version 19.44.35222gcc.exe (Rev11, Built by MSYS2 project) 15.2.0``Related: #193 (an earlier, partial attempt that only covered the C flag
set and predates the C++ support split).