本文主要是介绍Drogon Win11 编译 /MT,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Drogon是一个基于C++17/20的Http应用框架,使用Drogon可以方便的使用C++构建各种类型的Web应用服务端程序。
Drogon的主要应用平台是Linux,也支持Mac OS、FreeBSD和Windows。
它的主要特点如下:
- 网络层使用基于epoll(macOS/FreeBSD下是kqueue)的非阻塞IO框架,提供高并发、高性能的网络IO。详细请见TFB Tests Results;
- 全异步编程模式;
- 支持Http1.0/1.1(server端和client端);
- 基于template实现了简单的反射机制,使主程序框架、控制器(controller)和视图(view)完全解耦;
- 支持cookies和内建的session;
- 支持后端渲染,把控制器生成的数据交给视图生成Html页面,视图由CSP模板文件描述,通过CSP标签把C++代码嵌入到Html页面,由drogon的命令行工具在编译阶段自动生成C++代码并编译;
- 支持运行期的视图页面动态加载(动态编译和加载so文件);
- 非常方便灵活的路径(path)到控制器处理函数(handler)的映射方案;
- 支持过滤器(filter)链,方便在控制器之前执行统一的逻辑(如登录验证、Http Method约束验证等);
- 支持https(基于OpenSSL实现);
- 支持websocket(server端和client端);
- 支持Json格式请求和应答, 对Restful API应用开发非常友好;
- 支持文件下载和上传,支持sendfile系统调用;
- 支持gzip/brotli压缩传输;
- 支持pipelining;
- 提供一个轻量的命令行工具drogon_ctl,帮助简化各种类的创建和视图代码的生成过程;
- 基于非阻塞IO实现的异步数据库读写,目前支持PostgreSQL和MySQL(MariaDB)数据库;
- 基于线程池实现sqlite3数据库的异步读写,提供与上文数据库相同的接口;
- 支持ARM架构;
- 方便的轻量级ORM实现,支持常规的对象到数据库的双向映射操作;
- 支持插件,可通过配置文件在加载期动态拆装;
- 支持内建插入点的AOP
地址:Drogon
因为想着编译/MT版本,所以就手动编译了Drogon,记录下。
依赖库
- 内置
- trantor,non-blocking I/O C++网络库,也是作者开发,已作为git仓库submodule,无需提前安装;
- 必须
- jsoncpp,json的c++库,版本不低于1.7;
- libuuid,生成uuid的c库;
- zlib,用于支持压缩传输;
- 可选
- boost,版本不低于1.61,只在C++编译器不支持c++17或STL库不完整支持
std::filesystem
时才需要安装; - OpenSSL,安装后drogon将支持HTTPS,否则drogon只支持HTTP;
- c-ares, 安装后drogon对DNS的支持会具有更好的性能;
- libbrotli,安装后drogon的HTTP响应会支持brotli压缩;
- postgreSQL, mariadb, sqlite3的客户端开发库,安装后drogon会提供对相应的库的访问能力;
- hiredis, 安装后drogon将支持redis的访问;
- gtest, 安装后drogon的单元测试代码可以被编译;
- yaml-cpp, 安装后drogon将支持yaml格式的配置文件;
- boost,版本不低于1.61,只在C++编译器不支持c++17或STL库不完整支持
准备工具:Cmake, Git, Visual Stutio,
- Windows 10(11) SDK
- Cmake
- Git
- Visual Stutio 2022(2019)
先下载源码
cd $WORK_PATH
git clone https://github.com/drogonframework/drogon
cd drogon
git submodule update --init
依赖不用conan和vcpkg安装 自己编译 /MT版本
- jsoncpp1.9.5 地址:jsoncpp
git clone https://github.com/open-source-parsers/jsoncpp
cd jsoncpp
#修改CMakeLists.txt
BUILD_SHARED_LIBS 改为OFF
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
下面使用vs2022打开编译即可
- zlib 地址:zlib
git clone -b master https://github.com/madler/zlib.git
使用Cmake也可以,直接使用vs2022新建工程将头文件添加库目录,将src添加进工程,选择库lib
代码生成选择/MT 编译即可。
- boost 下载VC143 64位 预编译好的即可 地址:boost
- OpenSSL 编译参考之前的编译教程:Openssl编译
- c-ares 地址:c-ares
git clone https://github.com/c-ares/c-ares
cd c-ares
#修改CMakeLists.txt
CARES_STATIC ON
CARES_SHARED OFF
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
使用vs2022打开编译即可
- libbrotli 地址:brotli
git clone https://github.com/google/brotli
cd brotli
#修改CMakeLists.txt
BUILD_SHARED_LIBS OFF
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
使用vs2022打开编译即可
- postgreSQL, mariadb
直接下载二进制安装文件,安装后头文件和库文件copy出来即可。
地址:postgresql 下载地址:postgresql-downloads mariadb
- hiredis 地址:hiredis
git clone https://github.com/redis/hiredis
cd hiredis
#修改CMakeLists.txt
BUILD_SHARED_LIBS OFF
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
使用vs2022打开编译即可
注:预处理器 内容为
%(PreprocessorDefinitions)
WIN32
_WINDOWS
NDEBUG
_CRT_SECURE_NO_WARNINGS
WIN32_LEAN_AND_MEAN
CMAKE_INTDIR="Release"
hiredis_EXPORTS
-
Sqlite3 地址:sqlite3
下载版本为 Source Code 中的 sqlite-amalgamation 其中 sqlite-autoconf这个源码要tcl辅助编译。
源码共4个文件两个c文件两个头文件
新建工程,类型库文件lib 运行库/MT
将c文件添加至工程头文件添加库目录编译即可。
- yaml-cpp 地址:yaml-cpp
git clone https://github.com/jbeder/yaml-cpp
使用 cmake-gui打开
- 只勾选下面2个即可,别的都不要勾选
YAML_CPP_BUILD_CONTRIB
YAML_CPP_BUILD_TOOLS
生成工程文件后,使用vs2022打开编译即可
- gtest 地址:googletest
git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
重头戏,开始编译Drogon
新建工程文件,分别添加三个项目
- drogon 类型库文件lib
- drogon_ctl 类型 应用程序exe
- trantor 类型库文件lib
将源文件添加至对应的项目中,设置头文件引用库目录,添加链接器库文件编译。
本文源码目录如下
项目包含如下:
- drogon
源文件
<ItemGroup><ClCompile Include="..\..\drogon\lib\src\AccessLogger.cc" /><ClCompile Include="..\..\drogon\lib\src\AOPAdvice.cc" /><ClCompile Include="..\..\drogon\lib\src\CacheFile.cc" /><ClCompile Include="..\..\drogon\lib\src\ConfigAdapterManager.cc" /><ClCompile Include="..\..\drogon\lib\src\ConfigLoader.cc" /><ClCompile Include="..\..\drogon\lib\src\Cookie.cc" /><ClCompile Include="..\..\drogon\lib\src\DrClassMap.cc" /><ClCompile Include="..\..\drogon\lib\src\drogon_test.cc" /><ClCompile Include="..\..\drogon\lib\src\DrTemplateBase.cc" /><ClCompile Include="..\..\drogon\lib\src\FiltersFunction.cc" /><ClCompile Include="..\..\drogon\lib\src\FixedWindowRateLimiter.cc" /><ClCompile Include="..\..\drogon\lib\src\GlobalFilters.cc" /><ClCompile Include="..\..\drogon\lib\src\Histogram.cc" /><ClCompile Include="..\..\drogon\lib\src\Hodor.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpAppFrameworkImpl.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpBinder.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpClientImpl.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpControllersRouter.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpFileImpl.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpFileUploadRequest.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpRequestImpl.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpRequestParser.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpResponseImpl.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpResponseParser.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpServer.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpSimpleControllersRouter.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpUtils.cc" /><ClCompile Include="..\..\drogon\lib\src\HttpViewData.cc" /><ClCompile Include="..\..\drogon\lib\src\IntranetIpFilter.cc" /><ClCompile Include="..\..\drogon\lib\src\JsonConfigAdapter.cc" /><ClCompile Include="..\..\drogon\lib\src\ListenerManager.cc" /><ClCompile Include="..\..\drogon\lib\src\LocalHostFilter.cc" /><ClCompile Include="..\..\drogon\lib\src\MultiPart.cc" /><ClCompile Include="..\..\drogon\lib\src\NotFound.cc" /><ClCompile Include="..\..\drogon\lib\src\PluginsManager.cc" /><ClCompile Include="..\..\drogon\lib\src\PromExporter.cc" /><ClCompile Include="..\..\drogon\lib\src\RangeParser.cc" /><ClCompile Include="..\..\drogon\lib\src\RateLimiter.cc" /><ClCompile Include="..\..\drogon\lib\src\RealIpResolver.cc" /><ClCompile Include="..\..\drogon\lib\src\Redirector.cc" /><ClCompile Include="..\..\drogon\lib\src\SecureSSLRedirector.cc" /><ClCompile Include="..\..\drogon\lib\src\SessionManager.cc" /><ClCompile Include="..\..\drogon\lib\src\SlashRemover.cc" /><ClCompile Include="..\..\drogon\lib\src\SlidingWindowRateLimiter.cc" /><ClCompile Include="..\..\drogon\lib\src\StaticFileRouter.cc" /><ClCompile Include="..\..\drogon\lib\src\TaskTimeoutFlag.cc" /><ClCompile Include="..\..\drogon\lib\src\TokenBucketRateLimiter.cc" /><ClCompile Include="..\..\drogon\lib\src\Utilities.cc" /><ClCompile Include="..\..\drogon\lib\src\WebSocketClientImpl.cc" /><ClCompile Include="..\..\drogon\lib\src\WebSocketConnectionImpl.cc" /><ClCompile Include="..\..\drogon\lib\src\WebsocketControllersRouter.cc" /><ClCompile Include="..\..\drogon\lib\src\YamlConfigAdapter.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\RedisClientImpl.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\RedisClientLockFree.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\RedisClientManager.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\RedisConnection.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\RedisResult.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\RedisSubscriberImpl.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\RedisTransactionImpl.cc" /><ClCompile Include="..\..\drogon\nosql_lib\redis\src\SubscribeContext.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\ArrayParser.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\Criteria.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\DbClient.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\DbClientImpl.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\DbClientLockFree.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\DbClientManager.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\DbConnection.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\DbListener.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\Exception.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\Field.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\mysql_impl\MysqlConnection.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\mysql_impl\MysqlResultImpl.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\postgresql_impl\PgConnection.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\postgresql_impl\PgListener.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\postgresql_impl\PostgreSQLResultImpl.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\RestfulController.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\Result.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\Row.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\SqlBinder.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\sqlite3_impl\Sqlite3Connection.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\sqlite3_impl\Sqlite3ResultImpl.cc" /><ClCompile Include="..\..\drogon\orm_lib\src\TransactionImpl.cc" /><ClCompile Include="..\..\drogon\third_party\mman-win32\mman.c" /></ItemGroup>
头文件
项目属性---c/c++---常规---附加包含目录
地址根据自己存放源代码目录为准!!!!!!
..\..\drogon\third_party\mman-win32;..\..\drogon\nosql_lib\redis\inc;..\..\drogon\orm_lib\inc;..\..\drogon\trantor;..\..\drogon\lib\inc;..\..\drogon\lib\src;..\..\drogon;..\..\drogon\orm_lib\src\mysql_impl;%(AdditionalIncludeDirectories)
预处理器
%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;USE_BROTLI;DBUG_OFF;USE_REDIS;CMAKE_INTDIR="Release";HAS_YAML_CPP;YAML_CPP_STATIC_DEFINE
代码生成--运行库 /MT
所有选项--附加选项
%(AdditionalOptions) /external:I "..\..\..\3rdsdk\jsoncpp\include" /external:I "..\..\..\3rdsdk\brotli\include" /external:I "..\..\..\3rdsdk\postgresql\include" /external:I "..\..\..\3rdsdk\sqlite3\include" /external:I "..\..\..\3rdsdk\hiredis\include" /external:I "..\..\..\3rdsdk\zlib\include" /external:I "..\..\..\3rdsdk\yaml-cpp\include" /external:I "..\..\..\3rdsdk\mariadb\include\mysql" /external:I "..\..\..\3rdsdk\yaml-cpp\include" /utf-8 /Zc:__cplusplus
文档管理程序---所有选项
附加依赖项
..\..\..\3rdsdk\yaml-cpp\lib\yaml-cpp.lib
- trantor
源文件
<ItemGroup><ClCompile Include="..\..\drogon\third_party\wepoll\Wepoll.c" /><ClCompile Include="..\..\drogon\trantor\net\Channel.cc" /><ClCompile Include="..\..\drogon\trantor\net\EventLoop.cc" /><ClCompile Include="..\..\drogon\trantor\net\EventLoopThread.cc" /><ClCompile Include="..\..\drogon\trantor\net\EventLoopThreadPool.cc" /><ClCompile Include="..\..\drogon\trantor\net\InetAddress.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\Acceptor.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\Connector.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\NormalResolver.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\Poller.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\poller\EpollPoller.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\poller\KQueue.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\poller\PollPoller.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\Socket.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\TcpConnectionImpl.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\Timer.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\TimerQueue.cc" /><ClCompile Include="..\..\drogon\trantor\net\inner\tlsprovider\OpenSSLProvider.cc" /><ClCompile Include="..\..\drogon\trantor\net\TcpClient.cc" /><ClCompile Include="..\..\drogon\trantor\net\TcpServer.cc" /><ClCompile Include="..\..\drogon\trantor\utils\AsyncFileLogger.cc" /><ClCompile Include="..\..\drogon\trantor\utils\ConcurrentTaskQueue.cc" /><ClCompile Include="..\..\drogon\trantor\utils\crypto\openssl.cc" /><ClCompile Include="..\..\drogon\trantor\utils\Date.cc" /><ClCompile Include="..\..\drogon\trantor\utils\Logger.cc" /><ClCompile Include="..\..\drogon\trantor\utils\LogStream.cc" /><ClCompile Include="..\..\drogon\trantor\utils\MsgBuffer.cc" /><ClCompile Include="..\..\drogon\trantor\utils\SerialTaskQueue.cc" /><ClCompile Include="..\..\drogon\trantor\utils\TimingWheel.cc" /><ClCompile Include="..\..\drogon\trantor\utils\Utilities.cc" /><ClCompile Include="..\..\drogon\trantor\utils\WindowsSupport.cc" /></ItemGroup>
头文件
项目属性---c/c++---常规---附加包含目录
地址根据自己存放源代码目录为准!!!!!!
..\..\drogon\trantor;..\..\drogon;..\..\drogon\third_party\wepoll;..\..\drogon\trantor\utils;..\..\drogon\trantor\net;..\..\drogon\trantor\net\inner;%(AdditionalIncludeDirectories)
预处理器
%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;USE_OPENSSL;TRANTOR_TLS_PROVIDER=OpenSSL;CMAKE_INTDIR="Release";HAS_YAML_CPP
代码生成--运行库 /MT
所有选项--附加选项
%(AdditionalOptions) /external:I "..\..\..\3rdsdk\openssl\include" /utf-8 /Zc:__cplusplus
- drogon_ctl
源文件
<ItemGroup><ClCompile Include="..\..\drogon\drogon_ctl\cmake.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\cmd.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\config_json.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\config_yaml.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\create.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\create_controller.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\create_filter.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\create_model.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\create_plugin.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\create_project.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\create_view.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\demoMain.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\filter_cc.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\filter_h.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\gitignore.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\help.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\main.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\model_cc.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\model_h.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\model_json.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\plugin_cc.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\plugin_h.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\press.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\restful_controller_base_cc.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\restful_controller_base_h.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\restful_controller_cc.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\restful_controller_custom_cc.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\restful_controller_custom_h.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\restful_controller_h.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\test_cmake.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\test_main.cc" /><ClCompile Include="..\..\drogon\drogon_ctl\version.cc" /></ItemGroup>
头文件
项目属性---c/c++---常规---附加包含目录
地址根据自己存放源代码目录为准!!!!!!
..\..\drogon\third_party\mman-win32;..\..\drogon\nosql_lib\redis\inc;..\..\drogon\orm_lib\inc;..\..\drogon\trantor;..\..\drogon\lib\inc;..\..\drogon\lib\src;..\..\drogon;..\..\drogon\trantor\utils;..\..\drogon\trantor\net;..\..\drogon\trantor\net\inner;..\..\drogon\orm_lib\src\mysql_impl;%(AdditionalIncludeDirectories)
预处理器
%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;USE_BROTLI;DBUG_OFF;USE_REDIS;CMAKE_INTDIR="Release";HAS_YAML_CPP
代码生成--运行库 /MT
附加选项
%(AdditionalOptions) /external:I "..\..\..\3rdsdk\jsoncpp\include" /external:I "..\..\..\3rdsdk\mariadb\include\mysql" /utf-8 /Zc:__cplusplus
连接器--附加库目录
$(outputpath);%(AdditionalLibraryDirectories)
输入
ws2_32.lib;Rpcrt4.lib;iphlpapi.lib;Crypt32.lib;Secur32.lib;shlwapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;advapi32.lib;drogon.lib;trantor.lib;..\..\..\3rdsdk\openssl\lib\libcrypto.lib;..\..\..\3rdsdk\openssl\lib\libssl.lib;..\..\..\3rdsdk\zlib\lib\zlib.lib;..\..\..\3rdsdk\hiredis\lib\hiredis.lib;..\..\..\3rdsdk\sqlite3\lib\sqlite3.lib;..\..\..\3rdsdk\postgresql\lib\libpq.lib;..\..\..\3rdsdk\brotli\lib\brotlicommon.lib;..\..\..\3rdsdk\brotli\lib\brotlidec.lib;..\..\..\3rdsdk\brotli\lib\brotlienc.lib;..\..\..\3rdsdk\jsoncpp\lib\jsoncpp.lib;..\..\..\3rdsdk\mariadb\lib\mariadbclient.lib;..\..\..\3rdsdk\mariadb\lib\mysqlservices.lib;%(AdditionalDependencies)
忽略待定默认库
%(IgnoreSpecificDefaultLibraries)
OK大功告成Enjoy!!!
备注:使用conan管理依赖库,想要/MT版本的在设置前添加 compiler.runtime=static即可。
conan install .. -s compiler="msvc" -s compiler.version=193 -s compiler.cppstd=17 -s build_type=Release --output-folder . --build=missing -s compiler.runtime=static
这篇关于Drogon Win11 编译 /MT的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!