做6.828时,配置qemu,碰到了一个makefile错误,去年配没遇到过。
qemu安装过程见课程官网tools安装说明。
当我从官网git到qemu源码包之后,./config完,下一步就是make了,但是这时候报错了。
make[1]: *** No rule to make target '/home/yiichan/qemu/exec.c', needed by 'exec.o'. Stop.
Makefile:173: recipe for target 'subdir-i386-softmmu' failed
make: *** [subdir-i386-softmmu] Error 2
到网上找原因,具体参考了这个博客。原因是工程的目录改变了,但是Makefile引用的文件里的.h头文件目录没变。要找到出错的文件,打开,看看是不是头文件的问题。
比如我这里的例子。我找到exec.c,打开,看到里面的头文件。
#include "hw/qdev.h"
#include "qemu/osdep.h"
#include "sysemu/kvm.h"
#include "sysemu/sysemu.h"
#include "hw/xen/xen.h"
#include "qemu/timer.h"
#include "qemu/config-file.h"
#include "qemu/error-report.h"
#include "exec/memory.h"
#include "sysemu/dma.h"
#include "exec/address-spaces.h"
#if defined(CONFIG_USER_ONLY)
#include <qemu.h>
#else /* !CONFIG_USER_ONLY */
#include "sysemu/xen-mapcache.h"
#include "trace.h"
#endif
#include "exec/cpu-all.h"
#include "qemu/rcu_queue.h"
#include "exec/cputlb.h"
#include "translate-all.h"
#include "exec/memory-internal.h"
#include "exec/ram_addr.h"
#include "qemu/range.h"
大概是这样的。但是我发现,在exec.c所在的目录下,并没有这些头文件的上一层文件夹。查找后,我发现,上面所有在父文件夹里的头文件,它们的父文件夹都在exec.c同目录下的include文件夹内,也就是在exec.c编译预处理时,在exec.c目录下按照这个路径并不能找到这些头文件。所以我把每个在父文件夹里的头文件,父文件夹前面都加上了二级父文件夹include目录,像下面这样。
#include "include/exec/cpu-all.h"
#include "include/qemu/rcu_queue.h"C
#include "include/exec/cputlb.h"
#include "include/translate-all.h"
重新编译,果然没出错误,之后make install,qemu正常安装。
补充: C语言include的处理过程
C语言include有两种格式
#include <> //直接搜索系统路径
#include "" //先搜索当前路径,没有要求的头文件再搜索系统路径
网上的博客只说搜索当前路径,没说是不是会迭代搜索完当前路径下的所有目录,经我自己写了个头文件测试,以及这里这个错误,我发现,加双引号只会搜索当前路径,并不会迭代加深,如果当前文件所在的路径没有搜索到,就不会在当前目录下停留了。我的qemu安装make不成功,就是因为这个。