DæmonNews: News and views for the BSD community

Daemon News Ezine BSD News BSD Mall BSD Support Forum BSD Advocacy BSD Updates

Cross compiling from FreeBSD to Windows

David Yeske <dyeske@daemonnews.org>

This article explains how to cross compile from FreeBSD to Windows. This is possible because of the MinGW compiler which has been ported to FreeBSD and exists in the ports collection. We will use the PuTTY program, a gui ssh suite of utilities, as an example and cross compile it to run on windows. We will step you through modifying the existing FreeBSD ports framework to make this work. After the initial modification, other ports could be used to create windows binaries.

Cross compiling directions

This cookbook style guide will allow you to quickly begin cross compiling programs for windows.

  • The first thing you need to do is install the mingw port. This is the compiler that creates the binaries that run on windows.
    # cd /usr/ports/devel/mingw32-gcc
    # make clean install
    
  • Next, we install ccache. This will speed up the cross compilation.
    # cd /usr/ports/devel/ccache
    # make clean install
    
  • Then we create a wrapper for ccache. This simple shell script will make it easier to compile using ccache. These two lines should be saved in the file /usr/local/mingw32/bin/ccache_cc Don't forget to use chmod to make it executable.
    #!/bin/sh
    exec ccache /usr/local/mingw32/bin/gcc "${@}"
    
  • Now create a directory and place this Makefile in it. This Makefile is will work with the existing FreeBSD ports collection.
  • In order for this Makefile to work, a modification must be made to the ports collection. The following code should be stored in a file called /usr/ports/Mk/bsd.win32.mk or the existing file /usr/ports/Mk/bsd.port.mk can be modified so the USE_WIN32=yes statement in the Makefile will cause the following to be defined, but that will take quite a bit more effort on your part to get right.
    BUILD_DEPENDS+= ${LOCALBASE}/mingw32/bin/gcc:${PORTSDIR}/devel/mingw32-gcc
    CC= ${LOCALBASE}/mingw32/bin/gcc
    CXX=    ${LOCALBASE}/mingw32/bin/g++
    DLLWRAP=${LOCALBASE}/mingw32/bin/dllwrap
    RANLIB= ${LOCALBASE}/mingw32/bin/ranlib
    WINDRES=${LOCALBASE}/mingw32/bin/windres
    
  • Finally, you just need to compile PuTTY like a normal FreeBSD port. Do not run "make install", because it will not work.
    # cd putty
    # make CC=/usr/local/mingw32/bin/ccache_cc
    
  • Results

    $ file *.exe
    pageant.exe:  MS Windows PE 32-bit Intel 80386 GUI executable not relocatable
    plink.exe:    MS Windows PE 32-bit Intel 80386 console executable not relocatable
    pscp.exe:     MS Windows PE 32-bit Intel 80386 console executable not relocatable
    psftp.exe:    MS Windows PE 32-bit Intel 80386 console executable not relocatable
    putty.exe:    MS Windows PE 32-bit Intel 80386 GUI executable not relocatable
    puttygen.exe: MS Windows PE 32-bit Intel 80386 GUI executable not relocatable
    puttytel.exe: MS Windows PE 32-bit Intel 80386 GUI executable not relocatable
    

    The resulting binarys can be run in Windows or under Wine.

    Acknowledgments

    I would like to thank the authors of MinGW and the various people that gave me feedback.


    $Id: crosscompile,v 1.20 2004/09/09 05:58:18 www Exp $
Google
Web daemonnews.org

More Articles
  • Interview with Jan Schaumann
  • Interview with Theo de Raadt
  • Book Review: Virtualization with VMware ESX Server
  • Editorial: Not Quite Dead Yet
  • The Design of OpenBGPd
  • Interview with der Mouse
  • Letter to Steve Jobs
  • Interview with Manuel Bouyer on Xen
  • Apple and Open Source
  • BSDCan 2006
  • BSD Certification Survey Results
  • Lab in a Box
  • Ike Notes on BSDCan 2005
  • BSDCan 2005 Photos
  • FreeBSD Developer Summit Pictures

  • Advertisements




    Author maintains all copyrights on this article.
    Images and layout Copyright © 1998-2006 Dæmon News. All Rights Reserved.