Thread: Help picking a good Windows compiler that supports Linux code.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96

    Help picking a good Windows compiler that supports Linux code.

    Hello,

    I'm used to writing code in Linux. I'm working on a program, however, that I'd like to support multiple platforms. I'd like to have one source code and be able to compile it in Linux or in Windows without changing anything in the code. I use the GNU C compiler, version 4.8.3.

    I was using MinGW. I never really liked Cygwin because I always needed to provide a dll with my Windows applications. My program is going to be communicating with a device via a serial port and unfortunately, RS-232 serial communication in C is a lot different in Windows than it is in Linux. I've thankfully been able to find a library that supports Windows, FreeBSD, Linux and a few other OSes that will allow me to do what I want to do. It doesn't support MinGW. It says it supports MinGW-W64.

    What do people generally use for a compiler when they're looking for portable code? I was looking at how to install this MinGW-W64 and it looks a bit confusing. I see something about MSYS2 and then there's another option, Cygwin, there's a bunch more.

    Also, what's a good free IDE to use for Windows? CodeBlocks or is there any other good ones? Dev-C++ seems pretty decent, a little buggy but not bad. It just doesn't seem like it's being developed anymore though. Thanks.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,094
    What do people generally use for a compiler when they're looking for portable code?
    O_o

    I have to say, but I've used "MinGW-W64" on "Windows" for quite a while, and I'd recommend you to do the same.

    Dev-C++ seems pretty decent, a little buggy but not bad.
    DO NOT USE THE OLD "Dev-C++" APPLICATION!

    The old "Dev-C++" application isn't a little buggy. A bug in the editor widget can result in losing code.

    You can mitigate the code loss problems by also using version control, but you have no reason to use an application with such a bug when so many exist which do not have such a problem.

    The "Orwell Dev-C++" compiler fixed, as far as I can tell, the editor code loss bug.

    The "Orwell Dev-C++" is indeed still a little buggy, but I don't think you'll lose any code.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by phantomotap View Post
    O_o

    I have to say, but I've used "MinGW-W64" on "Windows" for quite a while, and I'd recommend you to do the same.
    Thank you! I'm sorry for such all the simple questions here but like I said, I normally program in Linux. I haven't had to write a Windows program since 2005. Back than, we used Turbo Pascal! Anyway, with this MinGW64-W32, how do I go about installing it? If I were to install something like MSYS2, would it come with it and would I need to use the MSYS2? I'm guessing the MSYS2 is just like a Linux shell, with bash and a collection of *nix like utlities, wget, git, etc. I don't really like using that in Windows but if I have to, I will. Or do I need to download MinGW64-W32 separately?

    Quote Originally Posted by phantomotap View Post
    O_o
    DO NOT USE THE OLD "Dev-C++" APPLICATION!

    The old "Dev-C++" application isn't a little buggy. A bug in the editor widget can result in losing code.

    You can mitigate the code loss problems by also using version control, but you have no reason to use an application with such a bug when so many exist which do not have such a problem.

    The "Orwell Dev-C++" compiler fixed, as far as I can tell, the editor code loss bug.

    The "Orwell Dev-C++" is indeed still a little buggy, but I don't think you'll lose any code.

    Soma
    Oh no! I think I was using the bad one. The About page says Bloodshed software (copyright 1991 I think) I have a more mature version on the Linux box. Mainly, I code on Linux, bring the code out here on this Windows box and then see if it compiles and executes properly. What IDE do you use? Is it the Orwell Dev-C++ or is it a different one all together?

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    Never tried it but:
    It looks like you download the Mingw64 compiler on Windows and than follow this page < MinGW-w64 - for 32 and 64 bit Windows / Wiki2 / Compiling the Runtime > to install it...type in the word autoconf. You install Cygwin separately on Windows for the portable Linux shell. There are other options, but Mysys2 is not on the friends list. I quickly read that Mysys2 is not just wrappers, but it is more for building native Windows applications...which maybe means that the code will be more efficient but not portable.

  5. #5
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Cygwin or some mingw distro are your best bet. Cygwin is a better option for what you are asking, since it implements a Unix layer on top of Windows. Hence the required .dll distribution along with your executable (which is not a big deal btw). However, you are mostly bounded to use Cygwin's versions of whatever librararies/frameworks you are using. For exampe, GTK+ 's downlaod page for Windows makes it very explicit that what it is offered in that page is NOT meant for development under Cygwin (obviously Cygwin provides GTK through its own package-manager).

    I'm using both the original mingw (formely mingw32) and mingw-w64 (only for 64-bit executables). Lots of libraries recommend the former, and/or provide such binaries. It's pretty straight forward to install and use, because it does not have any variations like mingw-w64. But it is focused on Windows, thus it lacks support for some Unix stuff. Don't remember which exactly, by heart right now... I think posix threads is among them (anyway, you should check these things out before deciding between mingw and mingw-w64).

    mingw-w64 is a fork of mingw and if I remember correctly, it has been the standard Cygwin compiler for a while. It provides better Unix support, but it is a bit more complicated than plain mingw, since it provides several installation options, depending on what features you want (for example, you may choose between win32 and posix threads, or which exception model you want to use, etc).

    As for IDEs, Code::Blocks is very decent (way better than Dev-C++, original or Orwell) and it is available on both worlds... on Macs too, I think, don't remember). Thus you won't have to learn a different IDE if you switch platforms.

    msys is just a minimal bash-like shell for Windows, which will prove really handy if you are use to Linux. Among its several advantages over Windows' native command-prompt is that it (msys) supports backticks. I'm not familiar with msys2 though.
    "Talk is cheap, show me the code" - Linus Torvalds

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,559
    From https://sourceforge.net/projects/msys2/
    MSYS2 is an independent rewrite of MSYS, based on modern Cygwin (POSIX compatibility layer) and MinGW-w64 with the aim of better interoperability with native Windows software.
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by migf1 View Post
    Cygwin or some mingw distro are your best bet. Cygwin is a better option for what you are asking, since it implements a Unix layer on top of Windows. Hence the required .dll distribution along with your executable (which is not a big deal btw). However, you are mostly bounded to use Cygwin's versions of whatever librararies/frameworks you are using. For exampe, GTK+ 's downlaod page for Windows makes it very explicit that what it is offered in that page is NOT meant for development under Cygwin (obviously Cygwin provides GTK through its own package-manager).

    I'm using both the original mingw (formely mingw32) and mingw-w64 (only for 64-bit executables). Lots of libraries recommend the former, and/or provide such binaries. It's pretty straight forward to install and use, because it does not have any variations like mingw-w64. But it is focused on Windows, thus it lacks support for some Unix stuff. Don't remember which exactly, by heart right now... I think posix threads is among them (anyway, you should check these things out before deciding between mingw and mingw-w64).

    mingw-w64 is a fork of mingw and if I remember correctly, it has been the standard Cygwin compiler for a while. It provides better Unix support, but it is a bit more complicated than plain mingw, since it provides several installation options, depending on what features you want (for example, you may choose between win32 and posix threads, or which exception model you want to use, etc).

    As for IDEs, Code::Blocks is very decent (way better than Dev-C++, original or Orwell) and it is available on both worlds... on Macs too, I think, don't remember). Thus you won't have to learn a different IDE if you switch platforms.

    msys is just a minimal bash-like shell for Windows, which will prove really handy if you are use to Linux. Among its several advantages over Windows' native command-prompt is that it (msys) supports backticks. I'm not familiar with msys2 though.
    Wow, that was extremely thorough and complete. It answers all my questions. I guess perhaps I should consider the Cygwin thing. I'm going to have to provide an installer and distribute DLLs anyway (for various libraries that I use, like GTK+, FontConfig, etc). I saw myself while I was trying to decide (before posting) that Cygwin needs a special version of GTK+. If I have to provide the Cygwin.dll file with each distribution, that means I'm either going to have to have two downloads, one for the installation file I make (probably called setup_ver.exe or something on Windows) and one for the .DLL or I'm going to have to use some third party installation package, like InstallShield (this is just an example, I haven't looked into it at all. I'm just throwing the name out because I know it's one such type of program).

    I had planned on writing a Windows Installation program for people, so my program would be accessible via the start menu. I can't really have a portable version, not with all the DLL's I'm going to have to redistribute anyways.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,978
    Think of Cygwin as providing a fairly full Posix emulation layer for your application code.

    MSYS2 also provides an emulation layer, but not for general redistribution. It "aims to provide support to facilitate using the bash shell, Autotools, revision control systems and the like for building native Windows applications using MinGW-w64 toolchains."

    I would not go with Cygwin unless the Posix emulation layer makes it much easier to port your code to Windows. Otherwise just use your favorite native IDE and tool-chain.

    gg

  9. #9
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Is there a way to compile 32-bit executables with the MinGW64-W32? I've tried
    Code:
    gcc -m32 -Wall -mms-bitfields -c main.c -o main.o
    and that works as expected. If I run file on main.o, I get:
    Code:
    main.o: MS Windows COFF Intel 80386 object file
    which shows that the compiler did in fact compile it as a 32-bit application, instead of PE32+ (which would be 64-bit). However, when I try to link, I run into all kinds of issues.
    Code:
    gcc main.o -o main.exe
    errors out about LD not knowing how to handle the 32-bit object file, which is expected. What are the linker flags I'm supposed to pass to LD and how do I pass them? I've tried a few things. Stuff like
    Code:
    gcc -Wl,-b pe-i386 main.o -o main.exe
    just gives me an error message about pe-i386 not being found. Any suggestions? I don't actually have to reinstall Mingw64-w32 again and pick the 32-bit version and have both versions installed at the same time on my system, do I? I know in Linux, I have an x86_64 system and I can easily compile stuff to run on the PS3 (PPC based CPU).

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,978
    Try adding -m32 to the link commands as well.

    gg

  11. #11
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Like this?
    Code:
    gcc -Wl,-m32 main.o -o main.exe
    or like this?
    Code:
    gcc -m32 main.o -o main.exe
    The second goes crazy, just like before. The first gets us closer, but errors out with:
    ..../../../../../x86_64-w64-mingw32/bin/ld.exe: unrecognised emulation mode: 32
    Supported emulations: i386pep i386pe
    collect2.exe: error: ld returned 1 exit status

    If I pick i386pep for a target, we get an error that implies this is never going to work:
    Code:
    gcc Wl,-mi386pep main.o -o main.exe
    
    C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/
    gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 architecture of input file `main.o' is incompatible with i386:x86-64 output
    main.o:main.c:(.text+0xa): undefined reference to `___main'
    main.o:main.c:(.text+0x2d): undefined reference to `_printf'
    main.o:main.c:(.text+0x60): undefined reference to `_putchar'
    main.o:main.c:(.text+0x8b): undefined reference to `_putchar'
    main.o:main.c:(.text+0xa4): undefined reference to `_putchar'
    main.o:main.c:(.text+0xc8): undefined reference to `_printf'
    C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x25): undefined reference to `WinMain'
    collect2.exe: error: ld returned 1 exit status

    If I pick i386pe for the target, I get a LOT of messages, mainly they're all the same though:
    Code:
    gcc Wl,-mi386pe main.o -o main.exe
    
    C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping  incompatible C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/lib/..
    /lib/libmingw32.a when searching for -lmingw32
    
    C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping
     incompatible C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/lib/..
    /lib\libmingw32.a when searching for -lmingw32
    
    C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping  incompatible C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/lib/libmingw32.a when searching for -lmingw32
    
    C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping  incompatible C:/Program Files/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/lib\libmingw32.a when searching for -lmingw32
    It just repeats a lot of times. I've extracted the object files from libmingw32.a and file says they're ACB archive data. I bet I'm going to have to reinstall the compiler and pick 32-bit for a target and just have two separate compilers. Might be best that way anyway, so then it'd be easier keeping track of where the 32-bit libraries are vs the 64-bit libraries.

  12. #12
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    If I recall correctly, older SJLJ (exception model) distros of mingw-w64 were indeed dual target via the -m32 flag, but more recent distros of all exception models are single target. If I do remember correctly, then I guess you have indeed to install both the 32bit and the 64bit versions of your chosen distro.
    "Talk is cheap, show me the code" - Linus Torvalds

  13. #13
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by migf1 View Post
    If I recall correctly, older SJLJ (exception model) distros of mingw-w64 were indeed dual target via the -m32 flag, but more recent distros of all exception models are single target. If I do remember correctly, then I guess you have indeed to install both the 32bit and the 64bit versions of your chosen distro.
    Yeah, it seems you might be correct there. I found some MinGW64 stuff for Linux that allows me to cross-compile Windows executables. It was written back in 2011. Compile for Windows on Linux | BlogCompiler The instructions given have you download a 32-bit compiler (which was last updated back in 2013) and a 64-bit compiler (I don't know when that was last updated). They run on Linux but produce Windows executables. Might be the best way to go. It just seems the information might be a bit outdated and there might be newer versions out there. I can always just reexecute the installer on Windows and pick the 32-bit target. Hopefully it doesn't overwrite anything and I can have both installed at the same time on the same machine.

  14. #14
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    If you don't get too fancy with your app, you may get away with plain mingw instead of mingw-w64. GTK+ (glib, etc) is definitely working just fine with mingw, and if the library you mention in the other thread has indeed #ifdef separated code specifically for Windows, it might as well compile fine with plain mingw on Windows.

    The downside would be that you won't be able to cross-compile from the linux host (I think the default mingw distro does not provide such a feature, but I haven't checked recently) . Also you'll restrict the executable to 32bits only.

    But it will probably simplify development & distribution for Windows.

    If however your libraries use mingw-w64 specific stuff, perhaps the best option is to install both 32bit and 64bit versions of the mingw-w64 toolchain, and compile your app respectively. If mingw-w64 is inadequate too, then I guess Cygwin would be your last bet.
    Last edited by migf1; 08-20-2015 at 05:14 PM.
    "Talk is cheap, show me the code" - Linus Torvalds

  15. #15
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Thanks Migf1. Yeah, I decided to go for the dual install on Windows. I was looking for an updated version of the 32-bit and the 64-bit MinGW-W64, for Linux, to create all 3 versions of the program on the one Linux box. Right now, I can compile it on Linux, I can use the same Makefile to copy a 32-bit version or a 64-bit version on Windows.

    Ultimately, compiling on one system would be optimal but for now, I can definitely live with this. I'll check out the code blocks stuff come tomorrow. I think I've taken my Makefile about as far as I'd like to go with for now. It was a bit of a pain. Been awhile since I wrote one. Thanks for all the help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good open-source socket API that supports IPV6?
    By Sebastiani in forum C++ Programming
    Replies: 16
    Last Post: 07-14-2014, 11:49 AM
  2. What is a good c++ compiler for windows xp
    By BrandNew in forum C++ Programming
    Replies: 6
    Last Post: 03-11-2011, 07:09 AM
  3. Good compiler for windows
    By behzad_shabani in forum C Programming
    Replies: 21
    Last Post: 06-17-2008, 07:45 AM
  4. Good Linux GUI Compiler
    By geek@02 in forum Linux Programming
    Replies: 5
    Last Post: 09-29-2005, 01:19 PM
  5. Wheres a good linux C compiler?
    By Budgiekarl in forum Linux Programming
    Replies: 9
    Last Post: 06-18-2003, 01:17 PM