Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Building a cross compiler (compile on Linux, run on Windows)
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
gralves
Guru
Guru


Joined: 20 May 2003
Posts: 389
Location: Sao Paulo, Brazil

PostPosted: Wed Jul 09, 2003 7:30 am    Post subject: Building a cross compiler (compile on Linux, run on Windows) Reply with quote

Building a cross compiler (compile on Linux, run on Windows)

Introduction
Ever had those programming assignements from professors who can only run windows? Tired of spending expensive Gbs of HD just to keep a runing copy of windoze so you can compile your programs for windows? Spending hours booting the system just to find an uninitialized pointer bug? Want to change your operational system behaviour using buffer overflow? YOUR PROBLEMS ARE OVER!

Whatever are your needs this guide will teach you how to build mingw32 on linux. It can be used to compile C and C++ code for windows machines. But be aware, linux system calls will not work (like fork, system, etc).

Before you begin you will need...

  • A copy of binutils source code
    This can be found in several places. If you have installed gentoo from stage 1 you probably have o copy of it on your distfiles directory. Otherwise you can get them by emerging binutils or downloading them from here.
  • A copy of gcc source code
    Same as binutils. You can also get it here.
  • A copy of the precompiled win32 api (they need to be compiled for win32 and you don't have a compiler yet :) )
    This one can be found on MinGW site on the download page. Click here for a direct download link.
  • A copy of the precompile mingw32 runtime library
    Same as above. The direct link is here.


Let the fun begin...


  1. Decompress everything on the right place.
    Easier said than done...
    You have to decompress everything in specific directories for it to work. It took me sometime to figure these out. First choose an location for the binaries/headers you you compile. I choosed /usr/local/cross-tools. Any directory will work. You will also have to choose a temporary directory for the compilling job. I used /root/cross. PS: You don't need to be root to do this unless you want the binaries available to everyone on the system.

    After choosing the directories it's time to start decompressing. Copy everyone of the tars to /root/cross/source and:
    Code:

    mkdir /usr/local/cross-tools/i386-mingw32msvc
    cd /usr/local/cross-tools/i386-mingw32msvc
    gzip -dc "/root/cross/source/mingw-runtime=3.0.tar.gz" | tar xf -
    gzip -dc "/root/cross/source/w32api-2.3.tar.gz" | tar xf -
    cd /root/cross/source
    gzip -dc "/root/cross/source/binutils-2.13.90-20030111-1-src.tar.gz" | tar xf -
    gzip -dc "/root/cross/source/gcc-3.2.3-20030504-1.tar.gz" | tar xf -


  2. Configure binutils
    You will want your source code for binutils and gcc imaculate in case you make something wrong in the LONG configuring/making process. Starting over is no fun. So lets create some directories to store the configuration.
    Code:

    mkdir /root/cross/binutils-i386-mingw32msvc
    mkdir /root/cross/gcc-i386-mingw32msvc

    Then thanks God for bash auto-completion command (TAB) and run these commands(be sure to change the directories for your configuration):
    Code:

    cd /root/croos/binutils-i386-mingw32msvc
    /root/cross/source/binutils-2.13.90-20030111-1-src/configure --prefix=/usr/local/cross-tools --target=i386-mingw32msvc &> configure.log

  3. Build and install binutils
    This one is easy!
    Code:

    make &>make.log

    After some long time, when its over do a cat make.log to make sure everything went ok. If yes all you need to do is:
    Code:

    make install &>make_install.log

    Again check the log file. If everything is OK you are on your way to go!
  4. Export binutils path
    Otherwise gcc won't be able to find it! I had some trouble with that...
    Code:
    export PATH=$PATH:/usr/local/cross-tools/bin

  5. Configure gcc
    Code:

    cd /root/croos/binutils-i386-mingw32msvc
    /root/cross/source/gcc-3.2.3-20030504-1/configure -v --prefix="/usr/local/cross-tools" --target=i386-mingw32msvc --with-headers="/usr/local/cross-tools/i386-mingw32msvc/include" --with-gnu-as --with-gnu-ld --without-newlib --disable-multilib &> configure.log

    Check the log to see if everything went ok.
  6. Build and install gcc
    Code:

    make &> make.log
    make install &> make_install.log

    Don't forget to check the logs!
  7. Clean the sys-includes
    These are used by the gcc build process and are not needed to compile anything else.
    Code:

    rm -rf "/usr/local/cross-tools/i386-mingw32msvc/sys-include



Using your new toy
To compile:
Code:

i386-mingw32msvc-gcc <source_name>.c -o <target_name>


Have Fun!
Back to top
View user's profile Send private message
grudge
Tux's lil' helper
Tux's lil' helper


Joined: 26 Oct 2002
Posts: 77
Location: South Africa

PostPosted: Wed Jul 09, 2003 2:07 pm    Post subject: Reply with quote

Cool, this is one of the few reasons left why I still have windows on my machine. I'll check it out tonight and see how well it works. Thanks for the tutorial. :lol:
Back to top
View user's profile Send private message
gralves
Guru
Guru


Joined: 20 May 2003
Posts: 389
Location: Sao Paulo, Brazil

PostPosted: Wed Jul 09, 2003 6:57 pm    Post subject: Reply with quote

The only issue I had so far is that the compiled executable won't run correctly under wine (it seems that the C I/O functions don't work) but on a windows machine it runs flawlessly.

Problably this is just a matter of putting the .dll for mingw32 somewhere where wine can find it.

I've also found some instructions on how to build directx and opengl code under linux for win32 on http://www.libsdl.org/ (it was from there that I got the idea) but their scripts are somewhat old and I had to read the code to find out how to compile a new version.
Back to top
View user's profile Send private message
akiross
Veteran
Veteran


Joined: 02 Mar 2003
Posts: 1170
Location: Mostly on google.

PostPosted: Sun Jul 13, 2003 11:17 am    Post subject: Reply with quote

thanx for this tutorial :)
it will be userful!

And, if someone wants to create simple apps with GUI interface, portable under windows, he can use this libraries, which are v simple to use
http://www.wxwindows.org/

bye
_________________
Libera scienza in libero stato.
Horizon of Events
Back to top
View user's profile Send private message
akiross
Veteran
Veteran


Joined: 02 Mar 2003
Posts: 1170
Location: Mostly on google.

PostPosted: Fri Aug 29, 2003 4:21 pm    Post subject: Reply with quote

hei, i got an error:
Code:

/usr/local/dev-cross/i386-mingw32msvc/lib//libmingw32.a(main.o)(.text+0x97):main.c: undefined reference to `WinMain@16'


what is this?
it's the first time i get it... (after install SDL libraries)

what if can be?
thanks
bye
_________________
Libera scienza in libero stato.
Horizon of Events
Back to top
View user's profile Send private message
gralves
Guru
Guru


Joined: 20 May 2003
Posts: 389
Location: Sao Paulo, Brazil

PostPosted: Sat Aug 30, 2003 3:42 am    Post subject: Reply with quote

akiross wrote:
hei, i got an error:
Code:

/usr/local/dev-cross/i386-mingw32msvc/lib//libmingw32.a(main.o)(.text+0x97):main.c: undefined reference to `WinMain@16'


what is this?
it's the first time i get it... (after install SDL libraries)

what if can be?
thanks
bye


It is a linking eror. It can't find the WinMAin@16 function... I don't know what it might be. You can try the MinGW forums...
Back to top
View user's profile Send private message
zhenlin
Veteran
Veteran


Joined: 09 Nov 2002
Posts: 1361

PostPosted: Sat Aug 30, 2003 5:36 am    Post subject: Reply with quote

WinMain@16 is the entry point, like main()
Back to top
View user's profile Send private message
MrStaticVoid
Tux's lil' helper
Tux's lil' helper


Joined: 25 Jul 2003
Posts: 114
Location: Maryland

PostPosted: Mon Sep 01, 2003 1:44 pm    Post subject: Reply with quote

Wow...what a great tip! However, I found instructions including a script which I used to build the compiler. It does almost exactly the same thing as gralves explained, but it also included the Windows OpenGL and DirectX libraries and it was all automated. It was nice to just start the script and go to bed and wake up with a cross-compiler.
Back to top
View user's profile Send private message
akiross
Veteran
Veteran


Joined: 02 Mar 2003
Posts: 1170
Location: Mostly on google.

PostPosted: Tue Sep 02, 2003 9:46 am    Post subject: Reply with quote

Hi!
I solve the problem with SDL
u need to pass the following library when link:

-lmingw32 -lSDLmain -lSDL (other libraries)

bye
_________________
Libera scienza in libero stato.
Horizon of Events
Back to top
View user's profile Send private message
daysleper
n00b
n00b


Joined: 30 Mar 2003
Posts: 4
Location: Norway

PostPosted: Mon Jan 19, 2004 1:01 pm    Post subject: Reply with quote

It's also possible to run MinGW under wine.

Just head over to the mingw website and download "MinGW-3.1.0-1.exe". Then execute the installer under wine:

Code:
wine -- MinGW-3.1.0-1.exe


Then you can compile to a Win32-binary like this:
Code:
wine -- g++ hello.cpp -o hello.exe


..and run it like this:
Code:
wine -- hello.exe
Hello Windows!


Nice -- eh?

If you want other stuff like .. cvs and make .. there's also a msysDTK-1.0.1.exe file you can download and install. :)

Edit: On second thought -- you could probably use the cvs and make already installed under Linux (I just did).
Back to top
View user's profile Send private message
kormoc
Apprentice
Apprentice


Joined: 17 Jun 2002
Posts: 250

PostPosted: Wed Jan 21, 2004 4:41 pm    Post subject: Reply with quote

Deleted

Last edited by kormoc on Mon Dec 24, 2018 9:05 am; edited 1 time in total
Back to top
View user's profile Send private message
Vanquirius
Retired Dev
Retired Dev


Joined: 14 Jun 2002
Posts: 1297
Location: Ethereal plains

PostPosted: Wed Jan 21, 2004 8:41 pm    Post subject: Reply with quote

@kormoc

The ebuilds will work, and are probably an easier method. But according to my memory and the ChangeLog of xmingw-gcc

ChangeLog wrote:
01 Oct 2003; Stefan Jones <:gentoo.org> :
Initial ebuild
,

such ebuilds were not available at the time this guide was written.
_________________
Hello.
Back to top
View user's profile Send private message
kormoc
Apprentice
Apprentice


Joined: 17 Jun 2002
Posts: 250

PostPosted: Wed Jan 21, 2004 9:22 pm    Post subject: Reply with quote

Deleted

Last edited by kormoc on Mon Dec 24, 2018 9:05 am; edited 1 time in total
Back to top
View user's profile Send private message
gralves
Guru
Guru


Joined: 20 May 2003
Posts: 389
Location: Sao Paulo, Brazil

PostPosted: Thu Jan 22, 2004 3:37 am    Post subject: Reply with quote

Thanks for posting the ebuilds... I hadn't checked on it for sometime and it will be a lot easier to maintain my system uptodate with them.
Back to top
View user's profile Send private message
capheind
n00b
n00b


Joined: 26 Mar 2004
Posts: 39
Location: bakersfield CA

PostPosted: Fri Apr 02, 2004 3:57 pm    Post subject: Reply with quote

Yeah emerge xmingw-gcc doesn't work on mine. Anyone else having any luck. I just get:

!!! ERROR: dev-util/xmingw-gcc-3.3.1 failed.
!!! Function src_install, Line 69, Exitcode 2
!!! make install failed
_________________
-Troy Leveille-dit-truchon
Back to top
View user's profile Send private message
docbill
n00b
n00b


Joined: 18 May 2004
Posts: 9

PostPosted: Wed May 26, 2004 7:51 pm    Post subject: How about compiling for Linux under Windows? Reply with quote

I'm looking for trick to do just the reverse. I want to be able to compile Linux applications using Cygwin. The reason you ask? For now it just to see if it can be done.

Bill
Back to top
View user's profile Send private message
SeeksTheMoon
Apprentice
Apprentice


Joined: 24 Sep 2003
Posts: 163

PostPosted: Tue Jul 13, 2004 9:30 am    Post subject: Reply with quote

I searched this forum and tested a little bit how to exactly use the xmingw-compiler.

1. dev-util/xmingw-runtime && dev-util/xmingw-w32api have to be emerged BEFORE you emerge the xmingw-gcc or you cannot create c++ code!

2. add /opt/xmingw/bin to your path with
Code:
export PATH=$PATH:/opt/xmingw/bin

and you have to add the xmingw-headers to the CPPFLAGS:
Code:
export CPPFLAGS="-I /opt/xmingw/i386-mingw32msvc/include"

or xmingw will not find windows.h and the other headers.
When you have installed DirectX SDK on a Windows-Partition it might work when you mount this partition and add -I <PATH> to CPPFLAGS for every include-directory you need.
It might be usefull to use gcc's flag "-fms-extensions", which "accepts some non-standard constructs used in Microsoft header files." when you are doing this.

3. simple code can be compiled with
Code:
i386-mingw32msvc-g++ hello.cpp -o hello.exe

where hello.cpp is
Code:
#include <iostream>
using namespace std;

int main() {
cout<<"Hello World!"<<endl;
return 0;
}


You can test hello.exe with wine or winex.
("i386-mingw32msvc-g++" sucks, you may create a symlink named /usr/bin/gw32 if you want.)

4. When you invoke configure-scripts and want to cross-compile, then use
Code:
./configure --target=i386-mingw32msvc --host=i386-mingw32msvc --build=i386-linux --prefix=/opt/xmingw/i386-mingw32msvc

with the other configure-parameters you may need.

Check the output for lines like
Code:

checking host system type... i386-pc-mingw32msvc
...
checking whether we are cross compiling... yes
...
checking for i386-mingw32msvc-g++... g++
...
checking for i386-mingw32msvc-ranlib... i386-mingw32msvc-ranlib
checking for i386-mingw32msvc-dlltool... i386-mingw32msvc-dlltool
checking for i386-mingw32msvc-dllwrap... i386-mingw32msvc-dllwrap
checking for i386-mingw32msvc-windres... i386-mingw32msvc-windres
checking for i386-mingw32msvc-objcopy... i386-mingw32msvc-objcopy
...
checking for windows.h... yes
...

and so on.

5. complex programs may have a special Makefile for mingw. Use this one instead of the normal Unix-Makefile:
Code:
make -f Makefile.mingw
Back to top
View user's profile Send private message
sibisco
n00b
n00b


Joined: 22 Jul 2004
Posts: 3

PostPosted: Thu Jul 22, 2004 12:32 pm    Post subject: cross compiler Reply with quote

hello!
My aim is to build a cross compiler from linux to windows. I have tested your application but I have a little problem to compile my test file. The linux prompt doesn't accept the command.

[root@dyn-9-147-232-208 root]# i686-pc-mingw32-gcc test.c -o i686-pc-mingw32
bash: i686-pc-mingw32-gcc: command not found

Can you help me to solve my problem.

Thanks
Back to top
View user's profile Send private message
SeeksTheMoon
Apprentice
Apprentice


Joined: 24 Sep 2003
Posts: 163

PostPosted: Fri Aug 06, 2004 2:11 pm    Post subject: Reply with quote

it is "i386-mingw32msvc-gcc", not "i686-mingw32msvc-gcc"
Back to top
View user's profile Send private message
sibisco
n00b
n00b


Joined: 22 Jul 2004
Posts: 3

PostPosted: Mon Aug 09, 2004 9:33 am    Post subject: Reply with quote

thanks for your help!
I have solved my problem : it was due to the PATH variable.
But now I have a new problem :
How use a makefile with a cross-compiler?
when you write a makefile, does the common "make" is compiled by the gcc of my cross compiler or compiled by the default gcc installed on my system?
Back to top
View user's profile Send private message
irondog
l33t
l33t


Joined: 07 Jul 2003
Posts: 715
Location: Voor mijn TV. Achter mijn pc.

PostPosted: Tue Aug 10, 2004 8:44 pm    Post subject: Reply with quote

Programming qt applications for windows in Linux would be cool.
The problem: qt for windows is not free.
A possible solution: http://kde-cygwin.sourceforge.net/qt3-win32/index.php

:) They made a windows version of QT by replacing X11 functions with win32 functions using the GPL qt/x11. :)

Here is a similar howto, which describes how to compile qt applications for windows (using a crosscompiler in Linux):
http://www.technosis.de/mingw/crosscompile/

Most important is this line:
Quote:
You need the "Qt for Windows" source code and of cource a licence for Qt Windows!!


So, taking this information together we can do this:
Compile Qt/win32 using cygwin, leave windows and write a crossplatform application in Linux with the power of C/C++ and Qt for Free.
_________________
Alle dingen moeten onzin zijn.
Back to top
View user's profile Send private message
sibisco
n00b
n00b


Joined: 22 Jul 2004
Posts: 3

PostPosted: Fri Aug 13, 2004 8:24 am    Post subject: cross compiler with various targets Reply with quote

Hi!
I have some questions about the cross compiling :
when we make a cross compiler, I think it's possible to have various targets, but how to do when the targets are very vast ?
I explain : I have to make a cross compiler which targets are AIX and Linux. How can I do to manage the different versions (mandrake 9.1 or red hat, AIX 5.1 or AIX 5.2...) ?
Thanks!
Back to top
View user's profile Send private message
salfter
n00b
n00b


Joined: 02 Jan 2003
Posts: 62

PostPosted: Fri Feb 23, 2007 6:10 pm    Post subject: This is obsolete information Reply with quote

The xmingw-* ebuilds are long gone. Something like
Code:
echo sys-devel/crossdev ~arch >>/etc/portage/package.keywords && emerge crossdev && crossdev i586-mingw32
is what you want to use to build a cross-compiler for building Windows apps.
Back to top
View user's profile Send private message
mariourk
l33t
l33t


Joined: 11 Jul 2003
Posts: 807
Location: Urk, Netherlands

PostPosted: Thu Mar 01, 2007 11:14 am    Post subject: Reply with quote

I've written a very small and basic program that I normally compile like this:
Code:

g++ *.h *.cc -o output-file

Now I want to compile it for windows. But I'm running into trouble with some includes.
The compiles says it cannot find those files. The only includes I'm using are:
Code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

Could anyone point me in the right direction?
_________________
If there is one thing to learn from history, it's that we usualy don't learn anything from it, at all.
Back to top
View user's profile Send private message
Colonel Paneek
n00b
n00b


Joined: 06 Mar 2003
Posts: 42

PostPosted: Sun Apr 15, 2007 2:37 am    Post subject: Reply with quote

mariourk wrote:
I've written a very small and basic program that I normally compile like this:
Code:

g++ *.h *.cc -o output-file

Now I want to compile it for windows. But I'm running into trouble with some includes.
The compiles says it cannot find those files. The only includes I'm using are:
Code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

Could anyone point me in the right direction?


The header files for mingw32 are in /usr/mingw32/usr/include

Code:
emerge sys-devel/crossdev && crossdev mingw32
equery files cross-mingw32/w32api | grep include
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum