编译环境安装sudoaptupdate# 安装编译环境sudoaptinstall-ygitcmake build-essential# 安装 postgresql (项目用到的数据库)sudoaptinstall-ypostgresql postgresql-client libpq-dev# 安装 pkg-configsudoaptinstall-ypkg-config克隆相关的库# 新建一个空目录mkdiroatppcdoatpp# oatpp 主包gitclone https://github.com/oatpp/oatpp.git# oatpp-swagger swagger包(生产文档)gitclone https://github.com/oatpp/oatpp-swagger.git# oatpp-websocket webSocket包(长连接通信)gitclone https://github.com/oatpp/oatpp-websocket.git# oatpp-postgresql postgresql包(数据库)gitclone https://github.com/oatpp/oatpp-postgresql.git# oatpp-libressl libressl包(https配置)gitclone https://github.com/oatpp/oatpp-libressl.git# oatpp-curl curl包(网络请求获取连接的ip对应的地址)gitclone https://github.com/oatpp/oatpp-curl.git写个安装脚本创建一个脚本(install.sh)输入下面的内容然后使用管理员权限执行执行。#!/bin/bash# 编译并安装build_and_install(){set-e# 任何命令失败立即退出函数echo正在处理项目$(pwd)# 1. 运行所有依赖安装脚本如果存在localdeps_dirutility/install-depsif[[-d$deps_dir]];thenecho发现依赖安装目录$deps_dirfordep_scriptin$deps_dir/install-*.sh;doif[[-f$dep_script]];thenecho正在运行依赖脚本$dep_scriptbash$dep_script||{echo依赖脚本失败$dep_script;return1;}fidoneelseecho未找到依赖目录$deps_dir跳过依赖安装步骤fi# 2. 编译安装流程echo切换到稳定版本分支...gitcheckout1.3.0-latest||{echo切换分支失败;return1;}echo创建并进入 build 目录...mkdir-pbuildcdbuildecho运行 CMake 配置...cmake..||return1echo编译并安装...makemakeinstallecho项目处理完成}# 1. 优先安装 oatpppriority_diroatppif[[-d$priority_dir]];thenecho 优先安装项目$priority_dir(cd$priority_dir||exit1build_and_install)elseecho警告优先项目$priority_dir不存在跳过fifordirin*/;dodirname${dir%/}# 跳过已经处理过的优先目录[[$dirname$priority_dir]]continueif[[-d$dirname]];thenecho进入目录:$dirname(cd$dirname||exit1build_and_install)fidonesudobashinstall.shoatpp-libressl安装依赖的时候可能会报错原因是LibreSSL 3.0.2的测试里面有个bug没改掉我们可以把报错的地方进去修改一下也可以在utility/install-deps/install-libressl.sh中的版本号改成3.8.2就没问题了。测试运行跑一下官网给我们的指引示例(test.cpp)#includeoatpp/web/server/HttpConnectionHandler.hpp#includeoatpp/parser/json/mapping/ObjectMapper.hpp#includeoatpp/network/Server.hpp#includeoatpp/network/tcp/server/ConnectionProvider.hpp#includeoatpp/core/macro/codegen.hpp/* Begin DTO code-generation */#includeOATPP_CODEGEN_BEGIN(DTO)/** * Message Data-Transfer-Object */classMessageDto:publicoatpp::DTO{DTO_INIT(MessageDto,DTO/* Extends */)DTO_FIELD(Int32,statusCode);// Status code fieldDTO_FIELD(String,message);// Message field};/* End DTO code-generation */#includeOATPP_CODEGEN_END(DTO)/** * Custom Request Handler */classHandler:publicoatpp::web::server::HttpRequestHandler{private:std::shared_ptroatpp::data::mapping::ObjectMapperm_objectMapper;public:/** * Constructor with object mapper. * param objectMapper - object mapper used to serialize objects. */Handler(conststd::shared_ptroatpp::data::mapping::ObjectMapperobjectMapper):m_objectMapper(objectMapper){}/** * Handle incoming request and return outgoing response. */std::shared_ptrOutgoingResponsehandle(conststd::shared_ptrIncomingRequestrequest)override{automessageMessageDto::createShared();message-statusCode1024;message-messageHello DTO!;returnResponseFactory::createResponse(Status::CODE_200,message,m_objectMapper);}};voidrun(){/* Create json object mapper */autoobjectMapperoatpp::parser::json::mapping::ObjectMapper::createShared();/* Create Router for HTTP requests routing */autorouteroatpp::web::server::HttpRouter::createShared();/* Route GET - /hello requests to Handler */router-route(GET,/hello,std::make_sharedHandler(objectMapper/* json object mapper */));/* Create HTTP connection handler with router */autoconnectionHandleroatpp::web::server::HttpConnectionHandler::createShared(router);/* Create TCP connection provider */autoconnectionProvideroatpp::network::tcp::server::ConnectionProvider::createShared({localhost,8000,oatpp::network::Address::IP_4});/* Create server which takes provided TCP connections and passes them to HTTP connection handler */oatpp::network::Serverserver(connectionProvider,connectionHandler);/* Print info about server port */OATPP_LOGI(MyApp,Server running on port %s,(constchar*)connectionProvider-getProperty(port).getData());/* Run server */server.run();}intmain(){/* Init oatpp Environment */oatpp::base::Environment::init();/* Run App */run();/* Destroy oatpp Environment */oatpp::base::Environment::destroy();return0;}如果你的编辑器再带检查功能就会发现文件开始的引入部分会报错因为系统找不到那些头文件。在Linux环境中编译安装的项目头文件一般都会放到/usr/local/include目录中我们查看这个目录就会看到并没有我们期望映入的oatpp目录而是oatpp-1.3.0目录。我们所安装的所有的包其实都在这个目录中为了让我们的编辑器能够顺利的找到头文件我们可以给它们做些软连接。sudoln-s/usr/local/include/oatpp-1.3.0/oatpp/oatpp /usr/local/include/oatppsudoln-s/usr/local/include/oatpp-1.3.0/oatpp/oatpp-test /usr/local/include/oatpp-testsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-curl/oatpp-curl /usr/local/include/oatpp-curlsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-libressl/oatpp-libressl /usr/local/include/oatpp-libresslsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-postgresql/oatpp-postgresql /usr/local/include/oatpp-postgresqlsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-swagger/oatpp-swagger /usr/local/include/oatpp-swaggersudoln-s/usr/local/include/oatpp-1.3.0/oatpp-websocket/oatpp-websocket /usr/local/include/oatpp-websocket做好了软连接再次打开编辑器应该就不会报错了。下面我们尝试编译一下刚刚创建的测试项目。g test.cpp-loatpp-otest运行报错内容如下:/usr/bin/ld: cannot find -lliboatpp: No such file or directory collect2: error: ld returned 1 exit status意思就是找不到静态库同样的编译安装的静态库(.a文件)一般都在/usr/local/lib目录中。查看它同样会发现没有liboatpp.a的文件这些文件也是在/usr/local/lib目录下一个oatpp-1.3.0的目录中同样的要让我们顺利的编译可以像处理头文件一样的挨个创建软连接。当然也可以直接在编译的过程指定静态文件所在的目录。g test.cpp -L/usr/local/lib/oatpp-1.3.0/-loatpp-otest不出意外的话现在是可以编译成功的编译成功后运行编译出来的可执行文件运行状况如下:./test I|2026-05-2222:22:411779459761064195|MyApp:Server running on port8000然后打开浏览器输入请求地址localhost:8000/hello可以看到{statusCode:1024,message:Hello DTO!}说明oatpp的开发环境已经部署完全。
oatpp开发环境在linux上的部署
发布时间:2026/5/25 21:16:10
编译环境安装sudoaptupdate# 安装编译环境sudoaptinstall-ygitcmake build-essential# 安装 postgresql (项目用到的数据库)sudoaptinstall-ypostgresql postgresql-client libpq-dev# 安装 pkg-configsudoaptinstall-ypkg-config克隆相关的库# 新建一个空目录mkdiroatppcdoatpp# oatpp 主包gitclone https://github.com/oatpp/oatpp.git# oatpp-swagger swagger包(生产文档)gitclone https://github.com/oatpp/oatpp-swagger.git# oatpp-websocket webSocket包(长连接通信)gitclone https://github.com/oatpp/oatpp-websocket.git# oatpp-postgresql postgresql包(数据库)gitclone https://github.com/oatpp/oatpp-postgresql.git# oatpp-libressl libressl包(https配置)gitclone https://github.com/oatpp/oatpp-libressl.git# oatpp-curl curl包(网络请求获取连接的ip对应的地址)gitclone https://github.com/oatpp/oatpp-curl.git写个安装脚本创建一个脚本(install.sh)输入下面的内容然后使用管理员权限执行执行。#!/bin/bash# 编译并安装build_and_install(){set-e# 任何命令失败立即退出函数echo正在处理项目$(pwd)# 1. 运行所有依赖安装脚本如果存在localdeps_dirutility/install-depsif[[-d$deps_dir]];thenecho发现依赖安装目录$deps_dirfordep_scriptin$deps_dir/install-*.sh;doif[[-f$dep_script]];thenecho正在运行依赖脚本$dep_scriptbash$dep_script||{echo依赖脚本失败$dep_script;return1;}fidoneelseecho未找到依赖目录$deps_dir跳过依赖安装步骤fi# 2. 编译安装流程echo切换到稳定版本分支...gitcheckout1.3.0-latest||{echo切换分支失败;return1;}echo创建并进入 build 目录...mkdir-pbuildcdbuildecho运行 CMake 配置...cmake..||return1echo编译并安装...makemakeinstallecho项目处理完成}# 1. 优先安装 oatpppriority_diroatppif[[-d$priority_dir]];thenecho 优先安装项目$priority_dir(cd$priority_dir||exit1build_and_install)elseecho警告优先项目$priority_dir不存在跳过fifordirin*/;dodirname${dir%/}# 跳过已经处理过的优先目录[[$dirname$priority_dir]]continueif[[-d$dirname]];thenecho进入目录:$dirname(cd$dirname||exit1build_and_install)fidonesudobashinstall.shoatpp-libressl安装依赖的时候可能会报错原因是LibreSSL 3.0.2的测试里面有个bug没改掉我们可以把报错的地方进去修改一下也可以在utility/install-deps/install-libressl.sh中的版本号改成3.8.2就没问题了。测试运行跑一下官网给我们的指引示例(test.cpp)#includeoatpp/web/server/HttpConnectionHandler.hpp#includeoatpp/parser/json/mapping/ObjectMapper.hpp#includeoatpp/network/Server.hpp#includeoatpp/network/tcp/server/ConnectionProvider.hpp#includeoatpp/core/macro/codegen.hpp/* Begin DTO code-generation */#includeOATPP_CODEGEN_BEGIN(DTO)/** * Message Data-Transfer-Object */classMessageDto:publicoatpp::DTO{DTO_INIT(MessageDto,DTO/* Extends */)DTO_FIELD(Int32,statusCode);// Status code fieldDTO_FIELD(String,message);// Message field};/* End DTO code-generation */#includeOATPP_CODEGEN_END(DTO)/** * Custom Request Handler */classHandler:publicoatpp::web::server::HttpRequestHandler{private:std::shared_ptroatpp::data::mapping::ObjectMapperm_objectMapper;public:/** * Constructor with object mapper. * param objectMapper - object mapper used to serialize objects. */Handler(conststd::shared_ptroatpp::data::mapping::ObjectMapperobjectMapper):m_objectMapper(objectMapper){}/** * Handle incoming request and return outgoing response. */std::shared_ptrOutgoingResponsehandle(conststd::shared_ptrIncomingRequestrequest)override{automessageMessageDto::createShared();message-statusCode1024;message-messageHello DTO!;returnResponseFactory::createResponse(Status::CODE_200,message,m_objectMapper);}};voidrun(){/* Create json object mapper */autoobjectMapperoatpp::parser::json::mapping::ObjectMapper::createShared();/* Create Router for HTTP requests routing */autorouteroatpp::web::server::HttpRouter::createShared();/* Route GET - /hello requests to Handler */router-route(GET,/hello,std::make_sharedHandler(objectMapper/* json object mapper */));/* Create HTTP connection handler with router */autoconnectionHandleroatpp::web::server::HttpConnectionHandler::createShared(router);/* Create TCP connection provider */autoconnectionProvideroatpp::network::tcp::server::ConnectionProvider::createShared({localhost,8000,oatpp::network::Address::IP_4});/* Create server which takes provided TCP connections and passes them to HTTP connection handler */oatpp::network::Serverserver(connectionProvider,connectionHandler);/* Print info about server port */OATPP_LOGI(MyApp,Server running on port %s,(constchar*)connectionProvider-getProperty(port).getData());/* Run server */server.run();}intmain(){/* Init oatpp Environment */oatpp::base::Environment::init();/* Run App */run();/* Destroy oatpp Environment */oatpp::base::Environment::destroy();return0;}如果你的编辑器再带检查功能就会发现文件开始的引入部分会报错因为系统找不到那些头文件。在Linux环境中编译安装的项目头文件一般都会放到/usr/local/include目录中我们查看这个目录就会看到并没有我们期望映入的oatpp目录而是oatpp-1.3.0目录。我们所安装的所有的包其实都在这个目录中为了让我们的编辑器能够顺利的找到头文件我们可以给它们做些软连接。sudoln-s/usr/local/include/oatpp-1.3.0/oatpp/oatpp /usr/local/include/oatppsudoln-s/usr/local/include/oatpp-1.3.0/oatpp/oatpp-test /usr/local/include/oatpp-testsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-curl/oatpp-curl /usr/local/include/oatpp-curlsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-libressl/oatpp-libressl /usr/local/include/oatpp-libresslsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-postgresql/oatpp-postgresql /usr/local/include/oatpp-postgresqlsudoln-s/usr/local/include/oatpp-1.3.0/oatpp-swagger/oatpp-swagger /usr/local/include/oatpp-swaggersudoln-s/usr/local/include/oatpp-1.3.0/oatpp-websocket/oatpp-websocket /usr/local/include/oatpp-websocket做好了软连接再次打开编辑器应该就不会报错了。下面我们尝试编译一下刚刚创建的测试项目。g test.cpp-loatpp-otest运行报错内容如下:/usr/bin/ld: cannot find -lliboatpp: No such file or directory collect2: error: ld returned 1 exit status意思就是找不到静态库同样的编译安装的静态库(.a文件)一般都在/usr/local/lib目录中。查看它同样会发现没有liboatpp.a的文件这些文件也是在/usr/local/lib目录下一个oatpp-1.3.0的目录中同样的要让我们顺利的编译可以像处理头文件一样的挨个创建软连接。当然也可以直接在编译的过程指定静态文件所在的目录。g test.cpp -L/usr/local/lib/oatpp-1.3.0/-loatpp-otest不出意外的话现在是可以编译成功的编译成功后运行编译出来的可执行文件运行状况如下:./test I|2026-05-2222:22:411779459761064195|MyApp:Server running on port8000然后打开浏览器输入请求地址localhost:8000/hello可以看到{statusCode:1024,message:Hello DTO!}说明oatpp的开发环境已经部署完全。