Handmade Hero»Forums»Code
Juan Pablo Montiel
2 posts
On cross-compiling.
Edited by Juan Pablo Montiel on Reason: Initial post
Hi folks. I recently discovered cross-compiling and it's some pretty cool stuff.
From what I've read, mingw-w64, which is a mingw fork, is the most popular choice in Linux.

I also read a tweet by mmozeiko saying you can also use clang to do the trick:
clang -target x86_64-pc-windows-gnu -fuse-ld=lld -g -gcodeview -Wl,/debug,/pdb:test.pdb -o test.exe test.c
You need mingw's or Microsoft's headers and libraries but it works. Is it superior to mingw-w64 in some way?

Are there any differences between an exe compiled this way and, say, an exe compiled in Windows with cl? Where can I find more resources on the clang way of doing it?
That's some arcane shit going on in the x86_64-pc-windows-gnu compiler option, I can't even find it in google.

Lastly, is there a thing like a cross-debugger? Like, debugging a Windows exe inside Linux. Maybe even under Wine? The craziness in all of this amazes me.

Thanks in advance.
Mārtiņš Možeiko
2198 posts / 1 project
On cross-compiling.
What do you mean by "Is it superior to mingw-w64 in some way?". This command will use mingw headers and libraries, which are also part of mingw-w64. So you are using same stuff (includes/libs) as mingw-w64 would do. Just on Linux, not Windows.

Are there any differences between an exe compiled this way and, say, an exe compiled in Windows with cl?

As a end result - no difference really. It will work & run the same. Obviously there are differences in compilers, so once code may run a bit slower than other compiler, or be smaller that with other compiler. It depends on actual code.

There is really no resource to describe what are actual differences. All you can do is try same code with both and compare results. On low-level you can check disassembly in godbolt - it can display output from multiple compilers, including gcc, clang and MSVC.

What "arcane shit" are you talking about? Those arguments are pretty standard stuff for clang and lld. Just do clang --help or lld --help and you'll get a lot of information on what is possible.

Cross-debugger is called remote debugger. Most debuggers, including Visual Studio and gdb, and lldb, support debugging process remotely. The problem is not the debugger. The problem is how you will run exe on Linux (without debugger). Wine is one option. Virtual Machines are another, but much heavier option. If you are switching to clang & lld, then it kind of makes sense to use their lldb debugger, which should be able to debug Windows binaries running under wine. It's all possible, but from time to time you'll get to something that does not work quite correctly (or is not implemented fully) - as not many people needs this kind of functionality. Be aware.
123 posts / 4 projects
On cross-compiling.
It seems like there may be some confusion here about how to read the command line arguments. The x86_64-pc-windows-gnu bit is just the second part of the -target option. If you search for "clang target", you will find the page https://clang.llvm.org/docs/CrossCompilation.html which explaines how how the -target option is specified. It's actually a combination of four different pieces of information separated by dashes, which is probably why searching for that specific combination didn't return many results.
Juan Pablo Montiel
2 posts
On cross-compiling.
Edited by Juan Pablo Montiel on
I meant arcane for me, I'm a beginner and it blew my mind and I assumed it was an obscure compiler switch (that I didn't know how to search for). Sorry for the language, it was a "cursing in awe" situation. Thank you Martins for the thorough explanation, as always. Also thank you Miles, that https://clang.llvm.org/docs/CrossCompilation.html link explains it all, that was probably my confusion.