Using VS for Proctocol buffer development under windows (C + + article)

Source: Internet
Author: User

Because of the working reason contact Proctocol Buffer (protobuf), as to what is protobuf, why use PROTOBUF, I will not repeat, Baidu is the answer.

Today I am introducing the environment configuration process and a simple example of using VS for protobuf development under Windows.

First give the official website of the town building, this official information can be found here https://github.com/google/protobuf

When it comes to new technology, it begins with confused, giving a general overview of the basic steps of using VS for proctocol buffer Development (c + +) under Windows.

1. Download the code package

2. Form the SLn file

3. Compile the required Lib files and Protoc.exe (EXE program can be downloaded directly)

4. Writing a. proto file

5. Generate. C and. h files with the Protoc.exe file

6. Add related resources (. c and. h files and Lib libraries) to the project to complete

Is still relatively clear, just at the beginning may not touch the road, do not know where to start. Yes, just a little bit of my running environment. Windowsx64 + vs2013

From the official download you need PROTOBUF source package version, if you choose 2.6.1, then congratulations, the configuration is quite simple, if you choose 3.0.0, then also congratulate you, you can learn to use a bit cmake

(In fact, the truth may be I lazy, do not like Linux, like to use the universe's strongest IDE vs, let vs instead of me to do, digress ...) )

1. Form the SLn file

2.6.1 version, open the folder "Vsprojects", there is a. sln file, open on it, yes, it is so simple.

3.0.0 version, there is no this folder (may be forgotten?) Or am I stupid not to find), do not panic, CMake folder under the Readme explicitly tell us how to generate this sln file. I'll translate it below (actually I added my test results)

Need to download CMake tools (No, don't do protobuf ... haha

Open vs2013 x64 Native tool command prompt, in the Start menu, vs Tools below, my path C:\Program Files (x86) \microsoft Visual Studio 12.0\common7\tools\ Shortcuts, really can not find a reference.

Install or unzip the cmake you downloaded

Add cmake environment variable (path\to is an instance path in the official document) in our dear black box (x64 native tools command Prompt)

C:\path\to>set path=%path%; C:\Program Files (x86) \cmake\bin

The CD command enters the CMake folder where you downloaded the source package, creates a new directory build in the CMake folder, and enters.

C:\path\to\protobuf\cmake>mkdir Build & CD build C:\Path\to\protobuf\cmake\build>

Create a release directory, if successful, the black box does not prompt the error, and you generated a lump of things in the corresponding directory.

C:\path\to\protobuf\cmake\build>mkdir Release & CD release C:\Path\to\protobuf\cmake\build\release " NMake makefiles  ^-dcmake_build_type=release ^-dcmake_install_prefix=. /.. /.. /.. /install ^ . /..

Create a debug directory

C:\path\to\protobuf\cmake\build>mkdir Debug & cd Debug C:\Path\to\protobuf\cmake\build\debug"  NMake makefiles" ^-dcmake_build_type=debug ^-dcmake_install_prefix=. /.. /.. /.. /install ^ . /..

The most critical step is to generate the SLn file

C:\path\to\protobuf\cmake\build>mkdir Solution & CD Solution C:\Path\to\protobuf\cmake\build\solution  "Visual Studio Win64" ^-dcmake_install_prefix=. /.. /.. /.. /install ^ . /..

Once the build is successful, you can double-click the sln file and compile it under vs.

During the generation process, I was sad because of the computer environment for the cause of the problem can not find Kernel32.lib caused the build failed, did not make it a bad day, finally by opening the VS installer, click Repair Fix.

You see, it's not a build!

Then use vs to open the project, compile, you see is not generated in the more things, which there are Protoc.exe and Libprotobuf.lib and libprotoc.lib files, this is what we need

Generate. C and. h files with the Protoc.exe run-as command

protoc-i= $SRC _dir--cpp_out= $DST _dir $SRC _dir/xxx.proto

If you want to have a Xxx.proto file, as to how this file is written, please see the link

Http://www.cnblogs.com/dkblog/archive/2012/03/27/2419010.html

2. Create a C + + console test Project

Well, to this point you have hot hot. cc and. h files, as well as Libprotobuf.lib and Libprotoc.lib, are eager to create a test project, okay, look at you so hungry, just meet you to create one. haha haha (i is mentally ill, you forgive me)

Create a console project, C + +, Add. cc and. h files, include. h files, compile, find errors

Error c1010, whether to forget to add #inlcude "Stdfx.h"

The first line in the generated. cc file says do not modify, but we are to modify, plus stdafx.h header file, yes, is so wayward, you have the source of what you fear!

Then add the above two. lib Files

Click "General" under "Configuration Properties", "Add to include directory" on the right, and import the path protobuf-xxx\src (source bundle downloaded from github). Compiling, right, and making mistakes, which is going to make you smooth? It's not a bad thing for a young man to eat a bit of pain .... (Omit 10000000 words here, add 300 rounds to beat the author)

Error is

Error c4996 Std::copy_impl ....

Workaround: Click "Preprocessor" under "c\c++" in "Properties" and add _scl_secure_no_warnings to the "preprocessor definition" on the right.

Click Compile again, is it successful? Isn't it? Are you excited?

You're going to think I'm going to give you an example project, no, the author is not a casual person, I will not do so, full network is, I write him why?

In fact, I will give you a high-level example of the project, the next blog to see .... (Do you know which stem it is?) )

Copyright is all I drop, reprint is to tell me drop (in fact, if you reprint I was very happy to drop, but remember the source AH)

Ask questions I am very welcome to drop, I am very fond of being harassed dripping!

Hope to help you!

Using vs for proctocol buffer development under windows (C + + article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.