开发注意事项
第一版本(当前版本)
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.
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
另外的建议
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目录。
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
所以建议以后还是直接使用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!