我遇到的问题
根据一般习惯而言,使用cmake链接某个第三方库时,一般先使用find_package()
由cmake帮忙找出相应的package。然后,在生成链接时,只需要使用target_link_libraries()
即可。
然而上述的方案碰上 链接libevent却失效了。
-
相关背景: 我是通过apt安装的libevent
-
报错信息
By not providing "FindLibevent.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Libevent", but CMake did not find one. Could not find a package configuration file provided by "Libevent" with any of the following names: libeventConfig.cmake libevent-config.cmake Add the installation prefix of "Libevent" to CMAKE_PREFIX_PATH or set "Libevent_DIR" to a directory containing one of the above files. If "Libevent" provides a separate development package or SDK, be sure it has been installed.
通过find /usr/share/cmake-3.16/Help/module -name | grep event
确实找不出与libevent相关的文件
解决方法:
使用 event 链接(应该是巧合)
# ...
add_executable(demo <源文件>)
# 注意是event
target_link_libraies(demo event)
#...
根除错误
在cmake的module文件夹下找不到相应库的***Config.cmake
文件。
在这里其实大概知道问题所在了。由于我是通过sudo apt-get install libevent-dev
安装的,可能没有帮我将libevent管理模块添加到cmake中,导致find_package找不到。
-
验证
- 从github中下载libevent 并传输到linux里
- 进入
libevent-master/test-export/
- 执行下述命令
& mkdir build && cd build & cmake ..
结果出现了一模一样的报错
-
根据文档通过cmake重新安装libevent (重装估计最方便了 \doge \doge)
libevent/Documentation/Building.md
最后务必执行make install
注意到最后几行字,文件终于回来了: )
-- Installing: /usr/local/lib/cmake/libevent/LibeventConfig.cmake -- Installing: /usr/local/lib/cmake/libevent/LibeventConfigVersion.cmake -- Installing: /usr/local/lib/cmake/libevent/LibeventTargets-static.cmake -- Installing: /usr/local/lib/cmake/libevent/LibeventTargets-static-release.cmake -- Installing: /usr/local/lib/cmake/libevent/LibeventTargets-shared.cmake -- Installing: /usr/local/lib/cmake/libevent/LibeventTargets-shared-release.cmake
再操作一下上述的验证步骤,cmake能找到了libevent了
-- Found libevent include directory: /usr/local/include -- Found libevent component: /usr/local/lib/libevent_core.so -- Found libevent component: /usr/local/lib/libevent_extra.so -- Found libevent component: /usr/local/lib/libevent_openssl.so -- Found libevent component: /usr/local/lib/libevent_mbedtls.so -- Found libevent component: /usr/local/lib/libevent_pthreads.so -- Found libevent 2.2.0 in /usr/local
apt 有时很方便,有时可能很麻烦 : (
最后附上cmake代码
cmake_minimum_required(VERSION 3.0)
project(demo)
find_package(Libevent REQUIRED)
# LIBEVENT_STATIC_LINK=TRUE
# set(LIBEVENT_STATIC_LIBRARIES "core; extra")
link_directories(
/usr/local/lib
/usr/lib
)
include_directories(
/usr/local/include/
/usr/include
)
add_compile_options(-Wall)
add_executable(demo demo.cpp)
target_link_libraries(demo libevent::core)