1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Windows How to run Windows Linux subsystem GCC executable?

Discussion in 'Software' started by wyx087, 8 Jan 2021.

  1. wyx087

    wyx087 Homeworld 3 is happening!!

    Joined:
    15 Aug 2007
    Posts:
    11,994
    Likes Received:
    713
    With my very mostly embedded C knowledge, I've written a little program to automate things when I wake the PC remotely, it'll do stuff based on MQTT messages from my home automation. Had to have /mnt/c/<full-path> in front of any program I want run, no big deal.

    This little program is compiled using GCC under the Linux shell now available on Windows. It runs nicely when invoked from the Linux shell using the usual Linux method: "./name.exe"

    But when I double click on the EXE in Explorer, expecting it to work like any Windows program....... nope!

    How do I run Linux subsystem GCC generated executable in Windows as a normal Windows program? I want to run it on start-up or from start menu, and want to see console printout.
     
  2. Gareth Halfacree

    Gareth Halfacree WIIGII! Lover of bit-tech Administrator Super Moderator Moderator

    Joined:
    4 Dec 2007
    Posts:
    17,130
    Likes Received:
    6,717
    Unless something's changed recently, you don't. You might have given the file a .EXE extension, but what you've compiled is an ELF - a Linux binary, not a Windows binary.

    You need to compile it for Windows - assuming the underlying code is portable, if not you need to rewrite it for Windows or run it in WSL.

    You can compile for Windows in WSL like so:

    Code:
    sudo apt install mingw-w64
    x86_64-w64-mingw32-gcc -o name.exe name.c
    
    It won't run in WSL any more, of course!
     
    wyx087 likes this.
  3. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,953
    Likes Received:
    270
    wyx087 and Gareth Halfacree like this.
  4. Gareth Halfacree

    Gareth Halfacree WIIGII! Lover of bit-tech Administrator Super Moderator Moderator

    Joined:
    4 Dec 2007
    Posts:
    17,130
    Likes Received:
    6,717
    Nice - either that wasn't a thing last time I played, or I just didn't know about it.

    Compiling the program as an actual Windows executable would mean a lot less overhead, mind - otherwise you're effectively running two operating systems for the sake of one small program.
     
  5. wyx087

    wyx087 Homeworld 3 is happening!!

    Joined:
    15 Aug 2007
    Posts:
    11,994
    Likes Received:
    713
    Perfect, that bath file method is exactly what I'm looking for.
    Code:
    wsl ./pc.exe
    Thank you!

    Last time I did it, before Windows Linux shell was a thing, I used Cygwin (I think! might have been MinGW) its GCC was able to generate an EXE that I can click on. One mustn't assume.


    Edit: missed the last part. Resource isn’t an issue on my desktop, there’s soooo much to go around, compared to embedded systems.
     
    Last edited: 8 Jan 2021

Share This Page