在WSL2里运行32位程序

以Debian root用户为例。请先看如下程序:
#include <stdlib.h>
#include <stdio.h>
int main() {
  printf("%d ", sizeof(int));
  printf("%d ", sizeof(long));
  printf("%d ", sizeof(void*));
  printf("%d\n", atoi("3"));
  return 0;
}

用gcc编译,输出结果是4 8 8 3,我还以为64位系统下sizeof(int)是8呢。用gcc -m32编译,首先编译时报找不到某个头文件,解决方法是:apt-get install gcc-multilib

然后运行时出错:

bash: ./a.out: cannot execute binary file: Exec format error

解决方法是:

  • WSL不支持,需要升级到WSL2(方法在后面)
  • Linux里dpkg --add-architecture i386; apt-get update

WSL升级已有Linux到WSL2的方法:

管理员身份运行cmd

wsl -l -v
  NAME      STATE           VERSION
* debian    Running         1

如果在 wsl --install 命令可用之前手动安装了 WSL,则可能还需要启用 WSL 2 所使用的虚拟机可选组件并安装内核包(如果尚未这样做)。

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

看 https://learn.microsoft.com/zh-cn/windows/wsl/install
下载 https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi 并安装、重启

wsl --set-version debian 2 转换现有的debian需要几分钟时间
wsl --set-default-version 2 为以后安装的设置默认值

然后D:\debian下从WSL的一大堆文件变成了只有ext4.vhdx和fsserver两个文件。我不后悔:-),毕竟不用装VirtualBox了嘛。但是用set-version换回1是我的权利:-)。而且在Linux下可通过/mnt访问Windows的文件(explorer.exe在/mnt/c/windows下):

alias zy='explorer.exe .'
export PATH=.:/mnt/c/windows:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

设置nano的tab size ~/.vimrc [为啥我记得注释用'打头,现在是"了]: 

syntax on
set noek
set ts=4
set sw=4
"/usr/share/vim/vim82/colors
color industry

Windows 11 安装 WSL2 并ssh远程连接 | WSL2 与 Win10 固定host互相访问 | 通过局域网访问 Windows 10 WSL 2 的网络服务

posted @ 2022-12-29 16:31  Fun_with_Words  阅读(211)  评论(0编辑  收藏  举报









 张牌。