一、环境配置
1.vs2017(Oatpp需要c++11支持)
2.Oatpp-1.3.0源码
3.CMake(编译成lib的话需要使用)
二、构建lib的步骤
1.安装cmake
2.执行
3.打开项目可以编译
三、使用Oatpp开发http服务器
1.创建项目
2.添加Oatpp源码
3.配置项目---添加源码
4.添加修改个别文件的编译输出路径(会报同名错误)
warning MSB8027: 名为 *.cpp 的两个或更多文件将生成到同一位置的输出。
Oatpp有几个同名的文件需要修改,也可以在.vcxproj修改
<ClCompile Include="Oatpp\oatpp\core\async\Executor.cpp"> <ObjectFileName>$(IntDir)/oatpp/core/async/Executor.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\core\async\Processor.cpp"> <ObjectFileName>$(IntDir)/oatpp/core/async/Processor.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\core\data\buffer\Processor.cpp"> <ObjectFileName>$(IntDir)/oatpp/core/data/buffer/Processor.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\core\data\mapping\ObjectMapper.cpp"> <ObjectFileName>$(IntDir)/oatpp/core/data/mapping/ObjectMapper.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\network\ConnectionProvider.cpp"> <ObjectFileName>$(IntDir)/oatpp/network/ConnectionProvider.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\network\tcp\server\ConnectionProvider.cpp"> <ObjectFileName>$(IntDir)/oatpp/network/tcp/server/ConnectionProvider.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\network\virtual_\client\ConnectionProvider.cpp"> <ObjectFileName>$(IntDir)/oatpp/network/virtual_/client/ConnectionProvider.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\network\virtual_\server\ConnectionProvider.cpp"> <ObjectFileName>$(IntDir)/oatpp/network/virtual_/server/ConnectionProvider.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\orm\Executor.cpp"> <ObjectFileName>$(IntDir)/oatpp/orm/Executor.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\parser\json\mapping\ObjectMapper.cpp"> <ObjectFileName>$(IntDir)/oatpp/parser/json/mapping/ObjectMapper.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\web\protocol\http\incoming\Request.cpp"> <ObjectFileName>$(IntDir)/oatpp/web/protocol/http/incoming/Request.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\web\protocol\http\incoming\Response.cpp"> <ObjectFileName>$(IntDir)/oatpp/web/protocol/http/incoming/Response.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\web\protocol\http\outgoing\Request.cpp"> <ObjectFileName>$(IntDir)/oatpp/web/protocol/http/outgoing/Request.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\web\protocol\http\outgoing\Response.cpp"> <ObjectFileName>$(IntDir)/oatpp/web/protocol/http/outgoing/Response.cpp.obj</ObjectFileName> </ClCompile> <ClCompile Include="Oatpp\oatpp\network\tcp\client\ConnectionProvider.cpp"> <ObjectFileName>$(IntDir)/oatpp/network/tcp/client/ConnectionProvider.cpp.obj</ObjectFileName> </ClCompile> |
5.添加库目录
wsock32.lib ws2_32.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib |
6.添加代码(案例)
handler.h
#pragma once
#ifndef HANDLER_H #define HANDLER_H
#include "oatpp/web/server/HttpRequestHandler.hpp"
#define O_UNUSED(x) (void)x;
// 自定义请求处理程序 class Handler : public oatpp::web::server::HttpRequestHandler { public: // 处理传入的请求,并返回响应 std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override { O_UNUSED(request);
return ResponseFactory::createResponse(Status::CODE_200, "Hello, World!"); } };
#endif // HANDLER_H |
main.cpp
//#include <stdio.h> //#include <iostream>
#include "oatpp/web/server/HttpConnectionHandler.hpp" #include "oatpp/network/tcp/server/ConnectionProvider.hpp" #include "oatpp/network/Server.hpp" #include "handler.h"
void run() { // 为 HTTP 请求创建路由器 auto router = oatpp::web::server::HttpRouter::createShared();
// 路由 GET - "/hello" 请求到处理程序 router->route("GET", "/hello", std::make_shared<Handler>());
// 创建 HTTP 连接处理程序 auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
// 创建 TCP 连接提供者 auto connectionProvider = oatpp::network::tcp::server::ConnectionProvider::createShared({ "localhost", 8000, oatpp::network::Address::IP_4 });
// 创建服务器,它接受提供的 TCP 连接并将其传递给 HTTP 连接处理程序 oatpp::network::Server server(connectionProvider, connectionHandler);
// 打印服务器端口 OATPP_LOGI("MyApp", "Server running on port %s", connectionProvider->getProperty("port").getData());
// 运行服务器 server.run(); }
int main() { // 初始化 oatpp 环境 oatpp::base::Environment::init();
// 运行应用 run();
// 销毁 oatpp 环境 oatpp::base::Environment::destroy();
return 0; } |
7.编译
8.测试
运行exe程序:
浏览器输入: http://127.0.0.1:8000/hello
warning MSB8027: 名为 *.cpp 的两个或更多文件将生成到同一位置的输出。这会导致错误的生成结果。
VC++编译源文件时默认全部输出(对象文件)到同一个目录下,遇到同名源文件覆盖前面的同名对象文件。为了解决这个问题,你可以设置输出路径与源文件路径类似。
右键项目->属性->配置属性->C/C++->输出文件->对象文件名,将$(IntDir)改为$(IntDir)\$(RelativeDir)\。文章来源:https://www.toymoban.com/news/detail-666831.html
文章来源地址https://www.toymoban.com/news/detail-666831.html
到了这里,关于Oatpp创建服务器项目(windows)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!