Cross Compiling for Windows using WSL/WSL2

Sachin Myneni 1 Reputation point
2021-03-12T22:56:48.53+00:00

We wanted to replace the slow cygwin toolset with WSL for building our Windows executable.
Executables are built on Windows 10 VMs. Access is either through RDP for manual tests or using SSH as part of our CI system.

Here is my process:

  1. Call setup script from DOS window: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
  2. Compile using cl.exe: C:\Users\osqa>"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64 C:\Users\osqa>cl.exe -version
    Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64
    Copyright (C) Microsoft Corporation. All rights reserved. cl : Command line warning D9002 : ignoring unknown option '-version'
    cl : Command line error D8003 : missing source filename C:\Users\osqa>
    C:\Users\osqa>cd workarea\osqa_ca-w10-bld-05_os_211\os C:\Users\osqa\workarea\osqa_ca-w10-bld-05_os_211\os>cl.exe hello.c
    Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64
    Copyright (C) Microsoft Corporation. All rights reserved. hello.c
    Microsoft (R) Incremental Linker Version 14.00.24215.1
    Copyright (C) Microsoft Corporation. All rights reserved. /out:hello.exe
    hello.obj C:\Users\osqa\workarea\osqa_ca-w10-bld-05_os_211\os>hello.exe
    Hello World

This works fine in the DOS window when log into the system using RDP or SSH using OpenSSH.
This also works fine in WSL if I RDP into the VM. But it does not work in WSL if I SSH into the VM: Note that I ssh into the windows VM, call the vcvarsall.bat script and then start WSL and then invoke the compiler. I do not ssh directly into the WSL.

prog\osqa@CA-W10-BLD-05 C:\Users\osqa\workarea\osqa_ca-w10-bld-05_os_211\os>"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64  
  
prog\osqa@CA-W10-BLD-05 C:\Users\osqa\workarea\osqa_ca-w10-bld-05_os_211\os>cl.exe hello.c  
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64  
Copyright (C) Microsoft Corporation.  All rights reserved.  
  
hello.c  
Microsoft (R) Incremental Linker Version 14.00.24215.1  
Copyright (C) Microsoft Corporation.  All rights reserved.  
  
/out:hello.exe  
hello.obj  
  
prog\osqa@CA-W10-BLD-05 C:\Users\osqa\workarea\osqa_ca-w10-bld-05_os_211\os>hello.exe  
Hello World  
  
prog\osqa@CA-W10-BLD-05 C:\Users\osqa\workarea\osqa_ca-w10-bld-05_os_211\os>wsl  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ rm hello.exe  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ cl.exe --version  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ cl.exe hello.c  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ echo $?  
66  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ gcc hello.c  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ echo $?  
0  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ ./hello.exe  
-bash: ./hello.exe: No such file or directory  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$ ./a.out  
Hello World  
osqa@ca-w10-bld-05:/c/Users/osqa/workarea/osqa_ca-w10-bld-05_os_211/os$  

As one can see cl.exe just returns back with an error '66'.
For shells error 66 typically means: cannot open input.

77373-image.png

Environment:

Host machine: Microsoft Windows [Version 10.0.19041.804]
OpenSSH: I've setup OpenSSH using instructions from here: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse
WSL: Ubuntu 20.04.2 LTS in WSL2. I've also tried WSL1, Ubuntu 18.04 with the same results.

Tests done so far:
I ran the command 'whoami' in DOS window, when logged in through each of RDP and SSH. In both case the results are identical so the user seems to be the same in case of both RDP and SSH.

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,182 questions
Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,254 questions
Windows 10 Compatibility
Windows 10 Compatibility
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Compatibility: The extent to which hardware or software adheres to an accepted standard.
456 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Teemo Tang 11,346 Reputation points
    2021-03-15T07:49:14.873+00:00

    Hi,
    In fact, for Cross-compile issue, you’d better ask for help from github forum.
    https://github.com/topics/cross-compiler
    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn.
    Thanks for your understanding and cooperating.
    Look at this similar case:
    [Help requested] Cross-compile from WSL to Windows
    https://github.com/veandco/go-sdl2/issues/336

    -------------------------------------------------------------------------------------

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Sachin Myneni 1 Reputation point
    2021-03-15T15:44:32.467+00:00

    Hello @Teemo Tang

    The reason why I posted here and not github is that the issue, while showing up in cross-compilation, seems to have to do with user identity and permissions.

    Note that I can get the cross-compilation to work fine when I RDP into the system. But not when I ssh into the system.

    If you still think this should go into github cross-compiler forum, I will post it there.

    Thank you,
    -Sachin

    0 comments No comments

  3. Sachin Myneni 1 Reputation point
    2021-03-15T16:48:12.297+00:00

    Another update: As I mentioned in my OP this is primarily for our CI system. We use a windows domain account that is not and administrator on all the build systems.
    I discovered that if I made this account an 'administrator' on the build system (added to administrator group), this whole thing works!
    I can ssh into the system (as the build account), start wsl and cross-compile

    So this is definitely a permissions issue.

    I would rather not leave the build-account with Administrator privileges on all build systems. I guess I can narrow my question to:
    What is the specific privilege that is required for ssh + wsl + windows_interop to work?

    Thank you,
    -Sachin