6

I have written a tiny application for Ubuntu, and it works great. I am interested in cross-compiling it for Windows.

I have installed mingw32 from repository, and it compiles simple files very well. However, the app I want to compile for Windows uses some UNIX specific mechanism - for example it makes use of the sys/socket.h file, and the mingw compiler claims it can't find it.

One solution is to install Cygwin on a Windows machine, and try compiling the application from there, using Cygwin executables. Unfortunately, I don't have access to any Windows machine, as I don't own a copy that system. It would be perfect if I somehow could use Cygwin (with it's libraries and headers) on my Ubuntu to compile an .exe file.

There is no package with it in the repositories. Can anyone give me some on how to use Cygwin on Ubuntu?

2
  • 3
    CygWin is designed to allow you to run Linux programs in Windows. It does not exist in the repositories, because you're already on Linux. According to the Wine AppDB ( appdb.winehq.org/… ) you can run cygwin within Wine. I cannot confirm or reject whether this runs 100% though, because I also own Windows and can test on that :P
    – Thomas Ward
    Mar 6, 2012 at 15:26
  • @ThomasWard No; "Cygwin runs Linux programs in Windows" is a misconception. Cygwin binaries are Windows binaries.
    – fkraiem
    Dec 16, 2019 at 19:32

3 Answers 3

5

Cygwin is a windows program that allows you to run linux programs in windows, so it wouldn't make any sense to have it in the repositories.

I suspect your only chance of having this work would be to run cygwin in wine.

It sounds like you have two separate questions:

  1. Can I use run Cygwin under Ubuntu to test the windows version I'm trying to build?
  2. How do I find windows libraries to use with mingw32 to replace the linux libraries I've been using?

As I said above, for 1, yes, you should be able to run cygwin in wine for testing.

Questions 2 is unrelated and should be moved to a different question. (For what it's worth, I have no idea about the answer to 2).

7
  • If so, is there any alternative that would provide my mingw32 with required libraries and headers? Mar 6, 2012 at 20:56
  • @rafalcieslak I edited more details into my answer Mar 6, 2012 at 22:20
  • "Cygwin is a windows program that allows you to run linux programs in windows" No, it isn't.
    – fkraiem
    Dec 16, 2019 at 19:32
  • @fkraiem.. yes it is. A little like what Wine does on linux for Windows programs.
    – Soren A
    Dec 16, 2019 at 20:18
  • 1
    @SorenA Cygwin is a toolchain, a runtime DLL, and a collection of binary packages which are built using that runtime DLL. The toolchain can run on any operating system as a cross-compiler, including Linux. The runtime DLL and binary packages only run on Windows. (In principle, they could run on Wine; in practice, Wine is full of bugs and limitations due to which Cygwin-under-Wine may or may not work – last time I tried it, I couldn't even get a simple "hello world" program to run, although I know others have got much further than that before.) Nov 10, 2020 at 18:16
4

Have you tried wine?

Anyway, you can use Virtual Box and 90-day Trial Windows 7, but only if your application is not comercial ;)

1
  • +1; Cygwin in Windows in VirtualBox in Ubuntu should work well.
    – sudodus
    Sep 24, 2019 at 13:30
0

The Cygwin cross-compilers are available on Fedora/EPEL using a custom repo (Copr) – https://copr.fedorainfracloud.org/coprs/yselkowitz/cygwin – so in principle it should be possible to run it on Ubuntu too. I don't know if you can install Fedora/EPEL cygwin on Ubuntu and if it would work (you could try converting the RPM to a deb using alien and installing that). Otherwise, you could study the RPM spec files for hints on how to build it from source on Ubuntu.

Note that Cygwin has three parts – a toolchain, the runtime DLL, and ported packages. The runtime DLL and the ported packages won't work under Linux unless you use Wine, and given the limitations of Wine's emulation of the Windows API may not work reliably (depending on exactly what you are using them for). The toolchain can be compiled for Linux as a cross-compiler, and then you can use it to compile Cygwin executables from Linux. This may be useful if you have a Linux-only CI environment, if you have plenty of Linux hosts and a shortage of Windows hosts, etc. (Trying to run the toolchain under Wine may work, although last time I tried to run Cygwin executables under Wine it complained I was using the wrong cygwin1.dll version for my Windows version – maybe using a newer Wine may fix that.)

Anyway, I managed to install Fedora 33 in a chroot on Ubuntu 19.10, and then install and run the Cygwin toolchain in that Fedora chroot – see the following script. Note this doesn't work on Ubuntu 20.04 or later due to the removal of yum in Ubuntu 20.04. Probably there is a less hacky way, like using a Fedora Docker container, or trying to use Alien to install the Fedora RPMs on Ubuntu directly.

Acknowledgement: parts of below script were lifted from this answer

#!/bin/bash
set -xue

# Script to install Fedora 33 in a chroot on Ubuntu
# And then in turn install cygwin64-gcc in that chroot
# Finally we test we can compile a .exe with Cygwin
#
# Run this as root
#
# Works on Ubuntu 19.10

# Configuration
fedoraver=33
chroot=/fedora${fedoraver}
rpminst=/tmp/rpminst
server=https://download.fedoraproject.org
serverpath=pub/fedora/linux/releases/${fedoraver}/Everything/x86_64/os/Packages/f
serverfile=fedora-repos-${fedoraver}-1.noarch.rpm

# Install yum and yumdownloader
apt install -y yum yum-utils

# Create our chroot
mkdir -p $chroot

# Download and install the fedora-repos package
wget $server/$serverpath/$serverfile
rpm -i --nodeps --force --root=$chroot $serverfile

# Ubuntu's RPM is too old for python-pip-wheel package
# So says YUM. But it works anyway if you do it manually
mkdir -p ${rpminst}
yumdownloader --installroot=$chroot --releasever=${fedoraver} --destdir=${rpminst} python-pip-wheel
rpm -i --nodeps --force --root=$chroot ${rpminst}/*.rpm

# Install DNF and core plugins
yum --installroot=$chroot --releasever=${fedoraver} install -y --nogpgcheck dnf dnf-plugins-core

# Ensure we can resolve DNS
rm -f $chroot/etc/resolv.conf
cp /etc/resolv.conf $chroot/etc

# Enable cygwin DNF repository
chroot $chroot dnf --releasever ${fedoraver} copr enable -y yselkowitz/cygwin

# Install cygwin64-gcc
chroot $chroot dnf --releasever ${fedoraver} install --nogpgcheck -y cygwin64-gcc

# Create test.c
echo $'#include<stdio.h>\nint main(int argc,char**argv){printf("Hello World!\\n");return 0;}' > $chroot/test.c

# Compile test.c
chroot $chroot x86_64-pc-cygwin-gcc -o test.exe test.c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .