Chinaunix首页 | 论坛 | 博客
  • 博客访问: 559328
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: 系统运维

2015-06-14 20:04:43

最近要编写的程序需要使用 protobuf ,所以总结一个 protobuf github 版本安装的脚本.
由于还没有学习 sed 和 gawk 操作,所以中途因为源代码中有一个地方需要修改,还是需要手动完成的.

国内的 protobuf:googlecode 网站已经被封了,我的virtual box 无论如何是连接不上 ,所以,采用从 github 上下载最新版本的.

github 上的文档中已经详细说明,
从 github 上面下载的 protobuf 是不附带 gtest
(google test :用于 protobuf 的单元测试,
googlecode 上所提供的  protobuf 附加 gtest ) 的,所以需要通过运行 autogen.sh 脚本文件来从 googlecode 上面自动下载并安装 。

我的 virtual box 同样也无法访问 googlecode ,因此,autogen.sh 脚本无法实现下载,编译并安装 gtest ,
只好自己编写脚本来实现将 protobuf 的依赖文件 gtest 从 github 下载,好在,github 上的 gtest 提供configure 文件 ,
安装 gtest 只需要执行 ./configure && make && make install 便可以正确的安装. 

脚本一共有 2 个,
一个是从 github 上面下载 protobuf 和 gtest 文件,并安装 gtest 文件

中途, 需要修改下载的 protobuf 中的编译配置文件 configure.ac 和 注释掉 protobuf 源文件中跨平台编译的有关 Win32 的源代码。

一个是通过 autotools 来编译 protobuf 中的系列文件


为了防止编译错误,需要提前修改的地方有如下几处

1.  protobuf 路径下的 configure.ac , 这个文件相当于是通过 autotools 中的  autoscan 命令所生成的 configure.in 文件, 将其中的 ''AC_CXX_STL_HASH" 使用 # 进行注释掉

2. 'src/google/protobuf/stubs/common.cc' 这个文件中,把有关 _WIN32 的所有代码都注释掉,因为我的平台是 linux ,并且在安装 gcc 的时候,是选择不支持跨平台交叉编译的,
   所以,不会使用到与 _WIN32 任何有关的代码 , 如果您编程的需求[gcc支持跨平台编译,或是需要使用 windows 中的函数库 ] 和我的不同,建议不要轻易修改.

我对文件 common.cc 注释掉的代码如下:


点击(此处)折叠或打开

  1. // updated by Aimer on linux platform

  2. //#ifdef _WIN32
  3. //#define WIN32_LEAN_AND_MEAN // We only need minimal includes
  4. //#include <windows.h>
  5. //#define snprintf _snprintf // see comment in strutil.cc
  6. //#elif defined(HAVE_PTHREAD)
  7. #include <pthread.h>
  8. //#else
  9. //#error "No suitable threading library available."
  10. //#endif

点击(此处)折叠或打开

  1. // ===================================================================
  2. // emulates google3/base/mutex.cc

  3. /*---------------------------------------
  4. modified by Aimer

  5. #ifdef _WIN32

  6. struct Mutex::Internal {
  7.   CRITICAL_SECTION mutex;
  8. #ifndef NDEBUG
  9.   // Used only to implement AssertHeld().
  10.   DWORD thread_id;
  11. #endif

  12. };

  13. Mutex::Mutex()
  14.   : mInternal(new Internal) {
  15.   InitializeCriticalSection(&mInternal->mutex);
  16. }

  17. Mutex::~Mutex() {
  18.   DeleteCriticalSection(&mInternal->mutex);
  19.   delete mInternal;
  20. }

  21. void Mutex::Lock() {
  22.   EnterCriticalSection(&mInternal->mutex);
  23. #ifndef NDEBUG
  24.   mInternal->thread_id = GetCurrentThreadId();
  25. #endif
  26. }

  27. void Mutex::Unlock() {
  28. #ifndef NDEBUG
  29.   mInternal->thread_id = 0;
  30. #endif
  31.   LeaveCriticalSection(&mInternal->mutex);
  32. }

  33. void Mutex::AssertHeld() {
  34. #ifndef NDEBUG
  35.   GOOGLE_DCHECK_EQ(mInternal->thread_id, GetCurrentThreadId());
  36. #endif
  37. }

  38. #elif defined(HAVE_PTHREAD)
  39. ---------------------------*/

  40. struct Mutex::Internal {
  41.   pthread_mutex_t mutex;
  42. };

  43. Mutex::Mutex()
  44.   : mInternal(new Internal) {
  45.   pthread_mutex_init(&mInternal->mutex, NULL);
  46. }

  47. Mutex::~Mutex() {
  48.   pthread_mutex_destroy(&mInternal->mutex);
  49.   delete mInternal;
  50. }

  51. void Mutex::Lock() {
  52.   int result = pthread_mutex_lock(&mInternal->mutex);
  53.   if (result != 0) {
  54.     GOOGLE_LOG(FATAL) << "pthread_mutex_lock: " << strerror(result);
  55.   }
  56. }

  57. void Mutex::Unlock() {
  58.   int result = pthread_mutex_unlock(&mInternal->mutex);
  59.   if (result != 0) {
  60.     GOOGLE_LOG(FATAL) << "pthread_mutex_unlock: " << strerror(result);
  61.   }
  62. }

  63. void Mutex::AssertHeld() {
  64.   // pthreads dosn't provide a way to check which thread holds the mutex.
  65.   // TODO(kenton): Maybe keep track of locking thread ID like with WIN32?
  66. }

  67. /*-----------------------
  68. #endif
  69. -------------------------*/

3. 执行命令 libtoolize --automake --copy --debug --force ; 这个解决方法是从网上找的,是中途执行 automake --add-missing 编译的时候遇到了
   'required file `build/ltmain.sh' not found' 错误而找的解决方案, 这条命令已经被写入到脚本2中了.

在修改了上述几个地方之后,通过执行脚本 2 便可以正确的被编译安装了



手动安装已经正确安装,并且已经可以运行, 脚本程序现在正在另一台 vb 上面进行测试,如果有错误的话会及时的更新这篇博客

step1.sh


点击(此处)折叠或打开

  1. #!/bin/bash

  2. # set protobuf installation path name

  3. PROTOBUF_PATH="/unixC/protobuf"


  4. #git clone from self github branch

  5. echo "git clone protobuf, wait ....."
  6. echo

  7. git clone git@github.com:aimer1027/protobuf.git $PROTOBUF_PATH

  8. echo "gin clone finish .... begin running autogen.sh script .... "
  9. echo

  10. mkdir $PROTOBUF_PATH

  11. echo "git clone gtest "
  12. echo

  13. git clone git@github.com:kgcd/gtest.git $PROTOBUF_PATH/gtest

  14. echo "configure and compile && install gtest "
  15. echo

  16. $PROTOBUF_PATH/gtest/configure --prefix=/usr/local/gtest

  17. cd /unixC/protobuf/gtest && make && make install

  18. echo "---------------- gtest installation finish ----------------------"
  19. echo
  20. echo
  21. echo "now ,change 1. configure.ac under ../protobuf/ "
  22. echo "and common.cc source file under ../protobuf/src/google/protobuf/stub/"

step2.sh

点击(此处)折叠或打开

  1. #!/bin/bash

  2. # set protobuf installation path name

  3. PROTOBUF_PATH="/unixC/protobuf"

  4. # in case of compiling error 'required file `build/ltmain.sh' not found'
  5. libtoolize --automake --copy --debug --force

  6. # execute autotools serial compile operations

  7. cd $PROTOBUF && aclocal
  8. cd $PROTOBUF && autoconf
  9. cd $PROTOBUF && autoheader

  10. cd $PROTOBUF && automake --add-missing

  11. # compile prefer setting
  12. $PROTOBUF_PATH/configure --prefix=/usr/local

  13. echo "finish setting , now we begin make && make install protobuf .... "

  14. # make and make install protobuf
  15. cd $PROTOBUF_PATH && make && make install

  16. echo "protobuf installation finish !"

  17. echo
  18. echo

  19. echo "let us update the system shared library"
  20. ldconfig

  21. echo
  22. echo
  23. echo "let us check whether protoc [protobuf compiler] can run on this platform"
  24. protoc

end

阅读(5463) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~