加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

几个Windows到Linux的代码移植难题

发布时间:2023-06-19 12:59:40 所属栏目:Linux 来源:
导读:几个Windows到Linux的代码移植难题:1、在Linux实现Win32 API之GetTickCount函数为了将Windows中的GetTickCount API函数移植到Linux,可以使用如下的代码:long GetTickCount() { tms tm; return times(&tm); } 2、W

几个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] = '''' ; 

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] = ''''; 

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 ); 

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!