为什么 pkg-config 会用另一个默认值覆盖 'prefix'?

Why pkg-config overrides 'prefix' with another default one?

我在使用 pkg-config link 某些程序库时遇到问题。问题是 'prefix' 每个库中的变量 pkg-config 文件 (*.pc) 都被不需要的目录覆盖,导致构建程序找不到库的头文件和 lib 文件。

此处,'*.pc' 文件之一,x264.pc:

prefix=/e/x264/x64-windows-rel  
exec_prefix=${prefix}   
libdir=${exec_prefix}/lib   
includedir=${prefix}/include    
...

我 运行 来自 MSYS2 终端的这个:

pkg-config --cflags --debug x264

这是它追踪到的一些输出:

...
 Parsing package file 'D:/msys64/usr/local/lib/pkgconfig\x264.pc'
 line>prefix=/e/x264/x64-windows-rel
 Variable declaration, 'prefix' overridden with 'D:/msys64/usr/local'
...

请注意,MSYS2 和 pkg-config 已更新为最新版本。

谁能告诉我为什么会这样,以及如何在不将 'prefix' 重命名为其他名称的情况下解决问题。? 谢谢

看起来你的 pkg-config 是用 --enable-define-prefix 选项编译的(看起来像 Windows 目标的默认设置)并且你的 .pc 文件位于名称为 pkgconfig 的目录中(D:/msys64/usr/local/lib/pkgconfig\x264.pc).顺便说一句,为什么 .pc 文件安装在 D:/msys64/usr/local/lib/pkgconfig 而库安装在其他地方?

这里引用 pkg-config 手册页:

    --define-prefix
          --dont-define-prefix  These  options  control whether pkg-config
          overrides the value of the variable prefix  in  each  .pc  file.
          With  --define-prefix, pkg-config uses the installed location of
          the .pc file to determine the prefix. --dont-define-prefix  pre-
          vents this behavior. The default is usually --define-prefix.

          When this feature is enabled and a .pc file is found in a direc-
          tory named pkgconfig, the prefix for that package is assumed  to
          be  the  grandparent  of the directory where the file was found,
          and the prefix variable is overridden for that file accordingly.

          If  the value of a variable in a .pc file begins with the origi-
          nal, non-overridden, value of  the  prefix  variable,  then  the
          overridden value of prefix is used instead. This allows the fea-
          ture to work even when the variables have been expanded  in  the
          .pc file.

因此您可以通过 --dont-define-prefix 选项禁用此行为。

pkg-config manual page (source code)

pkg-config manual page (rendered)

P.S。 MSYS2 有几个 pkg-config 包。你应该使用 mingw64/mingw-w64-x86_64-pkg-configmingw32/mingw-w64-i686-pkg-config。不要使用 msys/pkg-config.