本人在 ubuntu18.04 虚机编译 riscv-openocd 时遇到了一些 pkg-config 相关的问题,现总结分享出来,给有需要的人提供一个可参考的解决方案。

riscv-openocd 是从 GitHub 上直接 clone 下来的源码,在执行 ./configure 时报错,导致无法生成 Makefile 文件:

./configure: line 4523: syntax error near unexpected token `0.23'

./configure: line 4523: `PKG_PROG_PKG_CONFIG(0.23)'

咋一看,以为是哪里有语法错误,直接修改定位到 configure 文件的报错行屏蔽掉后,继续执行 ./configure 仍然报错:

./configure: line 14622: syntax error near unexpected token `LIBUSB1,'

./configure: line 14622: `PKG_CHECK_MODULES(LIBUSB1, libusb-1.0,'

仔细分析了下,其实这个跟 configure 文件本身的语法没关系,而是编译环境找不到 pkg-config 的宏 PKG_PROG_PKG_CONFIG、PKG_CHECK_MODULES。 

网上找了下,基本确认是因为在引导时 aclocal 无法找到 pkg.m4,这是因为未安装 pkg-config 或已将其安装在 aclocal 不知道的地方。我的编译环境是已经安装了 pkg-config 的,很显然极有可能是后者,aclocal 识别不到 pkg-config 的宏。网上的原文如下:

When you are bootstrapping (ie, running autoreconf), aclocal is unable to find pkg.m4. This is because pkg-config was either not installed or has been installed somewhere that aclocal does not know about. (ie, it was installed with a different prefix than automake.) There is actually a large school of thought that says that the best way to use pkg-config with the autotools is to not use it. If you look through the autoconf mailing list archive, you will see this debated ad-nauseum. I'm not saying that you should avoid using pkg-config (although I do believe it is best to do so), but please be aware that many people to recommend avoiding it completely.

但网上并没有给出有效的解决方案,我这边自己摸索了一下,最终成功的解决了改报错。

首先,更新一下编译环境的软件版本。这个步骤不一定需要,但最好执行一下,确保相关的软件版本为最新,防止因为软件版本差异而引入的其他问题。

# sudo apt-get update

# sudo apt-get upgrade

然后,在引导之前,运行以下几个自动化脚本工具。

# sudo aclocal

# sudo autoheader

# sudo autoconf

# sudo automake

之后,重新执行 riscv-openocd 引导脚本。

# ./bootstrap

+ aclocal --warnings=all

+ libtoolize --automake --copy

+ autoconf --warnings=all

+ autoheader --warnings=all

+ automake --warnings=all --gnu --add-missing --copy

Setting up submodules

Generating build system...

Bootstrap complete. Quick build instructions:

./configure ....

再执行 ./configure 就可以成功生成 makefile 文件了,接下来再执行 make、make install 就是顺理成章的事了。

诸如此类 pkg-config 的编译问题,不仅仅只是我这边编译 riscv-openocd 时会遇到,编译其他开源代码同样会出现类似的问题,但基本上原因都是一样的,按照上面的解决方案操作即可。

相信可以帮助到你!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐