libevent windows编译
准备环境
- cmake
https://cmake.org/download - visual studio
https://visualstudio.microsoft.com/zh-hans/downloads - openssl
https://slproweb.com/products/Win32OpenSSL.html
(假设openssl安装目录为C:\toolchain\OpenSSL-Win64)
下载libevent
https://github.com/libevent/libevent.git
修改CMakeLists.txt
打开libevent主目录下的CMakeLists.txt文件,找到find_package(OpenSSL REQUIRED)的位置,
在其上方添加openssl的安装信息,已告知libevent如何找到openssl, 改动完成后,如下:
...
...
set(OPENSSL_ROOT_DIR "C:/toolchain/OpenSSL-Win64")
set(OPENSSL_LIBRARIES "${OPENSSL_ROOT_DIR}/lib")
set(OPENSSL_INCLUDE_DIR "${OPENSSL_ROOT_DIR}/include")
find_package(OpenSSL REQUIRED)
...
...
配置
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=sdk .
-DCMAKE_INSTALL_PREFIX=sdk 选项表示安装目录为sdk,INSTALL后的所有文件位于此目录,
配置输出参考如下:
G:\Code\libevent> cmake -Bbuild -DCMAKE_INSTALL_PREFIX=sdk .
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.38.33135.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.35.1.windows.2")
-- Looking for __GNU_LIBRARY__
-- Looking for __GNU_LIBRARY__ - not found
-- Looking for _GNU_SOURCE
-- Looking for _GNU_SOURCE - not found
-- Looking for include file fcntl.h
-- Looking for include file fcntl.h - found
......
......
......
-- Found OpenSSL: optimized;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MD.lib;debug;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MDd.lib (found version "1.1.1w")
-- OpenSSL include: C:/toolchain/OpenSSL-Win64/include
-- OpenSSL lib: optimized;C:/toolchain/OpenSSL-Win64/lib/VC/libssl64MD.lib;debug;C:/toolchain/OpenSSL-Win64/lib/VC/libssl64MDd.lib;optimized;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MD.lib;debug;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MDd.lib
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
-- Found PythonInterp: C:/Program Files/Python38/python.exe (found suitable version "3.8.10", minimum required is "3")
-- Generating regress tests...
-- C:\Program Files\CMake\bin\ctest.exe
--
-- ---( Libevent 2.1.12-stable )---
--
-- Available event backends: WIN32
-- CMAKE_BINARY_DIR: G:/Code/libevent/build
-- CMAKE_CURRENT_BINARY_DIR: G:/Code/libevent/build
-- CMAKE_SOURCE_DIR: G:/Code/libevent
-- CMAKE_CURRENT_SOURCE_DIR: G:/Code/libevent
-- PROJECT_BINARY_DIR: G:/Code/libevent/build
-- PROJECT_SOURCE_DIR: G:/Code/libevent
-- CMAKE_MODULE_PATH: G:/Code/libevent/cmake/
-- CMAKE_COMMAND: C:/Program Files/CMake/bin/cmake.exe
-- CMAKE_ROOT: C:/Program Files/CMake/share/cmake-3.24
-- CMAKE_SYSTEM: Windows-10.0.19045
-- CMAKE_SYSTEM_NAME: Windows
-- CMAKE_SYSTEM_VERSION: 10.0.19045
-- CMAKE_SYSTEM_PROCESSOR: AMD64
-- CMAKE_SKIP_RPATH: NO
-- CMAKE_C_COMPILER: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe (id MSVC, clang 0, GNUC 0)
-- CMAKE_AR: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/lib.exe
-- CMAKE_RANLIB: :
--
-- Configuring done
-- Generating done
-- Build files have been written to: G:/Code/libevent/build
关键信息如下,表示找到了openssl:
-- Found OpenSSL: optimized;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MD.lib;debug;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MDd.lib (found version "1.1.1w")
-- OpenSSL include: C:/toolchain/OpenSSL-Win64/include
-- OpenSSL lib: optimized;C:/toolchain/OpenSSL-Win64/lib/VC/libssl64MD.lib;debug;C:/toolchain/OpenSSL-Win64/lib/VC/libssl64MDd.lib;optimized;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MD.lib;debug;C:/toolchain/OpenSSL-Win64/lib/VC/libcrypto64MDd.lib
编译
打开build目录下的vs工程libevent.sln
选择ALL_BUILD生成, 以进行编译
选择INSTALL生成, 以进行安装库文件、头文件、所需的cmake文件等