|
最近要编写的程序需要使用 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 注释掉的代码如下:
- // updated by Aimer on linux platform
- //#ifdef _WIN32
- //#define WIN32_LEAN_AND_MEAN // We only need minimal includes
- //#include
- //#define snprintf _snprintf // see comment in strutil.cc
- //#elif defined(HAVE_PTHREAD)
- #include
- //#else
- //#error "No suitable threading library available."
- //#endif
// ===================================================================
// emulates google3/base/mutex.cc
/*---------------------------------------
modified by Aimer
#ifdef _WIN32
struct Mutex::Internal {
CRITICAL_SECTION mutex;
#ifndef NDEBUG
// Used only to implement AssertHeld().
DWORD thread_id;
#endif
};
Mutex::Mutex()
: mInternal(new Internal) {
InitializeCriticalSection(&mInternal->mutex);
}
Mutex::~Mutex() {
DeleteCriticalSection(&mInternal->mutex);
delete mInternal;
}
void Mutex::Lock() {
EnterCriticalSection(&mInternal->mutex);
#ifndef NDEBUG
mInternal->thread_id = GetCurrentThreadId();
#endif
}
void Mutex::Unlock() {
#ifndef NDEBUG
mInternal->thread_id = 0;
#endif
LeaveCriticalSection(&mInternal->mutex);
}
void Mutex::AssertHeld() {
#ifndef NDEBUG
GOOGLE_DCHECK_EQ(mInternal->thread_id, GetCurrentThreadId());
#endif
}
#elif defined(HAVE_PTHREAD)
---------------------------*/
struct Mutex::Internal {
pthread_mutex_t mutex;
};
Mutex::Mutex()
: mInternal(new Internal) {
pthread_mutex_init(&mInternal->mutex, NULL);
}
Mutex::~Mutex() {
pthread_mutex_destroy(&mInternal->mutex);
delete mInternal;
}
void Mutex::Lock() {
int result = pthread_mutex_lock(&mInternal->mutex);
if (result != 0) {
GOOGLE_LOG(FATAL) |
|
|