Skip to content

Instantly share code, notes, and snippets.

@infirms
Last active August 23, 2023 10:43
Show Gist options
  • Save infirms/b0da1ca36dbae854c6ec0a133883b0b7 to your computer and use it in GitHub Desktop.
Save infirms/b0da1ca36dbae854c6ec0a133883b0b7 to your computer and use it in GitHub Desktop.
My own way how to crosscompile clang64 to be able produce both x64 and x86 binraries
Install both x86 and x64 clang compilers.(clang64 and clang32)
Get the actual path of your x86 clang.
Add this to your clang64 compiler flags editing the path if needed:
-resource-dir c:/tools/msys64/clang32/lib/clang/16
--sysroot=c:/tools/msys64/clang32
-m32
--target=i686-w64-windows-gnu
Congrats! If both compilers have all needed libraries and platform specific runtimes ( CRT ... etc) you will be able to compile
x86 binary with your x64 clang instance and if you will compare verbose output ("-v") of your linker on original clang32 and clang64
you will be surprised that flags are actually identical 1 to 1 and the only difference is our new specified flags.
Doing it this way never actually caused the problem, also be aware that with major clang released this will be changed!!!(clang32/lib/clang/16)
Keep track of this number or just be a better man and do some sort of automation.
Changing sysroot actually makes your clang64 think that it has become clang32 and it will use proper libraries for
example(libclang, different object files like UCRT object files and etc...).
For testing I was using msys2 edition of clang based environments that will default to the libc++ and LLD linker.
But if you are still using mingw64 or ming32 clang you can specify the same stuff with (-stdlib=libc++ and -fuse-ld=lld) and you can achieve the same results.
Also I had a success in use of the same technique for LLVM installation on the Arch Linux and other distros.
Be aware that this will only work on clang with GNU command line, to check it just type (clang --version) in your terminal.
You will be getting something like this:
clang --version
clang version 16.0.5
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/tools/msys64/clang64/bin
Fun fact you can also doublecheck needed architecture, just run (clang --version) on your target crosscompile toolchain
and use (Target: XXXXXXXXXXX) output to correct your (--target) compiler flag.
Back in the days I've spent a lot of time researching this and it made me to better understand LLVM toolchains, so never give up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment