最近再看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这个依赖项。