当前位置:编程学习 > C/C++ >>

基于Windows平台编译libmemcached的步骤

 

1. 下载MinGW32到本地,以便能够使用gcc编译器以及和linux相关的一些库文件。

2. 由于libmemcached的测试程序需要依赖memcached.exe本身,如果需要将memcached在win32下编译,需要修改部分和socket相关的code,而又考虑到仅仅是测试用例需要,因此决定通过修改configure(由autoconfig用于生成makefile的配置信息检查的shell文件)文件,以使libmemcached的编译不在依赖memcached。

3. 为了完成第二步,需要手工修改configure文件,将如下shell代码注释掉,以便在执行./configure的时候不再将memcached.exe作为一个必须的配置检查条件。注: 在该文件中搜索--with-memcached即可找到以下code

# Check whether --with-memcached was given.

#if test "${with_memcached+set}" = set; then :

#  withval=$with_memcached; ac_cv_with_memcached="$withval"

#else

#  ac_cv_with_memcached=memcached

#fi

 

 

  # just ignore the user if --without-memcached is passed.. it is

  # only used by make test

#  if test "x$withval" = "xno"; then :

#

#      ac_cv_with_memcached=memcached

#      MEMC_BINARY=memcached

 

#else

#

#       if test -f "$withval"; then :

#

#           ac_cv_with_memcached=$withval

#           MEMC_BINARY=$withval

#

#else

#

           # Extract the first word of "$ac_cv_with_memcached", so it can be a program name with args.

#set dummy $ac_cv_with_memcached; ac_word=$2

#{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5

#$as_echo_n "checking for $ac_word... " >&6; }

#if test "${ac_cv_path_MEMC_BINARY+set}" = set; then :

#  $as_echo_n "(cached) " >&6

#else

#  case $MEMC_BINARY in

#  [\\/]* | ?:[\\/]*)

#  ac_cv_path_MEMC_BINARY="$MEMC_BINARY" # Let the user override the test with a path.

#  ;;

#  *)

#  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR

#for as_dir in $PATH

#do

#  IFS=$as_save_IFS

#  test -z "$as_dir" && as_dir=.

#    for ac_exec_ext in '' $ac_executable_extensions; do

#  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then

#    ac_cv_path_MEMC_BINARY="$as_dir/$ac_word$ac_exec_ext"

#    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5

#    break 2

#  fi

#done

#  done

#IFS=$as_save_IFS

 

#  test -z "$ac_cv_path_MEMC_BINARY" && ac_cv_path_MEMC_BINARY=""no""

#  ;;

#esac

#fi

#MEMC_BINARY=$ac_cv_path_MEMC_BINARY

#if test -n "$MEMC_BINARY"; then

#  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $MEMC_BINARY" >&5

#$as_echo "$MEMC_BINARY" >&6; }

#else

#  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5

#$as_echo "no" >&6; }

#fi

#

#

#           if test "x$MEMC_BINARY" = "xno"; then :

#  as_fn_error $? "\"could not find memcached binary\"" "$LINENO" 5

#fi

#

#fi

#

#fi

 

#cat >>confdefs.h <<_ACEOF

#define MEMCACHED_BINARY "$MEMC_BINARY"

#_ACEOF

4.  从以上代码中可以发现MEMCACHED_BINARY宏是需要在程序中用到的,此时需要手工将用到该宏的地方进行修改,如下: (请参见libtest/server.c,同时搜索字符串MEMCACHED_BINARY)

        if (x == 0)

        {

//            原有代码

//          snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u -m 128",

//                   MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);

//            修改为

            snprintf(buffer, sizeof(buffer), "-d -P %s -t 1 -p %u -U %u -m 128",

                     construct->pid_file[x], construct->port[x], construct->port[x]);

        }

        else

        {

//            原有代码

//          snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u",

//                   MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);

//            修改为

            snprintf(buffer, sizeof(buffer), "-d -P %s -t 1 -p %u -U %u",

                     construct->pid_file[x], construct->port[x], construct->port[x]);

        }

5. 将./libmemcached/options/server.h中的//#include <arpa/inet.h>代码注释掉,因为win32中没有该文件。

6. 由于最初已经决定不编译测试用例,因此需要将./libtest/server.c文件中的部分代码注释掉,我这里的做法是将部分和linux相关的code使用如下方式注释掉:(注:不能将该文件全部注释掉,因为其他的测试文件需要依赖这里声明的函数,如果全部注释,将会导致其他的测试文件无法正常链接)

#ifndef WIN32

static struct timespec global_sleep_value= { .tv_sec= 0, .tv_nsec= 50000 };

#endif

 

static void kill_file(const char *file_buffer)

{

#ifndef WIN32

... ...

#endif

}

 

void server_startup(server_startup_st *construct)

{

#ifndef WIN32

... ...

#endif

}

 

7. 修改./libmemcached/options/目录下面的三个文件parser.cc, parser.h, scanner.cc,这3个文件中均包含ERROR,TRUE,FALSE 和FLOAT等4个枚举的成员,由于和win32中的相应的宏冲突了,结果导致无法编译通过,特别还会report与yacc和lex相关的错误,我这里的做法是在这3个文件中,将所有和这四个枚举相关的地方均改为ERROR1,TRUE1,FALSE1和FLOAT1。(注意大小写敏感)

   //1. 该段代码位于parser.h

    enum yytokentype {

     COMMENT = 258,

     END = 259,

     ERROR1 = 260,

     ... ...

   &nbs

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,