vs2015编译protobuf-3.1.0

12 篇文章 1 订阅

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:\path\install

E:\path\protobuf-3.1.0


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

[cpp]   view plain   copy 
  
   在CODE上查看代码片
  1. E:\path\protobuf-3.1.0\cmake>mkdir build & cd build  
  2. E:\path\protobuf-3.1.0\cmake\build>  

创建release版本的编译目录:

[cpp]   view plain   copy 
  
   在CODE上查看代码片  派生到我的代码片
  1. E:\path\protobuf-3.1.0\cmake\build>mkdir release & cd release  
  2. E:\path\protobuf-3.1.0\cmake\build\release>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install ../..
创建debug版本的编译目录:  

[cpp]   view plain   copy 
  
   在CODE上查看代码片  派生到我的代码片
  1. C:\Path\to\protobuf\cmake\build>mkdir debug & cd debug  
  2. C:\Path\to\protobuf\cmake\build\debug>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  
  
   在CODE上查看代码片  派生到我的代码片
  1. C:\Path\to\protobuf\cmake\build>mkdir solution & cd solution  
  2. C:\Path\to\protobuf\cmake\build\solution>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:\Path\to\protobuf\cmake\build\release>nmake  
  4.   
  5. or  
  6.   
  7.      C:\Path\to\protobuf\cmake\build\debug>nmake  


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

[cpp]   view plain   copy 
  
   在CODE上查看代码片  派生到我的代码片
  1. To install protobuf to the specified *install* folder:  
  2.   
  3.      C:\Path\to\protobuf\cmake\build\release>nmake install  
  4.   
  5. or  
  6.   
  7.      C:\Path\to\protobuf\cmake\build\debug>nmake install  


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

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

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

第一:

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

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


2.D:\protobuf-2.6.1\examples本来是有例子的,我们亲自实践下,动手自己在此目录下定义一个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.1\vsprojects\Debug

D:\protobuf-2.6.1\vsprojects\Debug>protoc-I=D:\protobuf-2.6.1\examples --cpp_out=D:\protobuf-2.6.1\examplesD:\protobuf-2.6.1\examples\person.proto

 

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

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

 

 

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

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

 

 

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

好了,一切配置好了,该写代码了,我们做一个最简单的输入输出。新建一个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") 

 

就能正常跑了:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值