windows 下 编译linux 程序(wsl)
WSL环境编译
在Windows Subsystem for Linux中直接使用Linux工具链
一、在 Windows 上编译 Linux 程序,请使用 WSL 并在其中安装 Linux 原生 GCC。进入dos窗口
1.查看发行版本
C:\Users\king>wsl --list --online 以下是可安装的有效分发的列表。 使用“wsl.exe --install <Distro>”安装。 NAME FRIENDLY NAME AlmaLinux-8 AlmaLinux OS 8 AlmaLinux-9 AlmaLinux OS 9 AlmaLinux-Kitten-10 AlmaLinux OS Kitten 10 AlmaLinux-10 AlmaLinux OS 10 Debian Debian GNU/Linux FedoraLinux-43 Fedora Linux 43 FedoraLinux-42 Fedora Linux 42 SUSE-Linux-Enterprise-15-SP7 SUSE Linux Enterprise 15 SP7 SUSE-Linux-Enterprise-16.0 SUSE Linux Enterprise 16.0 Ubuntu Ubuntu Ubuntu-24.04 Ubuntu 24.04 LTS archlinux Arch Linux kali-linux Kali Linux Rolling openSUSE-Tumbleweed openSUSE Tumbleweed openSUSE-Leap-16.0 openSUSE Leap 16.0 Ubuntu-20.04 Ubuntu 20.04 LTS Ubuntu-22.04 Ubuntu 22.04 LTS OracleLinux_7_9 Oracle Linux 7.9 OracleLinux_8_10 Oracle Linux 8.10 OracleLinux_9_5 Oracle Linux 9.5 openSUSE-Leap-15.6 openSUSE Leap 15.6 SUSE-Linux-Enterprise-15-SP6 SUSE Linux Enterprise 15 SP6
2. 安装archlinux
C:\Users\king>wsl --install archlinux
正在下载: Arch Linux
正在安装: Arch Linux
已成功安装分发。可以通过 “wsl.exe -d archlinux” 启动它
正在启动 archlinux...
Welcome to the Arch Linux WSL image!
This image is maintained at <https://gitlab.archlinux.org/archlinux/archlinux-wsl>.
Please, report bugs at <https://gitlab.archlinux.org/archlinux/archlinux-wsl/-/issues>.
Note that WSL 1 is not supported.
For more information about this WSL image and its usage (including "tips and tricks" and troubleshooting steps), see the related Arch Wiki page at <https://wiki.archlinux.org/title/Install_Arch_Linux_on_WSL>.
While images are built regularly, it is strongly recommended running "pacman -Syu" right after the first launch due to the rolling release nature of Arch Linux.
Generating pacman keys...
==> Generating pacman master key. This may take some time.
==> Updating trust database...
Done
#会自动启动
3.退出,再次进入
C:\Users\king>wsl -d archlinux
[root@LAPTOP-MC2ANUM6 king]#
4.默认wsl
C:\Users\king>wsl --status 默认分发: docker-desktop 默认版本: 2
如果你只输入wsl,进入的是docker-desktop的环境(本机安装有windows docker环境)
PS C:\msys64\usr\bin> wsl
docker-desktop:/tmp/docker-desktop-root/run/desktop/mnt/host/c/msys64/usr/bin#
5.再次安装 Ubuntu
C:\Users\king>wsl --install Ubuntu 正在下载: Ubuntu 正在安装: Ubuntu 已成功安装分发。可以通过 “wsl.exe -d Ubuntu” 启动它 正在启动 Ubuntu... Provisioning the new WSL instance Ubuntu This might take a while... Create a default Unix user account: king New password: Retype new password: passwd: password updated successfully To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details.
更新下
king@LAPTOP-MC2ANUM6:/mnt/c/Users/king$ sudo apt update [sudo] password for king: Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease Get:2 http://archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB] Get:3 http://archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB] Get:4 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages [15.0 MB] Get:5 http://archive.ubuntu.com/ubuntu noble/universe Translation-en [5982 kB] Get:6 http://archive.ubuntu.com/ubuntu noble/universe amd64 Components [3871 kB] Get:7 http://archive.ubuntu.com/ubuntu noble/universe amd64 c-n-f Metadata [301 kB] Get:8 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 Packages [269 kB] Get:9 http://archive.ubuntu.com/ubuntu noble/multiverse Translation-en [118 kB] Get:10 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 Components [35.0 kB] Get:11 http://archive.ubuntu.com/ubuntu noble/multiverse amd64 c-n-f Metadata [8328 B] Get:12 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [1627 kB] Get:13 http://archive.ubuntu.com/ubuntu noble-updates/main Translation-en [305 kB] Get:14 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 Components [175 kB] Get:15 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 c-n-f Metadata [15.7 kB] Get:16 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [1501 kB] Get:17 http://archive.ubuntu.com/ubuntu noble-updates/universe Translation-en [304 kB] Get:18 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 Components [378 kB] Get:19 http://archive.ubuntu.com/ubuntu noble-updates/universe amd64 c-n-f Metadata [31.4 kB] Get:20 http://archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [2309 kB] Get:21 http://archive.ubuntu.com/ubuntu noble-updates/restricted Translation-en [526 kB] Get:22 http://archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Components [212 B] Get:23 http://archive.ubuntu.com/ubuntu noble-updates/restricted amd64 c-n-f Metadata [516 B] Get:24 http://archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Packages [30.3 kB] Get:25 http://archive.ubuntu.com/ubuntu noble-updates/multiverse Translation-en [5808 B] Get:26 http://archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Components [940 B] Get:27 http://archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 c-n-f Metadata [484 B] Get:28 http://archive.ubuntu.com/ubuntu noble-backports/main amd64 Packages [40.4 kB] Get:29 http://archive.ubuntu.com/ubuntu noble-backports/main Translation-en [9208 B] Get:30 http://archive.ubuntu.com/ubuntu noble-backports/main amd64 Components [7140 B] Get:31 http://archive.ubuntu.com/ubuntu noble-backports/main amd64 c-n-f Metadata [368 B] Get:32 http://archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [29.2 kB] Get:33 http://archive.ubuntu.com/ubuntu noble-backports/universe Translation-en [17.6 kB] Get:34 http://archive.ubuntu.com/ubuntu noble-backports/universe amd64 Components [11.0 kB] Get:35 http://archive.ubuntu.com/ubuntu noble-backports/universe amd64 c-n-f Metadata [1444 B] Get:36 http://archive.ubuntu.com/ubuntu noble-backports/restricted amd64 Components [212 B] Get:37 http://archive.ubuntu.com/ubuntu noble-backports/restricted amd64 c-n-f Metadata [116 B] Get:38 http://archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B] Get:39 http://archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 c-n-f Metadata [116 B] Get:40 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB] Get:41 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [1349 kB] Get:42 http://security.ubuntu.com/ubuntu noble-security/main Translation-en [221 kB] Get:43 http://security.ubuntu.com/ubuntu noble-security/main amd64 Components [21.5 kB] Get:44 http://security.ubuntu.com/ubuntu noble-security/main amd64 c-n-f Metadata [9448 B] Get:45 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [915 kB] Get:46 http://security.ubuntu.com/ubuntu noble-security/universe Translation-en [206 kB] Get:47 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Components [71.4 kB] Get:48 http://security.ubuntu.com/ubuntu noble-security/universe amd64 c-n-f Metadata [19.4 kB] Get:49 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [2205 kB] Get:50 http://security.ubuntu.com/ubuntu noble-security/restricted Translation-en [503 kB] Get:51 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Components [212 B] Get:52 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 c-n-f Metadata [500 B] Get:53 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages [27.4 kB] Get:54 http://security.ubuntu.com/ubuntu noble-security/multiverse Translation-en [5956 B] Get:55 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Components [212 B] Get:56 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 c-n-f Metadata [384 B] Fetched 38.9 MB in 14s (2768 kB/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done 87 packages can be upgraded. Run 'apt list --upgradable' to see them.
安装 Linux 的 GCC 编译器(如 build-essential)
king@LAPTOP-MC2ANUM6:sudo apt install build-essential ... 安装略... king@LAPTOP-MC2ANUM6:/mnt/c/Users/king$ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 13.3.0-6ubuntu2~24.04' --with-bugurl=file:///usr/share/doc/gcc-13/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-13 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04)
默认界面,默认windows下的c:\Users\king 目录,里面的所有内容都可以看到
king@LAPTOP-MC2ANUM6:/mnt/c/Users/king$
/mnt/c/Users/king 这个目录就是windows和linux的对接口
编译: 在 WSL 终端中,使用标准的 gcc (或 g++) 命令编译你的 C/C++ 代码,它会生成原生的 Linux 可执行文件。
编辑test.c
$vim test.c #include <stdio.h> unsigned int a; int a1=1; char c = 66; char c1 = 'B'; int main() { int x = 100; printf("dec = %d\n", x); // 100 printf("octal = %o\n", x); // 144 printf("hex = %x\n", x); // 64 printf("octal = %#o\n", x); // 0144 printf("hex = %#x\n", x); // 0x64 printf("hex = %#X\n", x); // 0X64 char a = 'B'; // 等同于 char a = 66; char b = 'C'; // 等同于 char b = 67; printf("%d\n", a + b); // 输出 133 printf("aozhejin\n"); return 0; } #执行编译 $gcc test.c
#用nm查看 $gcc your_program.c -o your_program king@LAPTOP-MC2ANUM6:/mnt/c/Users/king$ nm a.out 0000000000003dc0 d _DYNAMIC 0000000000003fb0 d _GLOBAL_OFFSET_TABLE_ 0000000000002000 R _IO_stdin_used w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable 0000000000002130 r __FRAME_END__ 0000000000002054 r __GNU_EH_FRAME_HDR 0000000000004018 D __TMC_END__ 000000000000038c r __abi_tag 0000000000004018 B __bss_start w __cxa_finalize@GLIBC_2.2.5 0000000000004000 D __data_start 0000000000001120 t __do_global_dtors_aux 0000000000003db8 d __do_global_dtors_aux_fini_array_entry 0000000000004008 D __dso_handle 0000000000003db0 d __frame_dummy_init_array_entry w __gmon_start__ U __libc_start_main@GLIBC_2.34 0000000000004016 D _edata 0000000000004020 B _end 0000000000001250 T _fini 0000000000001000 T _init 0000000000001080 T _start 000000000000401c B a 0000000000004010 D a1 0000000000004014 D c 0000000000004015 D c1 0000000000004018 b completed.0 0000000000004000 W data_start 00000000000010b0 t deregister_tm_clones 0000000000001160 t frame_dummy 0000000000001169 T main U printf@GLIBC_2.2.5 U puts@GLIBC_2.2.5 00000000000010e0 t register_tm_clones
安装包管理工具
在CentOS中一般是yum,ubuntu 是Pacman
king@LAPTOP-MC2ANUM6:/mnt/c/Users/king/testwsl$ sudo apt-get install pacman
一些混合命令
C:\Users\king>wsl ls -la C:\Users\king>wsl ifconfig C:\Users\king>wsl top C:\Users\king>wsl ls C:\Users\king>wsl echo 1 > 1.txt 参考: https://learn.microsoft.com/zh-cn/windows/wsl/filesystems
备注: 在 WSL 里,gcc指向的是你安装的 Linux 版本里的 GCC,它编译出的是可以在 WSL 环境中运行的 Linux 程序。
参考:
https://learn.microsoft.com/zh-cn/windows/wsl/setup/environment
https://learn.microsoft.com/zh-cn/windows/wsl/basic-commands

浙公网安备 33010602011771号