APACHE THRIFT, CPPMICROSERVICES, C++, VISUAL STUDIO 2017

開發(fā)注意事項

第一版本(當(dāng)前版本)

libevent的版本比較老(1.4.15)。要測試網(wǎng)絡(luò)性能時,需要考慮這一點影響。

第二版本

源代碼編譯環(huán)境

vcpkg

Vcpkg: a tool to acquire and build C++ open source libraries on Windows]是一個新的工具,支持很多開源項目,包括libevent。

To acquire the tool:

Clone from GitHub: git clone https://github.com/Microsoft/vcpkg

Run the bootstrap script in the vcpkg folder: powershell -exec bypass scripts\bootstrap.ps1

Now vcpkg.exe is in the root of the repository

You are now all set (no installation, no registry… the tool and the associated “l(fā)ib folder” are xcopy-able)

Available triplets
arm-uwp.cmake
x64-uwp.cmake
x64-windows-static.cmake
x64-windows.cmake
x86-uwp.cmake
x86-windows-static.cmake
x86-windows.cmake

Use Vcpkg with your projects

The easiest way to use Vcpkg from Visual Studio is through the user-wide integration, making the system available to all VS projects you build.
vcpkg integrate install
也就是說這個命令執(zhí)行過之后,所有的新建VC項目都可以直接使用通過vcpkg.exe安裝(vcpkg install xxx)的第三方庫。
This will require administrator access the first time it is used on a given machine. After the first use, administrator access is no longer required and the integration is on a per-user basis.
In addition to the VS integration, Vcpkg also supports CMake and other build systems. For more information, see docs/EXAMPLES.md.

<pre>
D:\dev3rd\vcpkg>vcpkg integrate install
Applied user-wide integration for this vcpkg root. All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available.

CMake projects should use -DCMAKE_TOOLCHAIN_FILE=D:/dev3rd/vcpkg/scripts/buildsystems/vcpkg.cmake
</pre>

另外的建議

Egor Pugin

You should definitely check C++ Archive Network https://cppan.org/It has almost all of vcpkg ports while keeping no-brainer crossplatform/crosscompile/different VS versions support.

vkpkg當(dāng)前不支持的庫

cppmicroservices

使用cmake編譯。VS2017內(nèi)建支持cmake,但是目前用起來還是很不方便。
所以還是用了cmake-gui來進(jìn)行。
https://stackoverflow.com/questions/12407010/how-to-change-the-value-of-cmake-install-prefix-with-cmake-in-windows-developmen
For Windows you can use
cmake-gui [path to CMakeLists.txt]
In gui
Click Configure

edit CMAKE_INSTALL_PREFIX to whatever you want and then
Click Generate

說明

US_BUILD_SHARED_LIBS是Windows下cppmicroservices使用的宏。

需要自定義后期生成事件。其中可能需要一個res_0.zip,因為沒有使用CMake,所以需要自己制作一個放在project目錄。


image.png

<pre>
setlocal
cd $(RONGYI_DEV_ROOT)QuickO32Backend$(ProjectName)
if %errorlevel% neq 0 goto :cmEnd
D:
if %errorlevel% neq 0 goto :cmEnd
$(RONGYI_DEV_ROOT)CppMicroServices\build\bin$(Configuration)\usResourceCompiler3.exe -b $(RONGYI_DEV_ROOT)CppMicroServices/build/bin/$(Configuration)/$(ProjectName).dll -z $(RONGYI_DEV_ROOT)projects/QuickO32Backend/$(ProjectName)/res_0.zip
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
</pre>
所以建議以后還是直接使用CMake,將項目納入到Cppmicroservices的CMake系統(tǒng)之中,就可以避免這些問題(不過我沒有試過,不知道是否麻煩)

不使用vcpkg

這篇文章參考《APACHE THRIFT FOR C++ ON VISUAL STUDIO 2015》。

Today we are going to see how to build Apache Thrift for C++ on Visual Studio 2017. Then for demonstration, we’ll also build and run the C++ tutorial.
Disclaimer: this tutorial builds hugely on the one given by Adil Bukhari Configuring Apache Thrift for Visual Studio 2012. The reason I create a new one is that I followed his steps but stumbled upon a few problems preventing me from continuing it. Therefore, I find it quite helpful –for future learners– to complement that tutorial with the solutions to these problems.
Testing environment
Windows 10 64bit.
Microsoft Visual Studio 2015 (also tested with Visual Studio 2013).
Apache Thrift 0.9.2.
Boost 1.59.0.
Libevent 2.0.22.
OpenSSL 1.0.2d.
Summer time !

Requirements
Download Apache Thrift and Thrift compiler for Windows from the download page here.
Download and build Boost libraries (also follow Adil’s tutorial here: Configuring C++ Boost Libraries for Visual Studio).
Download libevent library from the official webpage.
Download OpenSSL for Windows (when you are on the OpenSSL binaries page, follow the link they suggest under OpenSSL for Windows — Works with MSVC++), and then install it.

Building Apache Thrift libraries

Open the Developer Visual Studio 2015 Command Prompt that you find viaStart > All Programs > Visual Studio 2015 > Developer Command Prompt for VS2015
Therein, navigate to the unzipped libevent directory (where sit sub-directories such as “compat”, “include”, and “test”).
Run the command nmake -f Makefile.nmake, which will build libevent.
Extract Apache Thrift tar file and navigate to {thrift_install_dir}\lib\cpp, then open thrift.sln project with Visual Studio 2015 (you’ll probably be prompted to upgrade the project, just accept it kindly!). The solution contains two projects: libthrift and libthriftnb.
Right-click on libthrift project and** select Properties, then go to C/C++ > General and under Additional Include Directories enter the following line: {boost_install_dir};{boost_install_dir}\boost_1_56_0\boost;{openssl_install_dir}\include , then to Librarian > General and under Additional Library Directories enter the following line:{openss_install_dir}\lib.**
Right-click now on libthriftnb project and select Properties, then go to C/C++->General and under Additional Include Directories enter the following line: {boost_install_dir};{libevent_install_dir};{libevent_install_dir}\include;{libevent_install_dir}\WIN32-Code;.

Inside** libthrift** project content, expand concurrency**** folder and Remove BoostThreadFactory.cpp file. This file causes compilation issues and must be removed in order to continue.
Select type of build: Debug or Release, and Build the two projects. Upon a success, you will see libthrift.lib and libthriftnb.lib files inside Debug or Release directory respectively.

Building Apache Thrift Server
Download the tutorial.thrift file (from here) and shared.thrift file (from here) –save them to the location where Thrift compiler for Window (thrift_x.y.z.exe) is located.
Run these the following two commands to generate C++ code for both of these service definition files:

1

thrift -r --gen cpp tutorial.thrift

1

thrift -r --gen cpp shared.thrift

This will generate a directory called gen-cpp in the same previous location.
Create a new Visual Studio project and drug-and-drop the content of the gen-cpp folder into it: *.h files under Header Files and *.ccp files under Sources Files, so it gives this structure: **


Right-click on your project and select Properties, then go to C/C++ > All Options, and under Additional Include Directories enter the following line: {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;, then to Linker > All Options, and under Additional Include Dependencies enter the following line: libboost_thread-vc140-mt-gd-1_59.lib;libboost_chrono-vc140-mt-gd-1_59.lib;libthrift.lib;%(AdditionalDependencies)Beware:You’ll modify these manually so be careful to the version numbers. In my case I downloaded Boost for Visual Studio 2015 (boost_1_59_0-msvc-14.0-64.exe), hence the presence of cv14, and 1_59. Change these two values following your Boost version you downloaded.

Note: The above** **two libraries are located under {boost_install_dir}/stage/lib….,…then to Linker > All Options and under Additional Library Directories enter the following line: {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug
**Note: **The second item in the path could end with \Release, if you used the Release configuration instead of Debug while compiling Apache Thrift (I used Debug for this tutorial).

Remove skeleton files (*.skeleton.cpp) from the project.
Build the project. If it succeeds than you can start the server.
Building Apache Thrift Client

Create a new Visual Studio project and drug-and-drop the content of the gen-cpp folder into it: *.h files under Header Files and .ccp files under Sources Files,
Download the CppClient.cpp file (from here) –save it under Sources Files.
Remove skeleton files (
.skeleton.cpp) from the project.
Right-click on the project and select Properties, then go to C/C++ > All Options and under Additional Include Directories enter the following line: {boost_install_dir};{thrift_install_dir}\lib\cpp\src\thrift\windows;{thrift_install_dir}\lib\cpp\src;, then to Linker > All Options and under Additional Library Directories enter the following line: libboost_thread-vc140-mt-gd-1_59.lib;libboost_chrono-vc140-mt-gd-1_59.lib;libthrift.lib;%(AdditionalDependencies)
Beware & note: [see point 4 of the previous section (Build Apache Thrift Server)].
…then to Linker > All Options and under Additional Library Directories enter the following line: {boost_install_dir}\stage\lib;{thrift_install_dir}\lib\cpp\Debug
Note: [see point 4 of the previous section (Build Apache Thrift Server)].

Build the project. If it succeeds than you can start the client (Ensure that your Server is already running).

Hula hoop! enjoy watching client and server running business!
NB: as anticipated in the introduction, the above steps have been also tested under Visual Studio 2013 with slight changes that you can guess yourselves, I guess!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容