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
|