在本地编译和安装了某个库后,如果其lib
目录下存在pkgconfig
子目录,则子目录下会存在若干.pc
文件,文件中会有prefix
的配置(该配置标识当前库的安装路径),当要把该库拷贝到其他机器上时,如果库的路径不一致,需要修改lib/pkgconfig
目录下.pc
文件的prefix
配置。
以curl
库举例说明:
其中
libcurl.pc
文件内容为:
由于该库是在其他机器上编译的,编译时指定的库路径为:/opt/curl-7.76.1-ubuntu-x64
,但是拷贝到本机时,存放的路径为:/home/jaron/curl-7.76.1-ubuntu-x64
,因此需要对该文件进行修改,修改后为:
当有很多.pc文件时,或者路径比较多时,为了方便快速的根据存放路径进行设置,这里写了一个更新pkgconfig文件脚本,注意:需要把该脚本文件放在库所在目录,例如:
update_pkgconfig.sh
#!/bin/bash
# 更新pkgconfig文件的安装目录(支持递归)
update_pkgconfig_prefix()
{
filePath=$1
prefixDir=$2
for file in `ls -a $filePath`
do
if [ -d ${filePath}/$file ]
then
# 子目录(递归)
if [[ $file != '.' && $file != '..' ]]
then
update_pkgconfig_prefix ${filePath}/$file $prefixDir
fi
else
filename=${filePath}/$file
# 查找.pc后缀的文件
if [ "${filename##*.}"x = "pc"x ]
then
# 查找设置prefix的所在行号
prefix_line=`grep -n "prefix=/" $filename | cut -d ":" -f 1`
# 新的设置内容
prefix_str="prefix=$prefixDir"
# 替换
sed -i "$prefix_line c $prefix_str" $filename
fi
fi
done
}
workdir=$(cd $(dirname $0); pwd)
update_pkgconfig_prefix $workdir/lib/pkgconfig $workdir