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

开发注意事项

第一版本(当前版本)

libevent的版本比较老(1.4.15)。要测试网络性能时,需要考虑这一点影响。

第二版本

源代码编译环境

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 “lib 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
也就是说这个命令执行过之后,所有的新建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当前不支持的库

cppmicroservices

使用cmake编译。VS2017内建支持cmake,但是目前用起来还是很不方便。
所以还是用了cmake-gui来进行。
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系统之中,就可以避免这些问题(不过我没有试过,不知道是否麻烦)

不使用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!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,340评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,762评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,329评论 0 329
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,678评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,583评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,995评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,493评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,145评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,293评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,250评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,267评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,973评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,556评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,648评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,873评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,257评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,809评论 2 339

推荐阅读更多精彩内容