meson和pkg-config

解决meson编译libevent_pthreads问题:pkg-config配置与源码安装
本文讲述了如何在meson构建中解决因pkg-config未找到libevent_pthreads而编译报错的问题,涉及apt安装、源码编译、pkg-config搜索路径设置和.pc文件编写的过程。

最近再看simple-cam,它是用meson构建的。

有这样一句:

deps = [
      dependency('libevent-pthreads'),
]

执行编译时报错:

meson.build:16:0: ERROR: Dependency "libevent_pthreads" not found, tried pkgconfig and cmake

但实际上libevent_pthreads我已经用apt安装过了,但是为什么还找不到?

原因是meson使用pkg-config来查找依赖项:

Pkg-config is a way for shared libraries to declare the compiler flags needed to use them.

那么原因就是pkg-config没有找到libevent-pthread。

pkg-config是如何查找到各种library的?它通过一些.pc文件来查找。

首先,我们可以用pkg-config查看所有的安装包:

pkg-config --list-all

然后,用下面代码查看pkg-config执行时搜索的文件:

$ pkg-config --variable pc_path pkg-config
/usr/local/lib/x86_64-linux-gnu/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig

在这些目录下添加自己编写的pc文件即可。

比如:

prefix=/usr/local
includedir=${prefix}/include
libdir=${prefix}/lib/x86_64-linux-gnu

Name: libcamera
Description: Complex Camera Support Library
Version: 0.0.0
Requires: libcamera-base
Libs: -L${libdir} -lcamera
Cflags: -I${includedir}/libcamera

其中,libdir是通过`dpkg -L`来看到的。我们看一下libevent_pthreads的情况:

$ dpkg -L libevent-pthreads-2.1-6
/.
/usr
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libevent_pthreads-2.1.so.6.0.2
/usr/share
/usr/share/doc
/usr/share/doc/libevent-pthreads-2.1-6
/usr/share/doc/libevent-pthreads-2.1-6/copyright
/usr/lib/x86_64-linux-gnu/libevent_pthreads-2.1.so.6
/usr/share/doc/libevent-pthreads-2.1-6/changelog.Debian.gz

可以看出,没有头文件。看来不能用apt来安装它、必须用源码安装的方式。我们从libevent.org上下载libevent的源码,执行configure、make和sudo make install,可以看到:

Libraries have been installed in:
   /usr/local/lib

/usr/bin/install -c -m 644 libevent.pc libevent_core.pc libevent_extra.pc libevent_pthreads.pc libevent_openssl.pc '/usr/local/lib/pkgconfig'

查看/usr/local/lib/pkgconfig/libevent_pthreads.pc:

#libevent pkg-config source file

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: libevent_pthreads
Description: libevent_pthreads adds pthreads-based threading support to libevent
Version: 2.1.12-stable
Requires: libevent
Conflicts:
Libs: -L${libdir} -levent_pthreads
Libs.private:  
Cflags: -I${includedir} -pthread

再执行pkg-config --list-all就可以看到libevent_pthread了。

然后执行meson build也可以顺利的找到libevent_pthread这个依赖项。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值