@@ -7,8 +7,8 @@ DolphinDB C++ API支持以下开发环境:
77* Windows GNU(MinGW)
88
99本教程介绍以下内容:
10- - [ 1. 编译libDolphinDBAPI ] ( #1-编译libdolphindbapi )
11- - [ 2. 项目编译 ] ( #2-项目编译 )
10+ - [ 1. 编译 API 动态库 ] (#1 -编译 API 动态库 )
11+ - [ 2. 编译可执行文件 ] ( #2-编译可执行文件 )
1212- [ 3. 建立DolphinDB连接] ( #3-建立dolphindb连接 )
1313- [ 4. 运行DolphinDB脚本] ( #4-运行dolphindb脚本 )
1414- [ 5. 运行DolphinDB函数] ( #5-运行dolphindb函数 )
@@ -18,185 +18,181 @@ DolphinDB C++ API支持以下开发环境:
1818- [ 9. C++ Streaming API] ( #9-c-streaming-api )
1919- [ 10. openssl 1.0.2版本源码安装] ( #10-openssl-102版本源码安装 )
2020
21+ ## 1. 编译 API 动态库
2122
23+ 本小节讲解如何在 Windows 环境下使用 Visual Studio 2022、在 Linux 环境(以 RedHat 为例),以及在 Windows 环境下使用 MinGW 编译出 API 动态库。
2224
23- ## 1. 编译libDolphinDBAPI
25+ ### 1.1 Windows 环境下使用 Visual Studio 2022编译
2426
25- 用户可以使用bin目录下已编译的libDolphinDBAPI, 也可以通过如下方法自己编译libDolphinDBAPI。
27+ #### 环境准备
2628
27- ### 1.1 在Linux环境下编译API
29+ 1 . 安装软件 [ Visual Studio 2022] ( https://visualstudio.microsoft.com/zh-hans/downloads/ ) ,[ Git] ( https://git-scm.com/downloads ) ,[ CMake] ( https://cmake.org/download/ ) 。
30+ 2 . 下载 [ OpenSSL 库] ( https://www.npcglib.org/~stathis/blog/precompiled-openssl ) 。本文中的下载目录是 ` D:/temp/openssl-1.0.2l-vs2017 ` 。
2831
29- #### 编译libuuid
32+ #### 代码准备
3033
31- DolphinDB API会用到libuuid,所以要先编译libuuid的静态库。编译方法如下:
34+ 在 Gitee 的 [ api-cplusplus ] ( https://gitee.com/dolphindb/api-cplusplus ) 项目中下载代码,并切换至预期分支。以 main 分支为例,在 Git Bash 中执行如下命令:
3235
33- * 下载 [ libuuid-1.0.3.tar.gz] ( https://sourceforge.net/projects/libuuid/files/ )
34-
35- * 解压:tar -xvf libuuid-1.0.3.tar.gz
36-
37- * cd libuuid-1.0.3 && ./configure CFLAGS="-fPIC" CPPFLAGS="-fPIC" && make
38-
39- * 如果编译成功, libuuid.a 会生成在目录 '.libs'下
40-
41- * 将libuuid.a拷贝到目录DolphinDBAPI
42-
43- #### 编译libDolphinDBAPI
44-
45- 编译命令:
46-
47- ```
48- cd api-cplusplus
49- mkdir build && cd build
50- cmake .. && make
36+ ```
37+ git clone git@gitee.com:dolphindb/api-cplusplus.git
38+ git checkout -b main origin/main
5139```
5240
53- 如果编译成功,会自动生成libDolphinDBAPI.so
54-
55-
56- ### 1.2 在Windows环境下用MinGW编译API
41+ #### 编译
5742
58- 编译命令 :
43+ 在 PowerShell 中执行如下命令,即可成功编译出 DolphinDBAPI.dll 和 DolphinDBAPI.lib :
5944
6045```
61- cd api-cplusplus
62- mingw32-make -f makefile.win32
46+ cd path/to/api-cplusplus
47+ mkdir build && cd build
48+ cmake .. -G "Visual Studio 17 2022" -A x64 -DUSE_OPENSSL=1 -DOPENSSL_PATH=D:/temp/openssl-1.0.2l-vs2017 -DCMAKE_CONFIGURATION_TYPES="Release;Debug"
49+ cmake --build . --config Release
6350```
6451
52+ ` cmake ` 参数说明:
6553
66- ### 1.3 在Windows环境下,用Visual Studio 2017编译API
67-
54+ - -G:用来指定编译器。如果安装为其它版本的 Visual Studio,则此处须对应修改。
55+ - -A:x64指定生成64位的动态库,x86指定生成32位的动态库。
56+ - -DUSE_OPENSSL=1:如果想使用 OpenSSL 来加密 API 和 DolphinDB 之间的通信,则必须指定该参数。指定后编译 API 动态库时会去链接 SSL 以及 Crypto 动态库。
57+ - -DOPENSSL_PATH=/path/to/openssl:如果系统中没有安装 OpenSSL,则可以通过该参数来指定路径。
58+ - -DCMAKE_CONFIGURATION_TYPES="Release;Debug":生成的 Visual Studio 项目文件支持编译 Release 或 Debug 类型的动态库。
59+ - --config Release:生成 Release 类型的目标文件,也可以改为 Debug。
6860
61+ ** 注意事项**
6962
70- #### 创建项目 libDolphinDBAPI
63+ 如果使用的是从上述网址下载的 OpenSSL 库,并且想编译64位的动态库,那么需要在执行 ` cmake ` 命令之前,删除该下载的 OpenSSL 库目录中的 bin,include,以及 lib 文件夹,并将 bin64,include64以及 lib64文件夹重命名为 bin、include 以及 lib,因为 CMake 会在这些目录中寻找头文件以及库。
7164
72- Windows Desktop->Dynamic Link Library (DLL)
65+ ### 1.2 Linux 环境下编译(以 RedHat 为例)
7366
74- #### 配置属性
67+ #### 环境准备
7568
76- 配置属性 -> 常规 -> 项目默认值 -> 配置类型 -> 动态库(.dll)
69+ 1 . 安装 [ gcc/g++] ( https://gcc.gnu.org/ ) (version >= 4.8.5),Git,CMake。
70+ 2 . 安装 [ Openssl-devel] ( https://pkgs.org/download/openssl-devel ) 以及 [ Uuid-devel] ( https://pkgs.org/download/uuid-devel ) 。
7771
78- #### 下载并将 [ Openssl] ( https://www.npcglib.org/~stathis/blog/precompiled-openssl/ ) 加入include和lib路径:
72+ ```
73+ yum install openssl-devel
74+ yum install libuuid-devel
75+ ```
7976
77+ #### 代码准备
8078
81- 1 . 配置属性页->VC++ 目录 -> 包含目录 ->C:\openssl-1.0.2l-vs2017\include64;
82- 2 . 配置属性页->VC++ 目录 -> 库目录 -> C:\openssl-1.0.2l-vs2017\lib64;
79+ 在 Gitee 的 [ api-cplusplus] ( https://gitee.com/dolphindb/api-cplusplus ) 项目中下载代码,并切换至预期分支。以 main 分支为例,在终端中执行如下命令:
8380
84- #### 添加下面的宏定义
81+ ``` linux
82+ git clone git@gitee.com:dolphindb/api-cplusplus.git
83+ git checkout -b main origin/main
84+ ```
8585
86- C/C++ -> 预处理器 -> 预处理器定义 ->WIN32_LEAN_AND_MEAN; _ WINSOCK_DEPRECATED_NO_WARNINGS; _ CRT_SECURE_NO_WARNINGS;WINDOWS;NOMINMAX;NDEBUG; _ WINDOWS; _ DDBAPIDLL;
86+ #### 编译
8787
88+ 在终端中执行如下命令,即可成功编译出 libDolphinDBAPI.so:
8889
90+ ```
91+ cd path/to/api-cplusplus
92+ mkdir build && cd build
93+ cmake .. -DABI=0 -DUUID_PATH=/home/uuid/ -DUSE_OPENSSL=1 -DOPENSSL_PATH=/home/openssl/
94+ make -j4
95+ ```
8996
90- #### 预编译选项(Precompiled header)
91- C/C++ -> 预编译头 -> 预编译头-> 不使用预编译头
97+ ` cmake ` 参数说明:
9298
99+ - -DABI=0:用来指定编译时` _GLIBCXX_USE_CXX11_ABI ` 的值,可以设置为1或者0。
100+ - -DUUID_PATH=/path/to/uuid:如果系统中没有安装 uuid,则可以通过该参数来指定 UUID 路径,编译时会在\$ {UUID_PATH}/include目录下寻找头文件,在\$ {UUID_PATH}/lib目录下寻找 uuid 库
101+ - -DUSE_OPENSSL=1:如果想使用 OpenSSL 来加密 API 和 DolphinDB 之间的通信,则必须指定该参数。指定后编译 API 动态库时会去链接 SSL 以及 Crypto 动态库。
102+ - -DOPENSSL_PATH=/path/to/openssl:如果系统中没有安装 OpenSSL,则可以通过该参数来指定 OPENSSL 路径。
93103
94- #### 链接(Linker):
104+ ### 1.3 Windows 环境下使用 MinGW 编译
95105
96- 连接器 -> 输入 -> 附加依赖项
97- ws2_32.lib
98- ssleay32MD.lib
99- libeay32MD.lib
106+ #### 环境准备
100107
101- #### 添加源码:
108+ 1 . 安装软件 [ MinGW] ( https://www.mingw-w64.org/downloads/ ) ,Git,CMake。
109+ 2 . OpenSSL 库使用的是 API 源码中 lib 目录下的 openssl-1.0.2u。
102110
103- 移除项目中代码,并添加src目录的源码到项目中。
111+ #### 代码准备
104112
105- #### 编译
113+ 在 Gitee 的 [ api-cplusplus ] ( https://gitee.com/dolphindb/api-cplusplus ) 项目中下载代码,并切换至预期分支。以main分支为例,在 Git Bash 中执行如下命令:
106114
107- 编译的时候选择release和x64.如果编译成功,在/username/source/repos/libDolphinDBPAI/x64/Release目录下会生成:libDolphinDBAPI.lib和libDolphinDBAPI.dll。
115+ ```
116+ git clone git@gitee.com:dolphindb/api-cplusplus.git
117+ git checkout -b main origin/main
118+ ```
108119
109- > 更详细的介绍请参阅 [ 用VS2017编译DolphinDB C++ API动态库 ] ( https://github.com/dolphindb/Tutorials_CN/blob/master/cpp_api_vs2017_tutorial.md )
120+ #### 编译
110121
111- ## 2. 项目编译
122+ 在 Power Shell 中执行如下命令,即可成功编译出 libDolphinDBAPI.dll:
112123
113- ### 2.1 在Linux环境下编译项目
124+ ```
125+ cd path/to/api-cplusplus
126+ mkdir build && cd build
127+ cmake .. -G "MinGW Makefiles" -DUSE_OPENSSL=1 -DOPENSSL_PATH=D:/workspace/testCode/api-cplusplus/lib/openssl-1.0.2u/openssl-1.0.2u/static
128+ cmake --build .
129+ ```
114130
115- #### 2.1.1 环境配置
131+ ` cmake ` 参数说明:
116132
117- C++ API需要使用g++ 4.8.5及以上版本。
133+ - -G:用来指定编译器。
134+ - -DUSE_OPENSSL=1:如果想使用 OpenSSL 来加密 API 和 DolphinDB 之间的通信,则必须指定该参数。指定后编译 API 动态库时会去链接 SSL 以及 Crypto 动态库。
135+ - -DOPENSSL_PATH=/path/to/openssl:如果系统中没有安装 OpenSSL,则可以通过该参数来指定路径。
118136
119- #### 2.1.2 下载bin文件和头文件
137+ ## 2. 编译可执行文件
120138
121- 从本GitHub项目中下载以下文件:
139+ 本小节主要讲解如何在 Windows 环境下使用 Visual Studio 2022、在 Linux 环境(以 RedHat 为例),以及在 Windows 环境下使用 MinGW 编译一个可执行文件,并链接上一对应小节生成的 API 动态库以连接数据库。
122140
123- - [ bin] ( ./bin ) (libDolphinDBAPI.so)
124- - [ include] ( ./include ) (DolphinDB.h, Exceptions.h, SmartPointer.h, SysIO.h, Types.h, Util.h)
141+ 注:这部分的工程存在于 API 源码中的 demo 文件夹内。
125142
126- #### 2.1.3 编译main.cpp
143+ ### 2.1 Windows 环境下使用 Visual Studio 2022编译
127144
128- 在bin和include的同级目录中创建project目录。进入project目录,并创建文件main.cpp:
145+ 1 . 将1.1节生成的 DolphinDBAPI.lib 拷贝到 ` demo/lib ` 目录下。
146+ 2 . 在 PowerShell 中执行下述命令:
129147
130- ``` cpp
131- #include " DolphinDB.h"
132- #include " Util.h"
133- #include < iostream>
134- #include < string>
135- using namespace dolphindb ;
136- using namespace std ;
137-
138- int main (int argc, char * argv[ ] ){
139-
140- DBConnection conn;
141- bool ret = conn.connect("111.222.3.44", 8503);
142- if(!ret){
143- cout<<"Failed to connect to the server"<<endl;
144- return 0;
145- }
146- ConstantSP vector = conn.run("`IBM`GOOG`YHOO");
147- int size = vector->rows();
148- for(int i=0; i<size; ++i)
149- cout<<vector->getString(i)<<endl;
150- return 0;
151- }
148+ ```
149+ cd path/to/api-cplusplus/demo
150+ mkdir build && cd build
151+ cmake .. -G "Visual Studio 17 2022" -A x64 -DUSE_OPENSSL=1 -DOPENSSL_PATH=D:/temp/openssl-1.0.2l-vs2017 -DCMAKE_CONFIGURATION_TYPES="Release;Debug"
152+ cmake --build . --config Release
152153```
153154
154- #### 2.1.4 编译
155-
156- 为了兼容旧的编译器,libDolphinDBAPI.so提供了2个版本,一个版本在编译时使用了-D_GLIBCXX_USE_CXX11_ABI=0的选项,放在[bin/linux_x64/ABI0](./bin/linux_x64/ABI0)目录下,另一个版本未使用-D_GLIBCXX_USE_CXX11_ABI=0,放在[bin/linux_x64/ABI1](./bin/linux_x64/ABI1)目录下。
155+ ` cmake ` 各项参数含义与1.1 编译中的相同。
157156
158- 另外由于DolphinDB添加了(Linux64 稳定版>=1.10.17,最新版>=1.20.6) SSL的支持, 所以编译前需要安装openssl。
157+ 1 . 生成的可执行文件 apiDemo.exe 在 ` demo\bin\Debug ` 目录下。
158+ 2 . 将所依赖的动态库(DolphinDBAPI.dll libeay32MD.dll ssleay32MD.dll)拷贝到 .exe 文件同目录下即可执行。
159159
160- >注:当前需要openssl版本为1.0.2,高版本的1.1版本会报错。如果系统自带的openssl版本不是1.0.2的话,可以参考[openssl源码安装](#10-openssl-102版本源码安装),或者使用已有的二进制包安装。查看openssl版本的命令为:```openssl version```
160+ ### 2.2 Linux 环境下编译(以 RedHat 为例)
161161
162- 以下是使用第一个动态库版本的g++编译命令:
163- ```
164- g++ main.cpp -std=c++11 -DLINUX -D_GLIBCXX_USE_CXX11_ABI=0 -DLOGGING_LEVEL_2 -O2 -I../include -lDolphinDBAPI -lpthread -lssl -lrt -L../bin/linux_x64/ABI0 -Wl,-rpath,.:../bin/linux_x64/ABI0 -o main
165- ```
162+ 1 . 将1.2节生成的 libDolphinDBAPI.so 拷贝到 demo/lib 目录下.
163+ 2 . 在终端中执行下述命令:
166164
167- 以下是使用另一个动态库版本的g++编译命令:
168165```
169- g++ main.cpp -std=c++11 -DLINUX -D_GLIBCXX_USE_CXX11_ABI=1 -DLOGGING_LEVEL_2 -O2 -I../include -lDolphinDBAPI -lpthread -lssl -lrt -L../bin/linux_x64/ABI1 -Wl,-rpath,.:../bin/linux_x64/ABI1 -o main@
166+ cd path/to/api-cplusplus/demo
167+ mkdir build && cd build
168+ cmake .. -DABI=0 -DUSE_OPENSSL=1 -DOPENSSL_PATH=/home/openssl/
169+ cmake --build .
170170```
171171
172- #### 2.1.5 运行
173-
174- 编译成功后,启动DolphinDB,运行main程序并连接到DolphinDB,连接时需要指定IP地址和端口号,如以上程序中的111.222.3.44:8503。
175-
176- ### 2.2 Windows环境下编译
177- 本节将简要介绍在windows上如何使用C++ API,更详细的介绍请参阅[用VS2017编译DolphinDB C++ API动态库](https://github.com/dolphindb/Tutorials_CN/blob/master/cpp_api_vs2017_tutorial.md#4-%E6%A1%88%E4%BE%8B%E9%AA%8C%E8%AF%81)第4节。
172+ ` cmake ` 各项参数说明如下所示,注意 * ABI* 的值必须相同:
178173
179- #### 2.2.1 环境配置
174+ - -DABI=0:用来指定编译时 _ GLIBCXX_USE_CXX11_ABI 的值,可以设置为1或者0。
175+ - -DUSE_OPENSSL=1:如果想使用 OpenSSL 来加密 API 和 DolphinDB 之间的通信,则必须指定该参数。指定后编译 API 动态库时会去链接 SSL 以及 Crypto 动态库。
176+ - -DOPENSSL_PATH=/path/to/openssl:如果系统中没有安装 OpenSSL,则可以通过该参数来指定路径。
180177
181- 本教程使用了Visual Studio 2017 64位版本 。
178+ 1 . 生成的可执行文件 apiDemo 在 ` demo\bin ` 目录下 。
182179
183- #### 2.2.2 下载bin文件和头文件
180+ ### 2.3 Windows 环境下使用 MinGW 编译
184181
185- 将本GitHub项目下载到本地。
182+ 1 . 将1.3节生成的 libDolphinDBAPI.dll 拷贝到 ` demo/lib ` 目录下。
183+ 2 . 在 PowerShell 中执行下述命令:
186184
187- #### 2.2.3 创建Visual Studio项目
188-
189- 创建windows console project,导入[include](./include)目录下头文件,创建1.1.3节中的main.cpp文件,导入libDolphinDBAPI.lib,并且配置lib目录。
190-
191- 请注意:
192- > 由于VS里默认定义了min/max两个宏,会与头文件中 `min` 和 `max` 函数冲突。为了解决这个问题,在预处理宏定义中需要加入 __NOMINMAX__。
193- > API源代码中用宏定义LINUX、WINDOWS等区分不同平台,因此在预处理宏定义中需要加入 WINDOWS。
194-
195- #### 2.2.4 编译和运行
185+ ```
186+ cd path/to/api-cplusplus/demo
187+ mkdir build && cd build
188+ cmake .. -G "MinGW Makefiles" -DUSE_OPENSSL=1 -DOPENSSL_PATH=D:/workspace/testCode/api-cplusplus/lib/openssl-1.0.2u/openssl-1.0.2u/static
189+ cmake --build .
190+ ```
196191
197- 启动编译,将对应的libDolphinDBAPI.dll拷贝到可执行程序的输出目录,即可运行 。
192+ ` cmake ` 各项参数含义与1.3中的相同 。
198193
199- Windows gnu开发环境与Linux相似,可以参考上一章的Linux编译。
194+ 1 . 生成的可执行文件 apiDemo.exe 在 ` demo\bin ` 目录下。
195+ 2 . 将所依赖的动态库(libDolphinDBAPI.dll,libeay32.dll,ssleay32.dll)拷贝到 .exe文件同目录下即可执行。
200196
201197## 3. 建立DolphinDB连接
202198
0 commit comments