WSL下搭建C开发环境,解决stdio.h缺失,build-essential安装失败,包依赖问题

在安装WSL下的C开发环境时,遇到了几个坑,在此记录下,借需要的朋友参考。

事件经过:

  1. 首先安装WSL,直接在Microsoft商店安装Ubuntu 20.04
  2. 之后进入Ubuntu,编写C代码,用gcc编译的时候开始报错,提示stdio.h头文件缺失
  3. 发现少了build-essential,尝试apt-get安装,继续报错,告警信息为包版本缺失(或不支持)
  4. 之后尝试安装aptitude,依旧是依赖包报错
  5. 换思路,更换源,发现换错源,更正为正确源
  6. apt-get update后,apt-get build-essential成功,最终编译成功。

安装WSL

这部分教程较多,没遇到什么坑,不细说了。

编写.C文件并编译

编写hello.c:

#include <stdio.h>

int main(int argc,char *argv[])
{
    printf("Hello World!");
}

使用gcc hello.c -o hello进行编译

安装gcc

报gcc: command not found,说明gcc未安装,输入apt-get install gcc,等待安装完成,继续编译,告警stdio.h:没有该文件或目录,代表build-essential未安装。

安装build-essential

执行命令sudo apt-get install build-essential,记得sudo防止权限不够,如果正常安装了,那编译应该能通过了,不过我这里报了新的告警,类似下面这种:

maistora@maistora:~$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 build-essential : Depends: libc6-dev but it is not going to be installed or
                            libc-dev
                   Depends: gcc (>= 4:4.4.3) but it is not going to be installed
                   Depends: g++ (>= 4:4.4.3) but it is not going to be installed
E: Broken packages

网上针对这种告警有不少讨论,有说版本stable和unstable问题的,有说包依赖关系导致的,需要使用aptitude安装的,我感觉都挺靠谱,就是行不通,所以介绍下我这里可行的方法。

切换源

切换源就是我找到的解决方法,但不能盲目地切换,源的选择和Ubuntu版本密切相关,下面细说:
切换源的方法来自坚强的山猫博客
Ubuntu 20.04换阿里源
首先在Ubuntu的bash下执行lsb_release -a,查看自己的Ubuntu版本
我的版本
之后到阿里源中查看是否有需要的源,并按照上述博客中的方法替换掉/etc/apt/source.list中的源,Ubuntu20.04替换源后,source.list如下:
Ubuntu20.04版本源
保存后退出,运行sudo apt-get update,应该能成功运行,随后执行sudo apt-get install build-essential,即可安装成功,最后gcc hello.c -o hello,编译成功:
编译运行结果
至此C环境安装暂且告一段落。

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值