Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit ae531d2

Browse files
committed
更新说明文档;
支持 find_package 引入本项目;
1 parent ffd779d commit ae531d2

11 files changed

+140
-15
lines changed

CMakeLists.txt

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
# CMakeList.txt: 顶层 CMake 项目文件,在此处执行全局配置
2-
# 并包含子项目。
3-
#
41
cmake_minimum_required (VERSION 3.1)
52

6-
project (mirai-cpp)
3+
project (mirai-cpp VERSION 0.4.4)
74

85
set(CMAKE_CXX_STANDARD 11)
96

@@ -43,7 +40,10 @@ endif()
4340
aux_source_directory(./src SRCS)
4441

4542
add_library(${PROJECT_NAME} ${LIBRARY_TYPE} ${SRCS})
46-
target_include_directories(${PROJECT_NAME} PUBLIC include)
43+
target_include_directories(${PROJECT_NAME} PUBLIC
44+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
45+
$<INSTALL_INTERFACE:include>
46+
)
4747
target_link_libraries(${PROJECT_NAME} nlohmann_json nlohmann_json::nlohmann_json)
4848
target_link_libraries(${PROJECT_NAME} CURL::libcurl)
4949
#target_link_libraries(${PROJECT_NAME} INTERFACE ${BOOST_LIBRARIES})
@@ -67,16 +67,37 @@ endif(MIRAI_CPP_BUILD_EXAMPLES)
6767
option(MIRAI_CPP_INSTALL "INSTALL_MIRAI_CPP" ON)
6868
if(MIRAI_CPP_INSTALL)
6969

70+
include(CMakePackageConfigHelpers)
71+
write_basic_package_version_file(
72+
mirai-cppConfigVersion.cmake
73+
VERSION ${PACKAGE_VERSION}
74+
COMPATIBILITY AnyNewerVersion
75+
)
76+
7077
install(
7178
DIRECTORY include
7279
DESTINATION ${CMAKE_INSTALL_PREFIX}
7380
)
7481

7582
install(
7683
TARGETS ${PROJECT_NAME}
84+
EXPORT ${PROJECT_NAME}Targets
7785
RUNTIME DESTINATION bin
7886
ARCHIVE DESTINATION lib
7987
LIBRARY DESTINATION lib
8088
)
8189

90+
install(
91+
EXPORT ${PROJECT_NAME}Targets
92+
FILE mirai-cppTargets.cmake
93+
# NAMESPACE MIRAICPP::
94+
DESTINATION lib/cmake/${PROJECT_NAME}
95+
)
96+
97+
configure_file(mirai-cppConfig.cmake.in mirai-cppConfig.cmake @ONLY)
98+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/mirai-cppConfig.cmake"
99+
"${CMAKE_CURRENT_BINARY_DIR}/mirai-cppConfigVersion.cmake"
100+
DESTINATION lib/cmake/mirai-cpp
101+
)
102+
82103
endif(MIRAI_CPP_INSTALL)

README.md

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,22 @@ cd vcpkg
4848
.\vcpkg integrate install
4949
```
5050

51+
3. 如果你使用的中文 Visual Studio,因为没有安装英文语言包,vcpkg 可能会无法正常工作。请打开 Visual Studio Installer 安装英文语言包(只需要安装英文语言包,不需要把 Visual Studio 切换为英文)
52+
53+
![通过 Visual Studio Install 安装英文语言包](./doc/pic/install_vs_eng_pkg.png)
54+
55+
5156
#### (2) 使用 **vcpkg** 安装 **mirai-cpp**
5257

5358
这一步稍微复杂,你需要执行(这里一定要在 **Powershell** 里面执行):
5459

5560
```powershell
56-
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; rm -Recurse -Force ports/mirai-cpp ; mv tmp/* ports/ ; rm -Recurse -Force tmp
57-
./vcpkg install mirai-cpp mirai-cpp:x64-windows
61+
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; mv tmp/* ports/ ; rm -Recurse -Force tmp
62+
63+
./vcpkg install mirai-cpp
64+
65+
# 对于 x64 的项目,还需要执行:
66+
#./vcpkg mirai-cpp:x64-windows
5867
```
5968

6069
耐心等待,上面的指令会帮你安装 mirai-cpp 以及它的依赖项目。
@@ -71,14 +80,12 @@ int main()
7180
{
7281
using namespace std;
7382
using namespace Cyan;
83+
system("chcp 65001");
7484
MiraiBot bot("127.0.0.1",8080);
7585
while (true)
7686
{
7787
try
7888
{
79-
// InitKeyVl0CEUzZ 改为你的 InitKey,
80-
// 2110000000 改为你的 bot 的 QQ 号码
81-
// 提示: mirai-cpp 中,数字后面加上 _qq 表示 qq 号码,数字后面加上 _gid 表示群号码。
8289
bot.Auth("InitKeyVl0CEUzZ", 2110000000_qq);
8390
break;
8491
}
@@ -87,21 +94,20 @@ int main()
8794
cout << ex.what() << endl;
8895
}
8996
}
90-
cout << "成功登录 bot。" << endl;
91-
97+
cout << "Bot Working..." << endl;
9298

9399
bot.On<FriendMessage>(
94100
[&](FriendMessage m)
95101
{
96-
// bot.SendFriendMessage(fm.Sender.QQ, fm.MessageChain);
102+
// bot.SendMessage(fm.Sender.QQ, fm.MessageChain);
97103
m.Reply(m.MessageChain);
98104
});
99105

100106
bot.On<GroupMessage>(
101107
[&](GroupMessage m)
102108
{
103-
// bot.SendGroupMessage(gm.Sender.Group.GID, "为什么要 " + gm.MessageChain);
104-
m.QuoteReply("为什么要 " + m.MessageChain);
109+
// bot.SendMessage(gm.Sender.Group.GID, "What is " + gm.MessageChain);
110+
m.QuoteReply("What is " + m.MessageChain);
105111
});
106112

107113
bot.EventLoop();
@@ -130,6 +136,9 @@ MSVC 并没有默认启动对 UTF-8 编码的支持。
130136

131137
如果一切正常,给你的机器人发消息,他会回复同样的消息给你!
132138

139+
> 提示: 添加了编译参数 **/utf-8** 后,**你的项目****源文件的编码**也必须是 **UTF-8** 编码,不然会出现编译错误。中文操作系统下,Visual Studio 创建的源文件默认为 **GB2312** 编码。所以你需要手动地修改**你的项目**的源文件为 **UTF-8** 编码。
140+
141+
133142
</details>
134143

135144

@@ -154,6 +163,11 @@ cd vcpkg
154163
.\vcpkg integrate install
155164
```
156165

166+
3. 如果你使用的中文 Visual Studio,因为没有安装英文语言包,vcpkg 可能会无法正常工作。请打开 Visual Studio Installer 安装英文语言包(只需要安装英文语言包,不需要把 Visual Studio 切换为英文)
167+
168+
![通过 Visual Studio Install 安装英文语言包](./doc/pic/install_vs_eng_pkg.png)
169+
170+
157171
#### (2) 使用 **vcpkg** 安装 **mirai-cpp** 的依赖项目
158172

159173
```powershell
@@ -194,17 +208,94 @@ git clone https://github.com/cyanray/mirai-cpp.git
194208

195209
在更新 mirai-cpp 之前,需要先删除已经安装的 mirai-cpp
196210

211+
Windows:
212+
197213
```powershell
198214
./vcpkg remove mirai-cpp mirai-cpp:x64-windows
199215
```
200216

217+
Linux:
218+
219+
```bash
220+
./vcpkg remove mirai-cpp
221+
```
222+
201223
删除之后,重新安装即可:
202224

225+
Windows:
226+
203227
```powershell
204228
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; rm -Recurse -Force ports/mirai-cpp ; mv tmp/* ports/ ; rm -Recurse -Force tmp
229+
205230
./vcpkg install mirai-cpp mirai-cpp:x64-windows
206231
```
207232

233+
Linux:
234+
```bash
235+
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; rm -rf ports/mirai-cpp ; mv tmp/* ports/ ; rm -rf tmp
236+
./vcpkg install mirai-cpp
237+
```
238+
239+
#### (2) 将程序轻松移植、部署到 Linux 上
240+
241+
<details>
242+
243+
(以下内容基于 “快速尝试2”,请先完成“快速尝试2”。)
244+
245+
上面的内容介绍了如何在 Windows 上开发使用 mirai-cpp 的程序,下面来介绍如何将你的程序移植到 Linux 平台,以便将程序部署到 Linux 服务器上。
246+
247+
为了易于讲解与操作,以下内容在 **WSL** (**W**indows **S**ubsystem for **L**inux) 上进行。这里不对如何安装 WSL 进行说明,关于如何安装 WSL 还请自行查阅资料。
248+
249+
1. 在 WSL 上安装 vcpkg
250+
251+
步骤与上文类似,但是略有不同:
252+
253+
```bash
254+
git clone https://github.com/Microsoft/vcpkg.git
255+
cd vcpkg
256+
./bootstrap-vcpkg.sh
257+
```
258+
259+
![在 WSL 上安装 vcpkg](./doc/pic/install_vcpkg_on_linux.png)
260+
261+
耐心等待,其结果如图所示。
262+
注意,如果执行 ls 发现没有生成 vcpkg 的可执行文件,请尝试重新执行上述的安装指令。(这似乎是个bug)
263+
264+
![在 WSL 上安装 vcpkg2](./doc/pic/install_vcpkg_on_linux2.png)
265+
266+
2. 使用 vcpkg 安装 mirai-cpp
267+
268+
269+
```bash
270+
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; mv tmp/* ports/ ; rm -rf tmp
271+
272+
./vcpkg install mirai-cpp
273+
```
274+
275+
![在 WSL 上安装 mirai-cpp](./doc/pic/install_mirai_cpp_on_linux.png)
276+
277+
3. 创建针对 WSL 的配置
278+
279+
打开在 “快速尝试2” 中用到的项目。按照如图所示步骤,创建一个针对 WSL 平台的配置。因为我的 WSL 安装了 GCC 编译器,所以这里选择 **WSL-GCC-Releas**
280+
281+
![创建WSL-GCC平台配置1](./doc/pic/vs_configure_linux_project.png)
282+
283+
和 “快速尝试2” 一样的操作,配置 CMake Toolchain File。但是这次的路径要填写 WSL 上的路径。
284+
285+
![创建WSL-GCC平台配置2](./doc/pic/vs_configure_linux_project2.png)
286+
287+
Enjoy it;
288+
289+
</details>
290+
291+
## 常见问题
292+
293+
### 1. 使用 vcpkg 安装 mirai-cpp 时出错
294+
295+
首先检查是不是网络错误导致下载文件失败。这种错误只要重新执行 `./vcpkg install mirai-cpp` 。如果你有可以加速网络的代理服务器,可以添加 **HTTP_PROXY****HTTPS_PROXY** 环境变量,以加速 **vcpkg** 的下载。
296+
297+
![配置环境变量](./doc/pic/env.png)
298+
208299
## 代码风格
209300

210301
本项目的代码使用的是我自己喜欢的代码风格,如果你有更好的建议(比如修改为 Google-Style),欢迎提出 issues 或 pull request。

doc/pic/env.png

7.99 KB
Loading
554 KB
Loading

doc/pic/install_vcpkg_on_linux.png

573 KB
Loading
945 KB
Loading

doc/pic/install_vs_eng_pkg.png

58.9 KB
Loading
63.1 KB
Loading
48.3 KB
Loading
44.3 KB
Loading

0 commit comments

Comments
 (0)