WSL 运行make提示/lib/modules/xxx/build: No such file or directory. Stop.错误解决办法

错误描述

在WSL下试图编译驱动文件,使用make命令编译c文件出现以下报错:

wang@DESKTOP-55P8P0H:/mnt/d/GithubDesktop/linux-driver-learning-experiment/1.hello_driver$ make
make -C /lib/modules/5.10.16.3-microsoft-standard-WSL2/build M=/mnt/d/GithubDesktop/linux-driver-learning-experiment/1.hello_driver modules
make[1]: *** /lib/modules/5.10.16.3-microsoft-standard-WSL2/build: No such file or directory.  Stop.
make: *** [Makefile:5: all] Error 2

错误原因

WSL2的内核是修改过的,无法使用 ubuntu上游的内核头文件和modules文件,因此,我们需要手动编译并安装一个版本。

解决步骤

参考:WSL2编译和安装内核以支持驱动编译

1. 下载对应版本的内核代码

查看内核版本,我的是5.10.16.3版本

wang@DESKTOP-55P8P0H:~/kernel$ uname -r
5.10.16.3-microsoft-standard-WSL2

WSL git仓库,找到对应的release

git clone --depth=1 https://github.com/microsoft/WSL2-Linux-Kernel

对应版本release
复制下载链接,下载tar包,解压

wget https://github.om/microsoft/WSL2-Linux-Kernel/archive/refs/tags/linux-msft-wsl-5.10.16.3.tar.gz
tar -cvzf linux-msft-wsl-5.10.16.3.tar.gz

2. 编译和安装

cd WSL2-Linux-Kernel-linux-msft-wsl-5.10.16.3
LOCALVERSION= make KCONFIG_CONFIG=Microsoft/config-wsl -j8
sudo LOCALVERSION= make KCONFIG_CONFIG=Microsoft/config-wsl modules_install -j8

运行第二步出现问题:

  ERROR: Kernel configuration is invalid.
         include/generated/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.

尝试运行make oldconfig && make prepare
再次报错:

*
* Unable to find the ncurses package.
* Install ncurses (ncurses-devel or libncurses-dev
* depending on your distribution).
*
* You may also need to install pkg-config to find the
* ncurses installed in a non-default location.
*
make[1]: *** [scripts/kconfig/Makefile:211: scripts/kconfig/mconf-cfg] Error 1
make: *** [Makefile:619: menuconfig] Error 2

需要安装ncurses、flex和bison

sudo apt install ncurses-dev
sudo apt install flex
sudo apt install bison

然后运行make menuconfig选择<Exit>直接退出,自动生成.config文件

再次运行wangmake oldconfig && make prepare
报错:

scripts/extract-cert.c:21:10: fatal error: openssl/bio.h: No such file or directory
   21 | #include <openssl/bio.h>
      |          ^~~~~~~~~~~~~~~
compilation terminated.
make[1]: *** [scripts/Makefile.host:95: scripts/extract-cert] Error 1
make: *** [Makefile:1234: scripts] Error 2

安装libssl-dev

sudo apt install libssl-dev

再次尝试第二步,成功编译内核。

3. 安装headers

sudo make headers_install ARCH=x86_64 INSTALL_HDR_PATH=/usr

结果

回到工作目录,再次尝试编译,成功。

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值