如何用GCC和Linux子系统编译Windows的可执行文件? [英] How to compile executable for Windows with GCC with Linux Subsystem?
问题描述
安装了gcc sudo apt-get install gcc
。 我为测试目的编写了一些简单的C代码: / p>
#include< stdio.h>
int main(void){
printf(Hello \\\
);
返回0;
}
用 gcc -c main.c编译它
但生成执行(仅Linux) main.o
。如果我运行 ./ main.o
,它会显示 Hello
。
我的问题是,如何编译 main.c
以便Windows可以运行它?基本上,您如何在Linux子系统中使用GCC生成 *。exe
文件?
gcc
创建Linux可执行文件。 要创建Windows可执行文件,您需要安装mingw交叉编译器:
sudo apt-get install mingw-w64
然后,您可以创建32位Windows可执行文件:
i686-w64-mingw32-gcc -o main32.exe main.c
Windows可执行文件:
x86_64-w64-mingw32-gcc -o main64.exe main.c
code>请注意,这些Windows可执行文件在Linux Subsystem内部不起作用,仅限于它。
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 displaysHello
.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 ?解决方案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.
这篇关于如何用GCC和Linux子系统编译Windows的可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!