How to compile C/C++ for Linux/Mac OS on a Windows?

Asked

Viewed 363 times

5

The title says it all. I have code that can be compiled on the two (three?) platforms and I want to do it straight from Windows only using GCC (Mingw in this case). Would that be possible in relation to cross-compiling? As I would in my case?

  • It is possible yes. GCC Cross Compiler.

  • @Qmechanic73 updated the question if you want to answer

  • @Qmechanic73 was exactly from this page that I took the doubt hahaha

  • This can be a help http://visualgdb.com/tutorials/porting/linux/

1 answer

0

I think the most important thing is to do your project in a way that you can easily compile on multiple platforms. I use the cmake (cmake cross compiling). With cmake, I can compile for Visual Studio, GCC, Mingw (cross-Compiler installed on Linux), Clang (Mac and Linux), without any trauma, it’s well worth it.

The second step is to port your code to properly suit all platforms, let’s assume that you use sockets, the response of a connect timeout given by VC++ is 10060 error with a predefined message, whereas in Mingw it is not possible to recover the error message. Its application should provide for such platform nuances.

And you should download the Linux toolchain on Windows. I think by downloading the msys2 and installing cmake with a Linux cross Compiler, it would be papaya with sugar to compile your project. Although I do the opposite, I develop on the Linux machine and compile for target Windows, and the final distribution in a VM with VC++.

Finally, if you don’t have a toolchain on hand, you can create yours. Basically, you have to compile binutils on Windows for target Linux, and then compile gcc with the binutils you just compiled. I’ll leave the reference how to create a crosscompiler for Rduino, I use this method because I always have in hand the newest gcc, in the case of Arduino.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.