How to compile for Windows on Linux with gcc/g++? - c++

I have written some effects in C++ (g++) using freeglut on Linux, and I compile them with
g++ -Wall -lglut part8.cpp -o part8
So I was wondering if it is possible to have g++ make static compiled Windows executables that contains everything needed?
I don't have Windows, so it would be really cool, if I could do that on Linux :)

mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32, for example.
Ubuntu, for example, has MinGW in its repositories:
$ apt-cache search mingw
[...]
g++-mingw-w64 - GNU C++ compiler for MinGW-w64
gcc-mingw-w64 - GNU C compiler for MinGW-w64
mingw-w64 - Development environment targeting 32- and 64-bit Windows
[...]

Suggested method gave me error on Ubuntu 16.04: E: Unable to locate package mingw32
===========================================================================
To install this package on Ubuntu please use following:
sudo apt-get install mingw-w64
After install you can use it:
x86_64-w64-mingw32-g++
Please note!
For 64-bit use: x86_64-w64-mingw32-g++
For 32-bit use: i686-w64-mingw32-g++

One option of compiling for Windows in Linux is via mingw. I found a very helpful tutorial here.
To install mingw32 on Debian based systems, run the following command:
sudo apt-get install mingw32
To compile your code, you can use something like:
i586-mingw32msvc-g++ -o myApp.exe myApp.cpp
You'll sometimes want to test the new Windows application directly in Linux. You can use wine for that, although you should always keep in mind that wine could have bugs. This means that you might not be sure that a bug is in wine, your program, or both, so only use wine for general testing.
To install wine, run:
sudo apt-get install wine

Install a cross compiler, like mingw64 from your package manager.
Then compile in the following way: instead of simply calling gcc call i686-w64-mingw32-gcc for 32-bit Windows or x86_64-w64-mingw32-gcc" for 64-bit Windows. I would also use the --static option, as the target system may not have all the libraries.
If you want to compile other language, like Fortran, replace -gcc with -gfortran in the previous commands.

I've used mingw on Linux to make Windows executables in C, I suspect C++ would work as well.
I have a project, ELLCC, that packages clang and other things as a cross compiler tool chain. I use it to compile clang (C++), binutils, and GDB for Windows. Follow the download link at ellcc.org for pre-compiled binaries for several Linux hosts.

From: https://fedoraproject.org/wiki/MinGW/Tutorial
As of Fedora 17 it is possible to easily build (cross-compile) binaries for the win32 and win64 targets. This is realized using the mingw-w64 toolchain: http://mingw-w64.sf.net/. Using this toolchain allows you to build binaries for the following programming languages: C, C++, Objective-C, Objective-C++ and Fortran.
"Tips and tricks for using the Windows cross-compiler": https://fedoraproject.org/wiki/MinGW/Tips

For Fedora:
# Fedora 18 or greater
sudo dnf group install "MinGW cross-compiler"
# Or (not recommended, because of its deprecation)
sudo yum groupinstall -y "MinGW cross-compiler"

Related

How to get "g++ -mx32" to work on RHEL 7.2

I am new to x64_86, but forced to use it because RedHat dropped its 32-bit OS support in RHEL 7.x. I have to complile a lot of code, and am not ready to jump to x64 yet (because I do not need 64-bit addresses and do not want to face all related porting issues). So I have considered using -m32 and -mx32, and decided that -mx32 is the best route for me. However, while -m32 works fine on my build machine, when I use -mx32, I get this error:
In file included from /usr/include/features.h:399:0,
from /usr/include/string.h:25,
from zz.cpp:1:
/usr/include/gnu/stubs.h:13:28: fatal error: gnu/stubs-x32.h: No such file or directory
# include <gnu/stubs-x32.h>
^
compilation terminated.
I searched the web for solutions and some links indicate that I have to install some mysterious "multilib" rpms for g++ and gcc, however, I cannot find these anywhere. Others suggest that I have to install Linux in the x32 mode and build libgcc for x32, which sound extreme. Any ideas or leads? Did someone actually try g++ -mx32? Maybe it is not even supported on the RH platform... Thanks!
P.S. In order to get the "-m32" option to work I had to install:
yum install glibc-devel.i686 libgcc.i686 libstdc++-devel.i686 ncurses-devel.i686
This one fails (yum cannot find these RPMs) - allegedly these are required for -mx32 to work:
yum install gcc-multilib g++-multilib
:(
Multilib is indeed your answer, but do not know why your repo does not support it. I installed mine via apt-get:
sudo apt-get install gcc-multilib
Although it uses 64-bit instructions, it uses the 32-bit ABI so annoyingly will not run under WSL (Windows Linux subsystem), which only supports the 64-bit one.

mingw without g++ compiler

i am new to linux and I have the following question:
I am trying to install MINGW in Ubuntu.
I ran the command:
sudo apt-get install mingw-w64
It was installed, and if i put the command gcc it runs ok. The problem is g++ command does not work. I guess it is because i don't have the c++ compiler (as I read in similar questions in stackoverflow).
I read too that you can use the next command:
mingw-get install g++
but i don't have the executable program for this command.
My question is, how can I install that executable? or is there another way to update my mingw so I can use the g++ compiler?
Hope I have explained myself correctly. Thank you for any help I receive.
mingw-get is a windows specific package manager, it is not needed when you use a native linux package manager such as APT.
Installing package mingw-w64 depends on package g++-mingw-w64, which depends on g++-mingw-w64-i686 and g++-mingw-w64-x86-64.
These packages install the mingw cross compilers as
/usr/bin/x86_64-w64-mingw32-g++-posix
/usr/bin/x86_64-w64-mingw32-g++-win32
/usr/bin/i686-w64-mingw32-g++-posix
/usr/bin/i686-w64-mingw32-g++-win32
Older versions of mingw cross compiler shipped /usr/bin/i586-mingw32msvc-c++, which is replaced by i686-w64-mingw32-c++-win32
You can usually use this toolchain in a project by running ./configure CXX=i686-w64-mingw32-c++-win32 or make CXX=i686-w64-mingw32-c++-win32
Note: the above description is correct for the most recent toolchain in Debian unstable. It may need some minor tweaking for older systems.

C++ compiler/SDK for Ubuntu 12.04 - Netbeans

I have installed Netbeans 7.1 on Ubuntu. When I was activating C++ in netbeans, it didn't lead me to the page where C++ compiler/SDK exists, as it did in windows. I am using Cygwin in windows, but seems like it is not there for Linux.
Please guide me to the correct location where I can find correct C++ compiler/SDK for Linux, which I can use in Netbeans.
You need to install gcc
gcc is the gnu c and c++ compiler
simply open a console and type:
sudo apt-get install build-essential
CPP setup instructions for netbeans:
http://netbeans.org/community/releases/60/cpp-setup-instructions.html

target mingw with cygwin

I've tried building the gtest 1.5 library for MinGW
Using MSys to configure and then make results in a lot of errors, most about Win32API
On their website it says mingw is not supported but in the FAQ it is specified that a user managed to build it for mingw from cygwin configuring like this:
./configure CC="gcc -mno-cygwin" CXX="g++ -mno-cygwin"
This configure fails for me when running a sanity check for the C++ preprocessor ... something about "/lib/cpp".
I've tried a cygwin install with only mingw-gcc4 (4.3.4) and a cygwin install with mingw-gcc4 (4.3.4) and cygwin's own gcc (~3.4.4).
Generally, the library could be smoothly configured and built with cygwin gcc (3.4.4) using the flags you have specified above. You should only validate that gcc-mingw, gcc-mingw-core & gcc-mingw-g++ packages are installed in cygwin. There is no need in mingw-gcc4

Where to download GNU C++ compiler

Can anyone suggest me where to download a GNU c++ compiler, which I can use in Ubuntu and also on Windows with Netbeans IDE, and also GNU tools.
Getting everything you need on Debian/Ubuntu is very easy:
sudo apt-get install build-essential
What about using GCC ?
Quoting it's homepage :
The GNU Compiler Collection includes
front ends for C, C++, Objective-C,
Fortran, Java, and Ada, as well as
libraries for these languages
(libstdc++, libgcj,...).
It will definitly work on Ubuntu ; it's even provided through the package system (sorry, my system is in french) :
$ apt-cache show g++
Package: g++
...
Description-fr: Compilateur C++ du projet GNU
Le compilateur C++ du projet GNU. Un compilateur C++ relativement portable
et capable de bonnes optimisations.
(which translates to Description: The GNU C++ compiler. This is the GNU C++ compiler, a fairly portable optimizing compiler for C++. in English)
For informations about Windows support, you can have a look at : http://gcc.gnu.org/install/specific.html#windows
Something like MinGW or Cygwin will probably do :-)
Quoting MinGW's homepage :
MinGW, a contraction of "Minimalist
GNU for Windows", is a port of the GNU
Compiler Collection (GCC), and GNU
Binutils, for use in the development
of native Microsoft Windows
applications.
For netbeans, I can't tell : I don't use it -- not for C++, at least...
For windows, you will want cygwin.
Use sudo apt-get install g++ on Ubuntu.
If you are using any Linux/Unix/Solaris OS it is available unless you have explicitly not installed. That said, if you still wish to install GNU C++ compiler, use this command
sudo aptitude install build-essential
and if you wish to download it on your windows, steps are here on Minimalist GNU for Windows
Try http://gcc.gnu.org/ and http://packages.ubuntu.com/hardy/gcc
To install the gcc and g++ compilers, you will need the build-essential package. This will also install GNU make.
build-essential contains a list of packages which are essential for building Ubuntu packages including gcc compiler, make and other required tools.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential
$ gcc -v
$ make -v
Now, you should able to compile software using C / C++ compilers.
To install the manpages for c and c++ development, install the manpages-dev package.
https://help.ubuntu.com/community/InstallingCompilers