2

我遇到了一个错误,说我的 Makefile 的第 235 行有错误:

make[4]: Leaving directory `/opt/home/root/native-upstream/native_client/tools/SRC/glibc/wctype'
make  subdir=manual -C manual ..=../ subdir_lib
make[4]: Entering directory `/opt/home/root/native-upstream/native_client/tools/SRC/glibc/manual'
Makefile:235: *** mixed implicit and normal rules.  Stop.
make[4]: Leaving directory `/opt/home/root/native-upstream/native_client/tools/SRC/glibc/manual'
make[3]: *** [manual/subdir_lib] Error 2
make[3]: Leaving directory `/opt/home/root/native-upstream/native_client/tools/SRC/glibc'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/opt/home/root/native-upstream/native_client/tools/BUILD/build-glibc32'
make[1]: *** [BUILD/stamp-glibc32] Error 2
make[1]: Leaving directory `/opt/home/root/native-upstream/native_client/tools'
make: *** [build-with-glibc] Error 2

但是我不知道哪个 Makefile 有这个错误,我的项目中有很多 Makefile:

#  find . -name Makefile
./tools/Makefile
./tools/BUILD/.gcc-extra-build-cloog-ppl/doc/Makefile
./tools/BUILD/.gcc-extra-build-cloog-ppl/Makefile
./tools/BUILD/.gcc-extra-build-cloog-ppl/test/Makefile
./tools/BUILD/.gcc-extra-build-gmp/printf/Makefile
./tools/BUILD/.gcc-extra-build-gmp/doc/Makefile
./tools/BUILD/.gcc-extra-build-gmp/tests/Makefile
./tools/BUILD/.gcc-extra-build-gmp/tests/devel/Makefile
...

所以我想打印每个 Makefile 的第 235 行,以找出谁是罪魁祸首,例如:

./tools/Makefile: 235: $(objpfx)c++-types-check.out: $(check-data) scripts/check-c++-types.sh
./tools/BUILD/.gcc-extra-build-cloog-ppl/doc/Makefile: 235: ifneq (,$(check-data))
./tools/BUILD/.gcc-extra-build-cloog-ppl/Makefile: 235: $(objpfx)c++-types-check.out:
./tools/BUILD/.gcc-extra-build-cloog-ppl/test/Makefile: 235: endif
./tools/BUILD/.gcc-extra-build-gmp/printf/Makefile: 235:
./tools/BUILD/.gcc-extra-build-gmp/doc/Makefile: 235:
./tools/BUILD/.gcc-extra-build-gmp/tests/Makefile: 235: #   Master Makefile for the GNU C library
./tools/BUILD/.gcc-extra-build-gmp/tests/devel/Makefile: 235:

有没有办法做到这一点?

4

3 回答 3

3

看起来问题出在 中/opt/home/root/native-upstream/native_client/tools/SRC/glibc/manual/Makefile,因为错误之前的那一行表示它正在进入该目录:

make[4]: Entering directory `/opt/home/root/native-upstream/native_client/tools/SRC/glibc/manual'
Makefile:235: *** mixed implicit and normal rules.  Stop.

也就是说,有几种方法可以找到每个 Makefile 的第 235 行。人们已经指出了 awk 和 sed 解决方案。如果您还想要文件名,一个快速简便的解决方案是使用grep

find . -name Makefile -print0 | xargs -0 grep -nH $ | grep :235:

find 命令查找所有名为 Makefile 的文件,并以空分隔符将它们打印出来(这样,如果文件名中有空格,您就不会遇到问题)。xargs将这些文件名传递给grep. grep -nH打印每个匹配行的文件名和行号;$as 模式确保每一行都匹配(它匹配每一行的结尾)。然后你在其中寻找你正在寻找的行号。

它并不完美;您可能会发现其中几行恰好包含“:235:”,但对于快速一次性应该没问题(如果您真的在意,可以使用`grep'^ [^:] *:235: ' 以确保您只匹配行号)。

于 2012-12-20T04:25:52.603 回答
1

我想最直接的方法是

sed -n '235p' *

这将打印当前目录中所有文件的第 235 行。如果您想通过目录递归,或排除某些文件,您将不得不使用更明确的 glob 或findwith -exec.

所以最后,你可能想要的是:

find . -type f -name Makefile -print -exec sed -n '235p' {} \;
于 2012-12-20T03:54:00.457 回答
1

这是一个解决方案findawk

find -type f -name "Makefile" -exec awk 'FNR==235 {print FILENAME; print}' {} +

这打印:

  • 生成文件的名称
  • 该 Makefile 的第 235 行

解释:

  • find -type f -name "Makefile"- 递归查找当前工作目录中所有命名的文件Makefile
  • -exec awk 'FNR==235 {print FILENAME; print}' {} +- 对于找到的每个此类文件,使用awk打印其名称,然后在第 235 行打印其内容。
于 2012-12-20T04:21:57.417 回答