lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2013年03月19日

cygwin上编译protobuf遇到No suitable threading library available的解决办法

Filed under: Android — 标签:, , , , — lizongbo @ 09:46

打算在Android的ndk中使用protobuf,参考了网上的说明,结果在cygwin下编译protobuf遇到”No suitable threading library available.”

以下是运行make时的错误信息:

/bin/sh ../libtool –tag=CXX –mode=compile g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c -o common.lo `test -f ‘google//stubs/common.cc’ || echo ‘./’`google//stubs/common.cc
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c google/protobuf/stubs/common.cc -DDLL_EXPORT -DPIC -o .libs/common.o
google/protobuf/stubs/common.cc:48:2: error: #error “No suitable threading library available.”
Makefile:1184: recipe for target `common.lo’ failed
make[2]: *** [common.lo] Error 1
make[2]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0/src”
Makefile:568: recipe for target `all-recursive’ failed
make[1]: *** [all-recursive] Error 1
make[1]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0”
Makefile:477: recipe for target `all’ failed
make: *** [all] Error 2

编译的步骤其实非常简单:

网上下载最新的protobuf文件:

http://protobuf.googlecode.com/files/protobuf-2.5.0.zip
来源:http://code.google.com/p/protobuf/

解压到:D:\cpplibs得到:D:\cpplibs\protobuf-2.5.0

然后cygwin下依次运行以下命令:

 

lizongbo@PC_LI ~
$ cd /cygdrive/d/cpplibs/protobuf-2.5.0/
lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ ./configure

lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ make

lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ make check

lizongbo@PC_LI /cygdrive/d/cpplibs/protobuf-2.5.0
$ make install

正常情况下就编译ok了,结果我运行make的是提示如下出错信息:

/bin/sh ../libtool –tag=CXX –mode=compile g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c -o common.lo `test -f ‘google/protobuf/stubs/common.cc’ || echo ‘./’`google/protobuf/stubs/common.cc
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I.. -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -O2 -g -DNDEBUG -MT common.lo -MD -MP -MF .deps/common.Tpo -c google/protobuf/stubs/common.cc -DDLL_EXPORT -DPIC -o .libs/common.o
google/protobuf/stubs/common.cc:48:2: error: #error “No suitable threading library available.”
Makefile:1184: recipe for target `common.lo’ failed
make[2]: *** [common.lo] Error 1
make[2]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0/src”
Makefile:568: recipe for target `all-recursive’ failed
make[1]: *** [all-recursive] Error 1
make[1]: 离开目录“/cygdrive/d/cpplibs/protobuf-2.5.0”
Makefile:477: recipe for target `all’ failed
make: *** [all] Error 2

 

在网上找遍了资料也没找到解决办法,然后查看common.cc对应代码为:

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN  // We only need minimal includes
#include <windows.h>
#define snprintf _snprintf    // see comment in strutil.cc
#elif defined(HAVE_PTHREAD)
#include <pthread.h>
#else
#error “No suitable threading library available.”
#endif

也是是非win32平台且找不到pthread的情况下才出这个问题。

查看运行configure的记录则是:

checking for library containing zlibVersion… -lz
checking for the pthreads library -lpthreads… no
checking whether pthreads work without any flags… no
checking whether pthreads work with -Kthread… no
checking whether pthreads work with -kthread… no
checking for the pthreads library -llthread… no
checking whether pthreads work with -pthread… no
checking whether pthreads work with -pthreads… no
checking whether pthreads work with -mthreads… no
checking for the pthreads library -lpthread… no
checking whether pthreads work with –thread-safe… no
checking whether pthreads work with -mt… no
checking for pthread-config… no

但网上有人说了cygwin是自带pthread库的(

Pthread is included in .  参考:http://stackoverflow.com/questions/2087435/some-issues-about-cygwinlinux-in-windows-socket-thread-other-programming-and  )

尝试各种方法之后仍然失败。

晚上在家里用电脑再按上述步骤操作,发现编译ok了。

再次尝试后总结出了原因,是因为我在环境变量里Path中增加了MinGW的bin目录,

在安装cygwin之前刚好先安装了minGW,且配置了以下环境变量:
MINGW_HOME=C:\MinGW
LIBRARY_PATH = %MINGW_HOME%\lib
C_INCLUDE_PATH = %MINGW_HOME%\include
在PATH变量的最后增加 ;%MINGW_HOME%\bin

于是把上面几个环境变量去掉,重新进入cygwin,终于编译ok了。

正常编译情况下运行configure的记录则是:

checking for the pthreads library -lpthreads… no
checking whether pthreads work without any flags… yes
checking for joinable pthread attribute… PTHREAD_CREATE_JOINABLE
checking if more special flags are required for pthreads… no
checking whether to check for GCC pthread/shared inconsistencies… no
checking whether what we have so far is sufficient with -nostdlib… no
checking whether -lpthread saves the day… no
configure: WARNING: Impossible to determine how to use pthreads with shared libr
aries and -nostdlib

 

 

 

D:\cpplibs\protobuf-2.5.0\src\.libs下顺利得到了libprotobuf.a,libprotobuf-lite.a,libprotoc.a等文件。

D:\cygwin\usr\local\include则是protobuf的相应头文件。

 

 

没有评论 »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress