基于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
staticstruct timespec global_sleep_value= { .tv_sec=0, .tv_nsec=50000 };
#endif

staticvoid kill_file(constchar*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,
... ...
TRUE1
=315,
FALSE1
=316,
FLOAT1
=317,
... ...
}

//2. 该段代码位于parser.cc
enum yytokentype {
COMMENT
=258,
END
=259,
ERROR1
=260,
... ...
TRUE1
=315,
FALSE1
=316,
FLOAT1
=317,
... ...
}

staticconstchar*const yytname[] =
{
"$end", "error", "$undefined", "COMMENT", "END", "ERROR1",
... ...
"TRUE1", "FALSE1", "','", "'='", "FLOAT1", ... ..., 0
};

//3. 该段代码位于scanner.cc
case44:
YY_RULE_SETUP
#line 133 "libmemcached/options/scanner.l"
{ yyextra
->begin= yytext; return ERROR1; }
YY_BREAK
case45:
YY_RULE_SETUP
#line 135 "libmemcached/options/scanner.l"
{
return TRUE1; }
YY_BREAK
case46:
YY_RULE_SETUP
#line 136 "libmemcached/options/scanner.l"
{
return FALSE1; }
YY_BREAK

8. 注释掉./clients/memslap.c中的如下代码//#include <sys/mman.h>,因为该文件为linux中内存管理的文件,win32并未提供相应的文件,因此可以直接注释掉。

9. 最后修改./tests/parser.cc中和access相关的3个函数(access是linux中文件相关的函数),其中的#ifndef WIN32是新加的。

test_return_t memcached_create_with_options_with_filename(memcached_st*)
{
#ifndef WIN32
if (access(SUPPORT_EXAMPLE_CNF, R_OK))
return TEST_SKIPPED;

memcached_st
*memc_ptr;
memc_ptr
= memcached(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""));
test_true_got(memc_ptr, memcached_last_error_message(memc_ptr));
memcached_free(memc_ptr);
#endif

return TEST_SUCCESS;
}

test_return_t libmemcached_check_configuration_with_filename_test(memcached_st
*)
{
#ifndef WIN32
if (access(SUPPORT_EXAMPLE_CNF, R_OK))
return TEST_SKIPPED;

memcached_return_t rc;
char buffer[BUFSIZ];

rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"support/example.cnf\""), buffer, sizeof(buffer));
test_true_got(rc
== MEMCACHED_SUCCESS, buffer);

rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=support/example.cnf"), buffer, sizeof(buffer));
test_false_with(rc
== MEMCACHED_SUCCESS, buffer);

rc
= libmemcached_check_configuration(STRING_WITH_LEN("--CONFIGURE-FILE=\"bad-path/example.cnf\""), buffer, sizeof(buffer));
test_true_got(rc
== MEMCACHED_ERRNO, buffer);
#endif
return TEST_SUCCESS;
}

test_return_t test_include_keyword(memcached_st
*)
{
#ifndef WIN32
if (access(SUPPORT_EXAMPLE_CNF, R_OK))
return TEST_SKIPPED;

char buffer[BUFSIZ];
memcached_return_t rc;
rc
= libmemcached_check_configuration(STRING_WITH_LEN("INCLUDE \"support/example.cnf\""), buffer, sizeof(buffer));
test_true_got(rc
== MEMCACHED_SUCCESS, buffer);
#endif
return TEST_SUCCESS;
}

10. 记着编译成功后make install.

原文地址:https://www.cnblogs.com/orangeform/p/2084882.html