How to compile executable for Windows with GCC with Linux Subsystem?

情到浓时终转凉″ 提交于 2019-12-17 17:25:51

问题


Windows 10 Anniversary Update includes the Linux Subsystem for Ubuntu. I installed gcc with sudo apt-get install gcc.

I wrote some simple C code for testing purposes:

#include <stdio.h>
int main(void){
    printf("Hello\n");
    return 0;
}

And compiled it with gcc -c main.c but the execute (Linux only) main.o is generated. If I run it ./main.o, it displays Hello.

My question is, how can I compile main.c so that Windows can run it? Basically, how do you generate a *.exe file with GCC in Linux Subsystem ?


回答1:


Linux Subsystem works as a Linux-computer. You can only run Linux executables inside it and default gcc creates Linux executables.

To create Windows executables, you need to install mingw cross-compiler:

sudo apt-get install mingw-w64

Then you can create 32-bit Windows executable with:

i686-w64-mingw32-gcc -o main32.exe main.c

And 64-bit Windows executable with:

x86_64-w64-mingw32-gcc -o main64.exe main.c

Note that these Windows executables will not work inside Linux Subsystem, only outside of it.




回答2:


If you compile using gcc on linux it will produce an ELF file not a PE (what windows understand) file

To compile a program for windows inside linux you can use mingw.



来源:https://stackoverflow.com/questions/38786014/how-to-compile-executable-for-windows-with-gcc-with-linux-subsystem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!