CMake在macOS上找不到libevent

tpgth1q7  于 2023-03-23  发布在  Mac
关注(0)|答案(2)|浏览(270)

我已经在macOS上安装了libevent-

$ brew install libevent

我正在尝试将其导入到我的CMakeLists.txt中-

cmake_minimum_required(VERSION 3.14)
project(xyz)
set(CMAKE_CXX_STANDARD 17)
find_package(libevent REQUIRED)

我得到以下CMake错误-

CMake Error at CMakeLists.txt:6 (find_package):
  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.

有人能告诉我如何在CMake中导入安装在系统中的libevent吗?

jhdbpxl9

jhdbpxl91#

我也有同样的问题,但我通过添加来解决这个问题:

link_directories(
  /usr/local/lib
  /usr/lib
)
INCLUDE_DIRECTORIES(
  /usr/local/include/
  /usr/include
)

它只是告诉Makefile在哪里可以找到libevent头和库。

goucqfw6

goucqfw62#

1.必须安装pkg-config

brew install pkg-config

1.然后在CMakeList.txt中

find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
    pkg_search_module(LIBEVENT libevent )
    if (LIBEVENT_FOUND)
        message(STATUS "Found libevent ${LIBEVENT_FOUND}")
        message(STATUS "libevent inc : ${LIBEVENT_INCLUDE_DIRS}")
        message(STATUS "libevent lib : ${LIBEVENT_LIBRARIES}")
    endif ()
endif()

# ...
add_executable(${PROJECT_NAME} ${SRC_FILES})
target_include_directories(${PROJECT_NAME} PRIVATE ${LIBEVENT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBEVENT_LIBRARIES})

相关问题