几个Windows到Linux的代码移植问题
1、在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 itoa char* itoa(int value, char* str, int radix) { int rem = 0; int pos = 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] = ''\0'' ; return strrev(str); } #endif
|
3、Windows到Linux关于__strrev的移植问题
因为在Linux系统中没有__strrev函数,那么将Windows代码移植到Linux系统时会有问题,本文下面描述一个技巧,在Linux中提供一个替代__strrev函数的方法。这里提供两个单独的实现:一个是普通的char* C函数使用的__strrev标准实现,另一个是针对STL的实现。两者的输入和输出仍然都是char*。
// // strrev 标准版 // #if !defined(__linux__) #define __strrev strrev #endif char* strrev(char* szT) { if ( !szT ) // 处理传入的空串. return ""; int i = strlen(szT); int t = !(i%2)? 1 : 0; // 检查串长度. for(int j = i-1 , k = 0 ; j > (i/2 -t) ; j-- ) { char ch = szT[j]; szT[j] = szT[k]; szT[k++] = ch; } return szT; } // // strrev 针对 STL 的版本. // char* strrev(char* szT) { string s(szT); reverse(s.begin(), s.end()); strncpy(szT, s.c_str(), s.size()); szT[s.size()+1] = ''\0''; return szT;
|
4、实现Sleep函数从Windows到Linux的移植
假设你有一些在Windows环境编写的代码,你想让它们在Linux环境下运行,条件是要保持对原有API署名的调用。比如在Windows中有Sleep,而在Linux中对应的函数是usleep,那么如何保持原有的函数名称调用呢?下面给出一段代码例子:
void Sleep(unsigned int useconds ) { // 1 毫秒milisecond) = 1000 微秒 microsecond). // Windows 的 Sleep 使用毫秒miliseconds) // Linux 的 usleep 使用微秒microsecond) // 由于原来的代码是在 Windows 中使用的,所以参数要有一个毫秒到微秒的转换。 usleep( useconds * 1000 ); }
|
评论暂时关闭