GIZA++.2003-09-30版本的VC2003移植方法

18 views
Skip to first unread message

Jacky

unread,
Sep 22, 2006, 9:55:06 AM9/22/06
to 语言技术中心论坛
1.安装STLPORT
GIZA++是在GNU C++的环境要求下写的.
采用了其中的标准模版库.
GNU C++的标准模版库是采用目前公认的最好的SGI STL.
由于STL有很多个实现版本,关于这方面的说明,请看http://jjhou.csdn.net/myan-stlport.htm
各个版本之间存在差异,为了减少麻烦,我们先安装SGI
STL的VC版本
具体做法参见http://jjhou.csdn.net/myan-stlport.htm
其中描述的是VC6.0的方法,大同小异.
更详细的请参见stlport自带的readme和install
我采用的是stlport-4.6.2, 用其中的src\vc71.mak
进行编译和安装

2.建立VC工程
不要忘了添加stl的include路径和lib路径
关闭使用precompiled header
以及开启异常支持 /GX 和多线程 /MT

3.稍微修改一点代码
这个主要是因为VC对C++语言的支持不完善造成的.
这一点MS已经受了好多批评.
下面的修改临时弄的,有些地方很拙劣,可以修改得更漂亮一点.

统一把文件后缀名由.C或.cc改成.cpp

把下列文件里的<stream.h>改成<iostream.h>
general.h
StarVar.h
FixedArray.h
SAOptimization.cpp
mkcls.cpp
StatVar.cpp


mystl.h
在#include <cmath>下一行
添加
using namespace std;

文件general.h

inline string operator&(const string&a,const string&b)
上面一行添加
using namespace std;


文件general.cpp

extern "C" {
#include <sys/time.h>
#include <sys/resource.h>
}
去掉
在void myerror(int line,const char *file,const char *expression)
上面一行添加
#ifdef WIN32
#include <windows.h>
#endif
把 srand48(z);改成srand(z);
把 drand48(); 改成rand();
把 clockSec函数改成下面的内容
double clockSec()
{
#ifndef WIN32
#ifdef linux
enum __rusage_who who=RUSAGE_SELF;
#else
int who=RUSAGE_SELF;
#endif
struct rusage rusage;
getrusage(who, &rusage);
return rusage.ru_utime.tv_sec+rusage.ru_utime.tv_usec/1000000.0;
#else

__int64 creation64, exit64, kernel64, user64;// in 100 nano
seconds

int rc = GetProcessTimes (GetCurrentProcess(),
(FILETIME *) &creation64,
(FILETIME *) &exit64,
(FILETIME *) &kernel64,
(FILETIME *) &user64);
return (unsigned long)(kernel64/10000000)+(unsigned
long)(user64/10000000);

#endif

}

把StatVar.h文件里面的#include <values.h>改成#include "values.h"
在当前目录创建values.h文件
内容如下
/* Old compatibility names for <limits.h> and <float.h> constants.
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be
useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not,
write to the Free Software Foundation, Inc., 59 Temple Place -
Suite 330,
Boston, MA 02111-1307, USA. */

/* This interface is obsolete. New programs should use
<limits.h> and/or <float.h> instead of <values.h>. */

#ifndef _VALUES_H
#define _VALUES_H 1

#include <limits.h>
#ifdef __cplusplus
extern "C" {
#endif

#ifdef MAXSHORT
#undef MAXSHORT
#endif
#ifdef MAXFLOAT
#undef MAXFLOAT
#endif

#define _TYPEBITS(type) (sizeof (type) * CHAR_BIT)

#define CHARBITS _TYPEBITS (char)
#define SHORTBITS _TYPEBITS (short int)
#define INTBITS _TYPEBITS (int)
#define LONGBITS _TYPEBITS (long int)
#define PTRBITS _TYPEBITS (char *)
#define DOUBLEBITS _TYPEBITS (double)
#define FLOATBITS _TYPEBITS (float)

#ifndef MINSHORT
# define MINSHORT SHRT_MIN
#endif
#ifndef MININT
# define MININT INT_MIN
#endif
#ifndef MINLONG
# define MINLONG LONG_MIN
#endif

#ifndef MAXSHORT
# define MAXSHORT SHRT_MAX
#endif
#ifndef MAXINT
# define MAXINT INT_MAX
#endif
#ifndef MAXLONG
# define MAXLONG LONG_MAX
#endif

#define HIBITS MINSHORT
#define HIBITL MINLONG


#include <float.h>

#define MAXDOUBLE DBL_MAX
#define MAXFLOAT FLT_MAX
#define MINDOUBLE DBL_MIN
#define MINFLOAT FLT_MIN
#define DMINEXP DBL_MIN_EXP
#define FMINEXP FLT_MIN_EXP
#define DMAXEXP DBL_MAX_EXP
#define FMAXEXP FLT_MAX_EXP


#ifdef __USE_MISC
/* Some systems define this name instead of CHAR_BIT or CHARBITS. */
# define BITSPERBYTE CHAR_BIT
#endif
#ifdef __cplusplus
}
#endif

#endif /* values.h */

把PopOptimization.h里面的
inline DEFINE_STANDARD_COMPARE(ProbAndOpt)
改成
inline DEFINE_STANDARD_COMPARE(ProbAndOpt);

KateProblemsTest.cpp
原文
int fromCatFile(KategProblem *p,const char *fname,bool verb=1)
{
leda_h_array<string,int> translation(-1);
改成
int fromCatFile(KategProblem *p,const char *fname,bool verb)
{
verb=1;
leda_h_array<string,int> translation(-1);

对于文件
GDAOptimization.cpp
KateProblem.h
KateProblem.cpp
把里面的pow和log函数的参数全部强制类型转换为(double)

mkcls.cpp
KateProblemKBC.cpp
把里面的<<endl;全部换成<<"\n";

在KateProblemKBC.cpp的
#include "KategProblem.h"的后面一行添加
#include "erf.h"


在当前目录创建新文件erf.h和erf.cpp
内容分别如下
erf.h
double erf(double x);
double erfc(double x);

erf.cpp
#include <iostream.h>
#include <iomanip.h>
#include <strstream.h>
#include <math.h>

#include "erf.h"
#if 1
#define signbit __signbit
__inline int __signbit(long double fe)
{ return (sizeof(long double) == sizeof(double))
? ((short *)&(fe))[3] & 0x8000
: ((short *)&(fe))[4] & 0x8000;
}
#else
#define signbit(fe) (sizeof(fe) == sizeof(float) ? \
(int)(((short *)&(fe))[1] & 0x8000) : \
(sizeof(fe) == sizeof(double) ? (int)(((short *)&(fe))[3] &
0x8000) \
: (int)(((short *)&(fe))[4] & 0x8000)) \
)
#endif
static const double rel_error= 1E-12; //calculate 12 significant
figures
//you can adjust rel_error to trade off between accuracy and speed
//but don't ask for > 15 figures (assuming usual 52 bit mantissa in a
double)


double erf(double x)
//erf(x) = 2/sqrt(pi)*integral(exp(-t^2),t,0,x)
// = 2/sqrt(pi)*[x - x^3/3 + x^5/5*2! - x^7/7*3! + ...]
// = 1-erfc(x)
{
static const double two_sqrtpi= 1.128379167095512574; // 2/sqrt(pi)
if (fabs(x) > 2.2) {
return 1.0 - erfc(x); //use continued fraction when fabs(x) > 2.2
}
double sum= x, term= x, xsqr= x*x;
int j= 1;
do {
term*= xsqr/j;
sum-= term/(2*j+1);
++j;
term*= xsqr/j;
sum+= term/(2*j+1);
++j;
} while (fabs(term/sum) > rel_error); // CORRECTED LINE
return two_sqrtpi*sum;
}


double erfc(double x)
//erfc(x) = 2/sqrt(pi)*integral(exp(-t^2),t,x,inf)
// = exp(-x^2)/sqrt(pi) * [1/x+ (1/2)/x+ (2/2)/x+ (3/2)/x+ (4/2)/x+
...]
// = 1-erf(x)
//expression inside [] is a continued fraction so '+' means add to
denominator only
{
static const double one_sqrtpi= 0.564189583547756287; // 1/sqrt(pi)
if (fabs(x) < 2.2) {
return 1.0 - erf(x); //use series when fabs(x) < 2.2
}
if (signbit(x)) { //continued fraction only valid for x>0
return 2.0 - erfc(-x);
}
double a=1, b=x; //last two convergent numerators
double c=x, d=x*x+0.5; //last two convergent denominators
double q1, q2= b/d; //last two convergents (a/c and b/d)
double n= 1.0, t;
do {
t= a*n+b*x;
a= b;
b= t;
t= c*n+d*x;
c= d;
d= t;
n+= 0.5;
q1= q2;
q2= b/d;
} while (fabs(q1-q2)/q2 > rel_error);
return one_sqrtpi*exp(-x*x)*q2;
}

在mystl.h的最后面添加
char *strlwr(char *s);
int strcasecmp(const char *s1, const char *s2);

在当前目录创建新文件mystl.cpp
内容如下
#include <string.h>
#include <stdlib.h>
#include "mystl.h"
char *strlwr(char *s)
{
if (s != NULL)
{
char *p;

for (p = s; *p; ++p)
*p = tolower(*p);
}
return s;
}
int strcasecmp(const char *s1, const char *s2)
{
char *tmp1,*tmp2;
tmp1=(char *)malloc(strlen(s1)+1);
tmp2=(char *)malloc(strlen(s2)+1);
strcpy(tmp1,s1);
strcpy(tmp2,s2);
return strcmp( strlwr(tmp1), strlwr(tmp2) );
}

Reply all
Reply to author
Forward
0 new messages