Welcome

首页 / 软件开发 / C++ / 几个Windows到Linux的代码移植问题

几个Windows到Linux的代码移植问题2011-04-25 vckbase Northtibet1、在 Linux 实现 Win32 API 之 GetTickCount 函数

为了将 Windows 中的 GetTickCount API 函数移植到 Linux,可以使用如下的代码:

long GetTickCount()
{
tms tm;
return times(&tm);
}

2、Windows 和 Linux 系统关于 itoa 的移植问题

大家知道,在将 Windows 的 STL 代码移植到 Linux 系统时,由于 Linux 系统中 STL 没有实现默认的itoa 函数,因此 itoa 在 Linux 中无法正常工作。要是在 GCC 命令行禁用 STL 的话,那么代码里就无法使用 STL,从而丢失可移植性。这里给出一个 简单可行的解决 方法,以便你碰到这种情况时顺利进行从 Windows 到 Linux 的移植:

#if defined(__linux__)#define _itoa itoachar* itoa(int value, char*str, int radix){intrem = 0;intpos = 0;char ch= ""!"" ;do{rem= value % radix ;value /= radix;if ( 16 == radix ){if( rem >= 10 && rem <= 15 ){switch( rem ){case 10:ch = ""a"" ;break;case 11:ch =""b"" ;break;case 12:ch = ""c"" ;break;case 13:ch =""d"" ;break;case 14:ch = ""e"" ;break;case 15:ch =""f"" ;break;}}}if( ""!"" == ch ){str[pos++] = (char) ( rem + 0x30 );}else{str[pos++] = ch ;}}while( value != 0 );str[pos] = """" ;return strrev(str);}#endif