2
votes

here is an interesting question that, if answered positively, would make cross compiling a whole lot easier.

Since gcc is written in C++, would it be possible to recompile the Linux gcc compiler on Windows MinGW G++ or VSC++ compiler, so that the resulting Windows executable would be able to compile c code to linux programs?

If so, what would be needed to do that?

So to simplify, here is what I want to do.

mingw32-g++ gcc.cpp -o gcc.exe

The command will probably not work because it would probably have been done before if it were that easy. What I ask is if this concept would be even possible.

Edit: thanks and expanding the question to NVCC

fvu was able to answer the question for the gcc compiler (please use the answer button next time), so if you had the same question you can thank him (or her) .

As an extention to the question, would it be possible to edit or recompile nvcc or the things it uses so that nvcc.exe can create a linux program from CUDA C code? I read that the windows variant of nvcc can only use the Visual Studio cl.exe and not MinGW or CygWin.

Is it possible to create linux programs with cl.exe? And if so, could that be used to generate linux programs with nvcc.exe?

1
Read the chapter on cross compiling in the gcc manual. Never went the exact route you describe, but I have built toolchains under Windows that target ARM9 embedded Linux machines, works like a charm - using cygwin btw. Look here for a gentle introduction. Also very useful info here - fvu
@fvu Thanks a lot! The link you provided even had a downloadable compiler ready to use. Please see the edit of the question for the extention to this question. - Rik Schaaf
You're welcome - as I was not sure to be in line with what you were looking for, I just commented. - fvu
afaik, you can just create compiled CUDA kernels (PTX - which is bytecode of runtime-part of CUDA; nvcc -ptx your.cu), and then write and compile the cuda host code with gcc, with loadable ptx (load ptx via cuModuleLoad + cuModuleGetFunction example). There is cuda manual about separating device and host codes: cuda-compiler-driver-nvcc using-separate-compilation-in-cuda docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/… - osgx
strange, so far I thought there is GCC version for Windows already. probably I heard about just deploying GCC in cygwin - mangusta

1 Answers

1
votes

Read the chapter on cross compiling in the gcc manual, gcc's architecture makes it quite easy to set up a toolchain where the target is different from the development machine.

I never went the exact route you describe, but I have built toolchains under Windows that target ARM9 embedded Linux machines, works like a charm - using cygwin btw. Look here for a gentle introduction. Also very useful info here.

I am not going to comment on what can be done with respect to nvcc, CUDA is somewhere on my (long) list of stuff to tinker with...

Now, can cl generate Linux binaries? The answer to this question is "sort of" : as long as the target processor is from a processor family that's supported by cl, the object files generated by it should probably not contain anything that would inhibit its execution on Linux, as they'll just contain machine code. That's the theory. However:

  • as Linux uses another executable format, you will need a Windows-hosted linker that understands Windows style object files (afaik, COFF), and links them together to a Linux style (ELF) executable. I never heard of such a beast, although in theory it could exist
  • the startup code (a tiny program that wraps around your main function) will also be different and needs to be written
  • and some more, eg library related issues

So, the practical answer is no, although it might be a nice summer project for a bored student :)