windoes下利用C++网络库libhv启动一个简单的restful http服务器

windoes下利用C++网络库libhv启动一个简单的restful http服务器

1、libhv简介
libhv是一个类似于libevent、libev、libuv的跨平台网络库,提供了更加简单的API接口和更加丰富的协议(包括http、ftp、smtp、dns、icmp等),libhv已广泛实用在公司的IOT平台、http API服务之中,正确性、稳定性、可扩展性、性能都有保证,完全开源,放心使用。

2、使用cmake编译libhv生成库文件

git clone https://github.com/ithewei/libhv.git
cd libhv
mkdir build
cd build
cmake ..
cmake --build .

上述命令cmake … 如果出现如下报错:

$ cmake ..
-- Building for: NMake Makefiles
CMake Error at CMakeLists.txt:3 (project):
  Running

   'nmake' '-?'

  failed with:

   系统找不到指定的文件。


CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "E:/cpp/aaa/libhv/build/CMakeFiles/CMakeOutput.log".
因为使用的是minGW64,所以使用下面这条命令就能正常编译了
$ cmake -G "MinGW Makefiles" ..

windows下的编译产物如下:

├── bin
│   └── libhv.dll
├── include
│   └── hv
└── lib
    ├── libhv.dll.a
    └── libhv_static.a

3、新建C++项目,将上述文件copy到项目下:
在这里插入图片描述
4、CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.24)
project(libhv_gin)

set(CMAKE_CXX_STANDARD 14)

# 定义工程根目录,CMAKE_SOURCE_DIR为内建变量,表示工程根目录的CMakeLists.txt文件路径
SET(ROOT_DIR ${CMAKE_SOURCE_DIR})

# 用于指定头文件的搜索路径,因为include/hv/HttpServer.h与main.cpp不在同一目录,所以如果不配置这个搜索路径,肯定会报错
include_directories(${ROOT_DIR}/include)
# 指定静态库或者动态库的搜索路径
link_directories(${ROOT_DIR}/lib)

add_executable(libhv_gin main.cpp)

# 指定要连接的静态库
# target_link_libraries(libhv_gin libhv_static.a)
target_link_libraries(libhv_gin libhv.dll.a)

5、如果启动报错:

E:\cpp\libhv-gin\cmake-build-debug\libhv_gin.exe

Process finished with exit code -1073741515 (0xC0000135)

在这里插入图片描述
6、正常启动,访问接口 http://localhost:8080/paths

猜你喜欢

转载自blog.csdn.net/qq_33867131/article/details/127532980
今日推荐