编译
下载并解压libhv
项目地址:https://github.com/ithewei/libhv.git
码云镜像:https://gitee.com/libhv/libhv.git
然后开始编译
libhv-master> mkdir mingw
libhv-master> cd mingw
libhv-master> CMake -G "MinGW Makefiles" ../
libhv-master> mingw32-make libhv libhv_static
至此,libhv编译成功,库文件在build\lib下
使用
新建一个工程、将include、lib\libhv.dll复制到工程目录下。
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(test_dll)
set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_NAME}/include)
link_directories(lib)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} libhv.dll)
main.cpp
#include <iostream>
#include "include/hv/hv.h"
#include "include/hv/HttpServer.h"
int main() {
HttpService service;
service.base_url = "/v1/api";
service.POST("/echo", [](HttpRequest* req, HttpResponse* res) {
res->body = req->body;
return 200;
});
http_server_t server;
server.port = 8080;
server.service = &service;
http_server_run(&server);
std::cout << "Hello, World!" << std::endl;
return 0;
}
编译,出错:inet_ntop was not declared in this scope
看了下源码,其实是有这个函数的(ws2tcpip.h中),仅当宏 _WIN32_WINNT >= 0x600时才可用,而我们的宏_WIN32_WINNT默认时0x502,在sdkddkver.h中有各类值,0x502是_WIN32_WINNT_WS03,而0x600是WIN6、VISTA、WS08、LONGHORN,所以只要是WinServer08和VISTA及以上就可以使用。我们在main.cpp中第一行加入:
#define _WIN32_WINNT _WIN32_WINNT_WIN7
解决:
测试:
成功!!!!!!!!!!!!!!