cygwin上编译RPC框架thrift

cygwin的配置就不多说了,缺什么就通过安装器安装好了。

在thrift的官网下载 thrift-0.11.0源码,通过cmake编译,生成Makefile之后,make出现编译错误

/cygdrive/d/work/thrift-0.11.0/mybuild $ make

[ 1%] Building CXX object compiler/cpp/CMakeFiles/parse.dir/thrift/thrifty.cc.o
/cygdrive/d/work/thrift-0.11.0/compiler/cpp/src/thrift/thrifty.yy: 在函数‘int yyparse()’中:
/cygdrive/d/work/thrift-0.11.0/compiler/cpp/src/thrift/thrifty.yy:1197:20: 错误:‘strdup’在此作用域中尚未声明
$$ = strdup("1");
^~~~
/cygdrive/d/work/thrift-0.11.0/compiler/cpp/src/thrift/thrifty.yy:1197:20: 附注:suggested alternative: ‘strcmp’
$$ = strdup("1");
^~~~
strcmp
make[2]: *** [compiler/cpp/CMakeFiles/parse.dir/build.make:74:compiler/cpp/CMakeFiles/parse.dir/thrift/thrifty.cc.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:1055:compiler/cpp/CMakeFiles/parse.dir/all] 错误 2
make: *** [Makefile:161:all] 错误 2

查看cmake生成Makefile的日志,在某个cmake文件里设置了-std=c++11,找到:

D:\work\thrift-0.11.0\build\cmake\DefineCMakeDefaults.cmake (2 hits)
Line 79: message(STATUS "Setting C++98 as the default language level (for an older MSVC compiler).")
Line 82: message(STATUS "Setting C++11 as the default language level.")

如下修改:

if (NOT DEFINED CMAKE_CXX_STANDARD)
  if (MSVC AND MSVC_VERSION LESS 1800)
    # MSVC 2012 and earlier don't support template aliases so you have to use C++98
    set(CMAKE_CXX_STANDARD 98) 
    message(STATUS "Setting C++98 as the default language level (for an older MSVC compiler).")
  else()
    #set(CMAKE_CXX_STANDARD 11) # C++11
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
    message(STATUS "Setting C++11 as the default language level.")
  endif()
  message(STATUS "To specify a different C++ language level, set CMAKE_CXX_STANDARD")
endif()

 继续编译,随即遇到编译错误:

[ 11%] Building CXX object lib/cpp/CMakeFiles/thrift_static.dir/src/thrift/transport/THttpServer.cpp.o
/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/transport/THttpServer.cpp: 在成员函数‘virtual void apache::thrift::transport::THttpServer::parseHeader(char*)’中:
/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/transport/THttpServer.cpp:52:47: 错误:‘strcasestr’在此作用域中尚未声明
   #define THRIFT_strcasestr(haystack, needle) strcasestr(haystack, needle)
                                               ^
/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/transport/THttpServer.cpp:64:9: 附注:in expansion of macro ‘THRIFT_strcasestr’
     if (THRIFT_strcasestr(value, "chunked") != NULL) {
         ^~~~~~~~~~~~~~~~~
/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/transport/THttpServer.cpp:52:47: 附注:suggested alternative: ‘strcasecmp’
   #define THRIFT_strcasestr(haystack, needle) strcasestr(haystack, needle)
                                               ^
/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/transport/THttpServer.cpp:64:9: 附注:in expansion of macro ‘THRIFT_strcasestr’
     if (THRIFT_strcasestr(value, "chunked") != NULL) {
         ^~~~~~~~~~~~~~~~~
make[2]: *** [lib/cpp/CMakeFiles/thrift_static.dir/build.make:495:lib/cpp/CMakeFiles/thrift_static.dir/src/thrift/transport/THttpServer.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:1140:lib/cpp/CMakeFiles/thrift_static.dir/all] 错误 2
make: *** [Makefile:161:all] 错误 2

这里我就直接修改源代码了:

#define _GNU_SOURCE
#include <string.h>

将这两行放在thrift-0.11.0/lib/cpp/src/thrift/transport/THttpServer.cpp的其他include头文件的前面,不然 #define _GNU_SOURCE 会被编译器忽略

 随即又遇到编译错误:

/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/concurrency/Mutex.cpp: 在静态成员函数‘static void apache::thrift::concurrency::Mutex::RECURSIVE_INITIALIZER(void*)’中:
/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/concurrency/Mutex.cpp:271:41: 错误:‘PTHREAD_MUTEX_RECURSIVE_NP’在此作用域中尚未声明
   init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
/cygdrive/d/work/thrift-0.11.0/lib/cpp/src/thrift/concurrency/Mutex.cpp:271:41: 附注:suggested alternative: ‘PTHREAD_MUTEX_RECURSIVE’
   init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
                                         PTHREAD_MUTEX_RECURSIVE
make[2]: *** [lib/cpp/CMakeFiles/thrift_static.dir/build.make:951:lib/cpp/CMakeFiles/thrift_static.dir/src/thrift/concurrency/Mutex.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:11
这里又是cygwin的库问题了,不支持PTHREAD_MUTEX_RECURSIVE_NP,改成 PTHREAD_MUTEX_RECURSIVE
#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
void Mutex::RECURSIVE_INITIALIZER(void* arg) {
  //init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
  init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE);
}
#endif

 链接单元测试时又出现库不兼容的问题。。。

CMakeFiles/UnitTests.dir/concurrency/MutexTest.cpp.o:MutexTest.cpp:(.rdata$.refptr._ZN6apache6thrift11concurrency5Mutex20ADAPTIVE_INITIALIZEREPv[.refptr._ZN6apache6thrift11concurrency5Mutex20ADAPTIVE_INITIALIZEREPv]+0x0):对‘apache::thrift::concurrency::Mutex::ADAPTIVE_INITIALIZER(void*)’未定义的引用
collect2: 错误:ld 返回 1
make[2]: *** [lib/cpp/test/CMakeFiles/UnitTests.dir/build.make:339:bin/UnitTests.exe] 错误 1
make[1]: *** [CMakeFiles/Makefile2:1736:lib/cpp/test/CMakeFiles/UnitTests.dir/all] 错误 2
make: *** [Makefile:161:all] 错误 2

自适应锁在cygwin的最新版是没有的,就给它弄个默认的,该咋死咋死吧:

#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
void Mutex::ADAPTIVE_INITIALIZER(void* arg) {
  // From mysql source: mysys/my_thr_init.c
  // Set mutex type to "fast" a.k.a "adaptive"
  //
  // In this case the thread may steal the mutex from some other thread
  // that is waiting for the same mutex. This will save us some
  // context switches but may cause a thread to 'starve forever' while
  // waiting for the mutex (not likely if the code within the mutex is
  // short).
  init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_ADAPTIVE_NP);
}
#else
void Mutex::ADAPTIVE_INITIALIZER(void* arg) {
  // From mysql source: mysys/my_thr_init.c
  // Set mutex type to "fast" a.k.a "adaptive"
  //
  // In this case the thread may steal the mutex from some other thread
  // that is waiting for the same mutex. This will save us some
  // context switches but may cause a thread to 'starve forever' while
  // waiting for the mutex (not likely if the code within the mutex is
  // short).
  init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_NORMAL);
}    
#endif

 OK,至此,完美编译成功!

posted @ 2018-06-25 17:26  丹西  阅读(820)  评论(0编辑  收藏  举报