vs2015编译protobuf-3.1.0 - Go语言中文社区

vs2015编译protobuf-3.1.0


1、安装vs2015

2、 安装cmake  https://cmake.org/download/

3、下载protobuff 3.1.0  https://github.com/google/protobuf/releases/

 

解压protobuf压缩包,在和protobuf同级目录下新建一个install文件夹,用作编译完成后方include ,lib等文件。

E:pathinstall

E:pathprotobuf-3.1.0

 

 

从VS开发人员命令行工具进入protobuf目录,创建build目录

 

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. E:pathprotobuf-3.1.0cmake>mkdir build & cd build  
  2. E:pathprotobuf-3.1.0cmakebuild>  

创建release版本的编译目录:

 

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. E:pathprotobuf-3.1.0cmakebuild>mkdir release & cd release  
  2. E:pathprotobuf-3.1.0cmakebuildrelease>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install ../..

创建debug版本的编译目录:

 

 

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. C:Pathtoprotobufcmakebuild>mkdir debug & cd debug  
  2. C:Pathtoprotobufcmakebuilddebug>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../../../../install ../..

创建生成visual stuido 工程的文件夹:

这一步需要注意的是,

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. "Visual Studio 14 2015 Win64"  

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. 是因为安装了visual studio 2015而决定的,这是所谓的generator,不同编译器是不同的,具体类型可见:<span style="white-space:pre">   http://www.cmake.org/cmake/help/latest/manual/cmake-generators.7.html#visual-studio-generators</span>  

 

 

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. C:Pathtoprotobufcmakebuild>mkdir solution & cd solution  
  2. C:Pathtoprotobufcmakebuildsolution>cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=../../../../install ../..

 

以上3种不是必须都做。

 

 

 

通过以上3个步骤,可以见到在不同目录都生成了相应的makefile文件,接下来是执行nmake进行编译:

 

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. To compile protobuf:  
  2.   
  3.      C:Pathtoprotobufcmakebuildrelease>nmake  
  4.   
  5. or  
  6.   
  7.      C:Pathtoprotobufcmakebuilddebug>nmake  

 

 

以下安装头文件、库文件等安装到之前制定的文件(install):

 

[cpp] view plain copy    在CODE上查看代码片派生到我的代码片

  1. To install protobuf to the specified *install* folder:  
  2.   
  3.      C:Pathtoprotobufcmakebuildrelease>nmake install  
  4.   
  5. or  
  6.   
  7.      C:Pathtoprotobufcmakebuilddebug>nmake install  

 

 

到此,release 和 debug版本都编译成功,vs可以使用了。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

vs平台开发用sln生成库要注意两点:

第一:

solution目录下有生成sln文件,可以用vs打开生成库,但要注意位数,比如如果

=====================================================================================================================================

 

 

2.D:protobuf-2.6.1examples本来是有例子的,我们亲自实践下,动手自己在此目录下定义一个proto:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

package tutorial; 

 

message Person { 

 required string name = 1; 

 required int32 age = 2; 

 optional string email = 3; 

 

然后使用cmd运行protoc.exe生成我们的目标语言格式(c++).

 

通过命令行将.proto的文件生成为.cpp的文件官网上是这样写的

protoc -I=$SRC_DIR --cpp_out=$DST_DIR$SRC_DIR/addressbook.proto

 

-I=$SRC_DIR 是源文件目录

--cpp_out=$DST_DIR是目标文件目录,cpp_out表示输出为cpp文件

$SRC_DIR/addressbook.proto表示要转换的的文件,包括目录和文件名

 

源文件目录如果省略,就代表是当前目录

目标文件如果省略,就代表输出目录是当前目录

示例:省略版

protoc-2.4.1-win32>protoc.exe ./addressbook.proto --cpp_out=./ 

 

示例:完整版

cd D:protobuf-2.6.1vsprojectsDebug

D:protobuf-2.6.1vsprojectsDebug>protoc-I=D:protobuf-2.6.1examples --cpp_out=D:protobuf-2.6.1examplesD:protobuf-2.6.1examplesperson.proto

 

然后可以看到,生成了person.pb.h和person.pb.cc的文件。

3.我们用vs2012新建一个空的项目,选择属性,配置一下:

 

 

点击配置属性 下的 C/C++ 的 常规,右边附加包含目录,导入这个路径D:protobuf-2.6.1src

点击链接器的常规,右边的附加库目录,导入这个路径D:protobuf-2.6.1vsprojectsDebug

 

 

三.开始一个最简单的项目

好了,一切配置好了,该写代码了,我们做一个最简单的输入输出。新建一个main.cpp,然后把之前生成的person.pb.h和person.pb.cc复制到项目里面,并添加到项目里面。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

#include <iostream> 

#include "person.pb.h" 

  

using namespace std; 

using namespace tutorial; 

 

int main() 

   Person person; 

 

   person.set_name("flamingo");    

   person.set_age(18);    

 

   cout<<person.name()<<endl; 

   cout<<person.age()<<endl; 

 

 

   system("pause"); 

   return 0; 

 

 

有些人说可以正常运行,但是我这边不行,主要是

 

网上查找原因,终于发现,需要在代码里面加两行:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

#pragma comment(lib,"libprotobuf.lib") 

#pragma comment(lib,"libprotoc.lib") 

 

就能正常跑了:

 

 

protobuf的使用和原理,请查看:http://blog.csdn.net/majianfei1023/article/details/45112415

获取更多帮主请关注小程序

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/xie1xiao1jun/article/details/53668423
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-09-13 23:28:45
  • 阅读 ( 1208 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢