Skip to content
Permalink
Browse files
Fix CPP client could not be successfully built on windows (#4665)
  • Loading branch information
xiaoyekanren committed Dec 31, 2021
1 parent 2d6307f commit dd37e9fb7490c464dc563ad0a2d7c756e1f55f91
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 91 deletions.
@@ -33,22 +33,8 @@
<thrift.with.cpp>ON</thrift.with.cpp>
<thrift.with.csharp>ON</thrift.with.csharp>
<thrift.with.python>ON</thrift.with.python>
<chmod.command>chmod</chmod.command>
<chmod.command.para>+x cmake</chmod.command.para>
</properties>
<profiles>
<profile>
<id>os-windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<chmod.command>echo</chmod.command>
<chmod.command.para>"do nothing"</chmod.command.para>
</properties>
</profile>
<profile>
<id>compile-cpp</id>
<build>
@@ -86,25 +72,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>make-cmake-executable</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<basedir>${cmake.root.dir}/bin</basedir>
<executable>${chmod.command}</executable>
<commandlineArgs>${chmod.command.para}</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.googlecode.cmake-maven-project</groupId>
<artifactId>cmake-maven-plugin</artifactId>
@@ -117,35 +117,19 @@ The version of gcc and boost installed by yum is too low, therefore you should c

#### Build Thrift on Windows

- building environment

Make sure a complete Windows C++ building environment is prepared on your machine.
MSVC, MinGW... are supported.

If you are using MS Visual Studio, remember to install Visual Studio C/C++ IDE and compiler(supporting CMake, Clang, MinGW).

- Flex and Bison
Windows Flex and Bison could be downloaded from SourceForge: https://sourceforge.net/projects/winflexbison/

After downloaded, please rename the executables to flex.exe and bison.exe and add them to "PATH" environment variables.

- Boost
For Boost, please download from the official website: https://www.boost.org/users/download/

Then build Boost by executing bootstrap.bat and b2.exe.
```powershell
bootstrap.bat
.\b2.exe
```

To help CMake find your Boost libraries on windows, you should set `-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}`
to your mvn build command.
- CMake



#### Cmake generator on Windows
For CMake, please download from the official website: https://cmake.org/download/

There is a long list of supported Cmake generators on Windows environment.


```
Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
Use -A option to specify architecture.
@@ -181,29 +165,55 @@ the list is available via command: `cmake --help`
When building client-cpp project, use -Dcmake.generator="" option to specify a Cmake generator.
E.g., `mvn package -Dcmake.generator="Visual Studio 15 2017 [arch]"`

- Flex and Bison

Windows Flex and Bison could be downloaded from SourceForge: https://sourceforge.net/projects/winflexbison/

After downloaded, please rename the executables to flex.exe and bison.exe and add them to "PATH" environment variables.

- Boost

#### Building C++ Client
For Boost, please download from the official website: https://www.boost.org/users/download/

Then build Boost by executing bootstrap.bat and b2.exe.
```powershell
bootstrap.bat
.\b2.exe
```

To compile cpp client, add "-P compile-cpp" option to maven build command.
To help CMake find your Boost libraries on windows, you should set `-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}`
to your mvn build command.

The compiling requires the module "compile-tools" to be built first.
- openssl

For openssl , download Source code from the official website: https://www.openssl.org/source/

download binary files from http://slproweb.com/products/Win32OpenSSL.html

- Add Path

Before compile,make sure cmake,flex,bison and openssl already add to "PATH" environment variables.

#### Compile and Test

To compile cpp client, add "-P compile-cpp" option to maven build command.

The compiling requires the module "compile-tools" to be built first.

- On Mac and Linux, the command to compile cpp-client is as follows:

`mvn package -P compile-cpp -pl example/client-cpp-example -am -DskipTest`

To compile on Windows, please install Boost first and add following Maven settings:
- On Windwos , Compile cpp-client

please install Boost first and add following Maven settings before compile on Windows:
```shell
-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}`
```
Add generator for cmake:
`-Dcmake.generator="Visual Studio 15 2017 [arch]"`
e.g.,
The complete command:
```shell
mvn package -P compile-cpp -pl client-cpp,server,example/client-cpp-example -am
-D"boost.include.dir"="D:\boost_1_75_0" -D"boost.library.dir"="D:\boost_1_75_0\stage\lib" -DskipTests
@@ -97,32 +97,15 @@ sudo yum install bison flex openssl-devel

#### 在 Windows 上编译 Thrift

- 编译构建环境

保证你的 Windows 系统已经搭建好了完整的 C/C++的编译构建环境。可以是 MSVC,MinGW 等。

如使用 MS Visual Studio,在安装时需要勾选 Visual Studio C/C++ IDE and compiler(supporting CMake, Clang, MinGW)。

- Flex 和 Bison

Windows 版的 Flex 和 Bison 可以从 SourceForge 下载:https://sourceforge.net/projects/winflexbison/

下载后需要将可执行文件重命名为 flex.exe 和 bison.exe 以保证编译时能够被找到,添加可执行文件的目录到 PATH 环境变量中。

- Boost

Boost 官网下载新版本 Boost: https://www.boost.org/users/download/

依次执行 bootstrap.bat 和 b2.exe,本地编译 boost

```shell
bootstrap.bat
.\b2.exe
```

为了帮助 CMake 本地安装好的 Boost,在编译 client-cpp 的 mvn 命令中需添加:

`-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}`
- CMake

#### CMake 生成器
CMake 官网下载地址 https://cmake.org/download/

CMake 需要根据不同编译平台使用不同的生成器。CMake 支持的生成器列表如下 (`cmake --help`的结果):

@@ -158,32 +141,65 @@ CMake 需要根据不同编译平台使用不同的生成器。CMake 支持的
```

编译 client-cpp 时的 mvn 命令中添加 -Dcmake.generator="" 选项来指定使用的生成器名称。

`mvn package -Dcmake.generator="Visual Studio 15 2017 [arch]"`

#### 编译 C++ 客户端
- Flex 和 Bison

Maven 命令中添加"-P client-cpp" 选项编译 client-cpp 模块。client-cpp 需要依赖编译好的 thrift,即 compile-tools 模块。
Windows 版的 Flex 和 Bison 可以从 SourceForge 下载:https://sourceforge.net/projects/winflexbison/

#### 编译及测试
下载后需要将可执行文件重命名为 flex.exe 和 bison.exe 以保证编译时能够被找到,添加可执行文件的目录到 PATH 环境变量中。

完整的 C++客户端命令如下:
- Boost

`mvn package -P compile-cpp -pl example/client-cpp-example -am -DskipTest`
Boost 官网下载地址 https://www.boost.org/users/download/

注意在 Windows 下需提前安装好 Boost,并添加以下 Maven 编译选项:
依次执行 bootstrap.bat 和 b2.exe,本地编译 boost

```shell
-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}`
bootstrap.bat
b2.exe
```

例如:
为了帮助 CMake 本地安装好的 Boost,在编译 client-cpp 的 mvn 命令中需添加:

`-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}`

- openssl

openssl 官网源码下载地址 https://www.openssl.org/source/

二进制文件下载地址 http://slproweb.com/products/Win32OpenSSL.html

- 增加环境变量

在编译前,需要确定cmake,flex,bison,openssl都加入了PATH。

#### 编译及测试

Maven 命令中添加"-P client-cpp" 选项编译 client-cpp 模块。client-cpp 需要依赖编译好的 thrift,即 compile-tools 模块。

- Mac , Linux 下,编译C++客户端完整命令如下:

`mvn package -P compile-cpp -pl example/client-cpp-example -am -DskipTest`

- Windows下,编译C++客户端:

编译前需额外添加Boost相关的参数:

`-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}`

需指定cmake的generator,例如:

`-Dcmake.generator="Visual Studio 15 2017 [arch]"`

完整编译C++客户端命令如下:

```shell
mvn package -P compile-cpp -pl client-cpp,server,example/client-cpp-example -am
-D"boost.include.dir"="D:\boost_1_75_0" -D"boost.library.dir"="D:\boost_1_75_0\stage\lib" -DskipTests
mvn package -P compile-cpp -pl client-cpp,server,example/client-cpp-example -am -Dcmake.generator="your cmake generator" -Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder} -DskipTests
```

编译成功后,打包好的zip 文件将位于:"client-cpp/target/client-cpp-${project.version}-cpp-${os}.zip"
编译成功后,打包好的 zip 文件将位于:"client-cpp/target/client-cpp-${project.version}-cpp-${os}.zip"

解压后的目录结构如下图所示 (Mac):

0 comments on commit dd37e9f

Please sign in to comment.