随笔 - 298  文章 - 377  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(34)

随笔分类

随笔档案

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

下载地址:https://github.com/google/protobuf

1.编译
通过cmake生成sln文件来编译
用cmd命令 cd 到3.0.0-beta-4\cmake

1
2
3
mkdir build & cd build
mkdir solutionx86 & cd solutionx86
cmake -G "Visual Studio 14 2015" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_GENERATOR_TOOLSET=v140_xp

如果要编译64位的话

1
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_GENERATOR_TOOLSET=v140_xp

注意中间不能有回车之类的,该命令行生成的是dll版本的(静态库的话要注意lib的运行库),其它版本请查考cmake目录下面的README.md

 

2.hello world

将examples目录下满的addressbook.proto,拷贝到生成的protoc.exe目录下面,cd到该目录

protoc --cpp_out=./ addressbook.proto

这样就会生成addressbook.pb.cc , addressbook.pb.h  两个文件,包含到你的项目中

addressbook.pb.cc,需要取消预编译头或者手动加入#include "stdafx.h"

然后包含头文件和lib文件

直接用examples目录下面的 add_person.cc里面的代码测试即可

 

注意:使用dll的时候需要定义宏PROTOBUF_USE_DLLS,小心不要将这个PROTOBUF_USE_DLLS定义在stdafx.h里面,然后你又取消了预编译头!


@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

需要工具:

1. visual studio 2015 

2. cmake

3.Git[可选]


安装完毕,继续



从官网下载protobuf :https://github.com/google/protobuf/releases/tag/v3.0.0



window编译protobuf有两种方式:一个使用vs自带的msvc(环境测试vs开发者命令行工具),一种是用Cygwin 或者 MinGW。

此处使用msvc 进行编译。


使用vs开发者命令行工具,先测试cmake是否可用。


打开上图第一项,测试:



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

如:

[html] view plain copy
  1. C:\Path\to\install  


进入protobuf目录,创建build目录

[cpp] view plain copy
  1. C:\Path\to\protobuf\cmake>mkdir build & cd build  
  2. C:\Path\to\protobuf\cmake\build>  


以下注意^和命令之间的空格

创建release版本的编译目录:

[cpp] view plain copy
  1. C:\Path\to\protobuf\cmake\build>mkdir release & cd release  
  2. C:\Path\to\protobuf\cmake\build\release>cmake -G "NMake Makefiles" ^  
  3. -DCMAKE_BUILD_TYPE=Release ^  
  4. -DCMAKE_INSTALL_PREFIX=../../../../install ^  
  5. ../..  

创建debug版本的编译目录:

[cpp] view plain copy
  1. C:\Path\to\protobuf\cmake\build>mkdir debug & cd debug  
  2. C:\Path\to\protobuf\cmake\build\debug>cmake -G "NMake Makefiles" ^  
  3. -DCMAKE_BUILD_TYPE=Debug ^  
  4. -DCMAKE_INSTALL_PREFIX=../../../../install ^  
  5. ../..  


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

这一步需要注意的是,

[cpp] view plain copy
  1. "Visual Studio 14 2015 Win64"  
[cpp] view plain copy
  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
  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" ^  
  3. -DCMAKE_INSTALL_PREFIX=../../../../install ^  
  4. ../..  

注意:这里用了Win64 那么生成的就是64位库,想要32位库就去掉Win64


以上3种不是必须都做。


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

[cpp] view plain copy
  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
  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打开生成库,但要注意位数,比如如果


vs + qt (32位库)

那么,protobuf也应该改成win32平台,讲所有项目都改成win32平台的,




不然会出现: fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

另外,32位编译器可以编译32和64位程序,这里选用win32



第二:


编译的库要和你使用的版本一致,如release debug,在下图的位置确定;



MD代表动态链接,MT代表静态链接,后面没有有d代表是release模式-》


release模式则用前两种。


同时,如果你在项目用到其他库,比如vs  + qt (32位动态链接库),那么protobuf也应该用动态链接方式生成lib,不然提示:

值“MT_StaticRelease”不匹配值“MD_DynamicRelease”

等这类错误。


动态链接方式:MD

静态链接方式:MT


总之,qt 库链接方式要和protobuf的一致,如果还用到其他的,全部都要一致。




cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF

cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../../../../installDebug ../.. -Dprotobuf_BUILD_TESTS=OFF



cmake -G "Visual Studio 14 2015" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_GENERATOR_TOOLSET=v140_xp  



cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=../../../../install ../..  


cmake -G "Visual Studio 14 2015" -DCMAKE_INSTALL_PREFIX=../../../../install ../..  -Dprotobuf_BUILD_TESTS=OFF

posted on 2017-08-02 23:17 聂文龙 阅读(1629) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理