Build - changnet/MServer GitHub Wiki
编译
环境
- Linux 或者 windows
- GCC (>= C++17) 或者 Visual Studio 2019
- Lua (>= 5.3)
- Mariadb(MySQL)
- MongoDB
注意
- 如果需要向下支持C++11,需要修改几处使用
std::filesystem
的地方 - shell目录中的脚本仅在Debian下测试,不同版本的Debian及其他发行版的Linux可能无法适用,需要根据实际情况手动调整
- Windows版本只用于测试,依赖库的选择不是很严谨
下载
git clone https://github.com/changnet/MServer.git
cd MServer
git submodule update --init --recursive
更新submodule
git submodule foreach git pull origin master
删除submodule
# 要删除的路径为 engine/http-parser
git submodule deinit -f -- engine/deps/http-parser
rm -rf .git/modules/engine/http-parser
# 这个section并不是路径名,具体可以看.gitmodules里的名字
git config -f .git/config --remove-section submodule.master/deps/http-parser
git config -f .gitmodules --remove-section submodule.master/deps/http-parser
安装编译环境及系统依赖(Linux)
- 编译环境 指基础编译工具,如GCC、cmake等工具
- 系统依赖 指一些基础的系统库依赖,如OpenSSL、MySQL等
执行以下脚本可全部安装相关的工具
cd MServer/shell
./build_env.sh
注意:这个工程包含特定版本的第三方库,位于package目录下。执行此脚本可能会覆盖已有版本。如果安装其他版本,不一定兼容
设置运行环境(Linux)
执行以下脚本设置程序的运行环境,比如core文件大小、最大文件句柄数量、core文件格式等待,这个设置不是必须的
cd MServer/shell
./build_env.sh set_sys_env
编译主程序(Linux)
如果上面的依赖均已正确编译,则下面的指令应该会在master/bin目录编译出来一个master二进制文件
cd MServer/shell
./build.sh
安装MariaDB(Linux)
程序运行需要依赖MariaDB,执行以下脚本会安装MariaDB,并且创建一个用于测试的数据库帐号及数据库
cd MServer/shell
./mariadb.sh install
安装MongoDB(Linux)
程序运行需要依赖MongoDB,执行以下脚本会安装MongoDB,并且创建一个用于测试的数据库帐号及数据库
cd MServer/shell
./mongodb.sh install
安装编译环境及系统依赖(Windows)
- 安装Visual Studio 2019(及以上版本)
请注意一定要安装英文语言包,下面的vcpkg需要用到。如果已经安装Visual Studio但没安装对应的语言包,重新执行Visual Studio installer
,选择修改,勾上英文语言包即可。 - 安装vcpkg,然后用vcpkg安装依赖 这里以使用win下的cmd为例子,如果使用git bash,则命令可能会有些差异,尤其是在代理设置方面。
git clone https://github.com/microsoft/vcpkg
cd vcpkg
.\bootstrap-vcpkg.bat
:: 安装完后,需要把vcpkg路径添加到系统Path,或者下面的指令直接指定vcpkg.exe路程
:: 安装依赖包
cd MServer
:: 本项目使用manifest模式,可以不指定安装目录,但默认是安装到vcpkg.json所在目录。项目只是临时使用安装目录,所以指定到其他目录
vcpkg install --x-manifest-root=.\project --x-install-root=.\engine\vcpkg_win
:: 安装完成后,不使用安装目录,而是使用导出的目录,方便迁移到未安装vcpkg的环境。导出依赖包到engine下的vcpkg_win_x64目录
vcpkg export --raw --x-manifest-root=.\project --x-install-root=.\engine\vcpkg_win --output-dir=.\engine\vcpkg_win_x64
:: 每次导出,vcpkg生成的目录是带时间的(vcpkg-export-20250514-100442),所以每次都不一样。更新vcpkg的包时,需要修改CMakeList.txt中的路径
-
linux下libmariadb的头文件在mariadb目录下,但通过vcpkg安装的却是在mysql上当下,因此需要进入刚导出的vcpkg目录,把
installed\x64-windows\include\mysql
拷贝为mariadb
-
用Visual Studio打开engine目录下的CMakeList.txt即可编译
当网络不好时,考虑使用代理。一般情况下,在设置系统http代理后,在power shell中运行的命令都会自动使用代理。如果使用Clash Verge等工具,在设置里开启系统代理即可。
如果还是不行。尝试需要手动设置,这里以sock5代理为例:
set http_proxy=socks5://127.0.0.1:10808
set https_proxy=socks5://127.0.0.1:10808
vcpkg使用的下载方式非常复杂(有时候使用powershell,有时候使用vcpkg.exe,有时候使用cmake来下载),上面设置的代理不一定会生效,这时候建议手动下载。如
https://github.com/mariadb-corporation/mariadb-connector-c/archive/7d304d26c787a3f0430624db977b615aba56e4bb.tar.gz: WinHttpSendRequest() failed: 12002
[DEBUG] Exiting after %s (%d us)
CMake Error at scripts/cmake/vcpkg_download_distfile.cmake:84 (message):
Failed to download file with error: 1
这时候在浏览器打开上面的链接https://github.com/mariadb-corporation/mariadb-connector-c/archive/7d304d26c787a3f0430624db977b615aba56e4bb.tar.gz
下载文件到vcpkg/downloads
这个目录,然后重新执行上面的vcpkg install安装命令即可。
单元测试
cd MServer/shell
./start.sh test --filter=string --skip=perf
# --filter参数是可选
运行
服务器本身并不是一个纯粹的引擎,它本身带了一个例子的(如果你是开启一个新项目,则需要删一些无用的文件)。可以用以下命令来开启测试:
cd MServer/shell
./start.sh
同时还有一个机器人进程用于测试
cd MServer/shell
./start.sh android
已知问题
- 在windows下用mintty(MYSY2、Git Bash、CygWin)运行程序会导致程序收不到ctrl + c信号,需要使用winpty解决
- mintty默认会缓存
stdout
的输出,会导致程序无输出,需要在程序里手动调用fflush(stdout)
- 执行单元测试时,程序日志会带ANSI color,win10需要手动开启该颜色支持
:: win10
:: win + r 打开cmd,执行以下指令
reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1
:: 删除
:: reg delete HKCU\Console /v VirtualTerminalLevel