2

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?

Rik Schaaf
  • 1,101
  • 2
  • 11
  • 30
  • 3
    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](http://wiki.osdev.org/GCC_Cross-Compiler) for a gentle introduction. Also [very useful info here](http://gcc.gnu.org/wiki/Building_Cross_Toolchains_with_gcc) – fvu Feb 24 '14 at 23:11
  • @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 Feb 25 '14 at 00:44
  • You're welcome - as I was not sure to be in line with what you were looking for, I just commented. – fvu Feb 25 '14 at 00:48
  • 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](http://stackoverflow.com/questions/20657004/cuda-linking-a-kernel-to-a-ptx-function)). There is cuda manual about separating device and host codes: cuda-compiler-driver-nvcc using-separate-compilation-in-cuda http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#using-separate-compilation-in-cuda – osgx Feb 25 '14 at 00:52
  • strange, so far I thought there is GCC version for Windows already. probably I heard about just deploying GCC in cygwin – mangusta Feb 25 '14 at 00:55
  • @osgx if I understand clearly, I have to compile the kernels separately with `nvcc -ptx kernel.cu` and then compile the other code with nvcc using `-arch=sm_20` or higher and the `-dc` option so I'd get separate object files for every code file. Then I can use the gcc cross-compiler to create the linux executable. I probably stated something wrong here, so please correct me if I'm wrong. Also, the example is for c++, not for c. So do I have to use the g++ compiler? Is there a c alternative? Furthermore, I read that object files are platform dependent. Therefore what I described doesn't work. – Rik Schaaf Mar 06 '14 at 20:47
  • @osgx What I really tried to say is: What do I need to do (the exact order) to use the windows nvcc compiler and [_This_](https://docs.google.com/file/d/0B85K_c7mx3QjUnZuaFRPWlBIcXM/edit) GNU compiler? – Rik Schaaf Mar 06 '14 at 20:50
  • @coolcat007, Sorry, I didn't test gcc+CUDA cross compiling from windows to linux; I also have no time and no ability to test it to create exact instructions. Basically you are right; g++ and gcc compiler can be used together, it is the same compiler in different language modes, but it is compatible. Object files are ABI-dependent; object files for host should be built using right gcc/g++ cross compiler; object code for the Nvidia card are universal both for linux and windows, don't know about object files (marked as fatbinary at image in http://docs.nvidia.com/). Easier is to get VirtualBox. – osgx Mar 07 '14 at 02:32

1 Answers1

1

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 :)

fvu
  • 32,488
  • 6
  • 61
  • 79
  • objcopy is the beast, isn't it? There are bad advices for using it as COFF-ELF translator since 2000: https://sourceware.org/ml/binutils/2000-09/msg00277.html sorry since 1996 http://compilers.iecc.com/comparch/article/96-07-132 but the beast can't do all the needed magic – osgx Mar 07 '14 at 02:39