如何用GCC和Linux子系统编译Windows的可执行文件? [英] How to compile executable for Windows with GCC with Linux Subsystem?

查看:1037
本文介绍了如何用GCC和Linux子系统编译Windows的可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 10周年更新包括Ubuntu的Linux子系统。我使用安装了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 文件?

Linux子系统可用作Linux计算机。您只能在其中运行Linux可执行文件,并且默认 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 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 ?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆