可移植性/兼容性问题 [英] Portability / compatibility issues

查看:164
本文介绍了可移植性/兼容性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在家里开发一些基本的字符串/文件操作C程序

(Sparcstation运行Solaris 9和gcc)用于我的工作环境(PA-RISC

运行HP -UX 11.11和c99兼容编译器)。但是我似乎已经在执行最基本的操作时遇到了问题和

比较。


我已经发布了我的测试代码下面,以及我在每个编译器上得到的结果。

问题的关键在于''strstr''函数在不同平台上的

行为有很大不同,实际上,宝贵的少数手册页将提供任何功能的实例,更不用说这个了。我不得不

写下has_slash。功能和使用已保留的工作SLASH,

解决strstr()给我的问题。


我知道C被吹捧为标准化开发提供

便携性的环境(不朽的短语我们通过键入

''make''来转换到另一个平台,让人想起;我现在没有看到。


任何建议/评论?分享和享受,Ian


#include< stdio.h>


#define SLASH" /"


int has_slash(char in_str [24])

{

int i = 0;


while(in_str [i]!= * SLASH&& in_str [i]!= NULL&& i< 100)i ++;


/ *格式错误* /

if(in_str [i]!= * SLASH)

{

i = 0;

} else < br $>
{i ++;

}

printf(" In function - i =%i \ n",i);

返回(i);


}


void main()

{

int test_true;


/ *同时考虑PA_RISC和SPARC * /

test_true = has_slash(" copy / f file") ;

if(test_true!= 0)printf(" Has slash = true \ n");


/ *新测试,strstr结果* /

char * str_result;

char test_date [24];


strcpy(test_date," 02_04_2005"); < b r />
if(strstr(test_date,SLASH)!= NULL)printf("通过自我发现

slash \ n");

if(strstr(test_date,SLASH)== NULL)printf(" By self- not found

slash \

n");


str_result = strstr(test_date," /");


if(str_result!= NULL)printf(" Found slash:%s:!\ n",str_result);

if(str_result == NULL)printf (未找到斜杠:%s:!\ n,

str_result);

}

/ * server1:PA_RISC with c99兼容编译器* /

/ * server1:/ tmp> ./a.out

功能 - i = 6

斜线=真

自发未斜线

未找到斜杠::!

server1:/ tmp> * /


/ *服务器2:带有gcc的SPARC * /

/ *#。/ a.out

功能 - i = 6

斜线= true

自发未斜线

分段故障 - 核心倾倒



/

I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the ''strstr'' function differs wildly in its
behaviour on different platforms, and precious few man pages will actually
provide a worked example of any function, let alone this one. I have had to
write the "has_slash" function and use the reserved work SLASH already, to
get around the problems that strstr() gives me.

I know C is touted as an standardised development environment that provides
portability (the immortal phrase "we converted to another platform by typing
''make''" springs to mind); I''m not seeing that right now.

Any advise / comments? Share and Enjoy, Ian

#include <stdio.h>

#define SLASH "/"

int has_slash(char in_str[24])
{
int i=0;

while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++;

/* wrong format */
if (in_str[i] != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);

}

void main()
{
int test_true;

/* PROVEN ON BOTH PA_RISC AND SPARC */
test_true=has_slash("copy /f file");
if (test_true != 0) printf("Has slash = true\n");

/* New test, strstr results */
char *str_result;
char test_date[24];

strcpy(test_date, "02_04_2005");
if (strstr(test_date, SLASH) != NULL) printf(" By self- found
slash\n");
if (strstr(test_date, SLASH) == NULL) printf(" By self- not found
slash\
n");

str_result=strstr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */

/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/

推荐答案

OzBob写道:
我正在开发一些基本的字符串/文件操作C在家里运行
(Sparcstation运行Solaris 9和gcc)用于我的工作环境(PA-RISC运行HP-UX 11.11和c99兼容编译器)。但是我似乎在执行最基本的操作和比较时遇到了问题。

我在下面发布了我的测试代码,以及我在每个编译器上得到的结果。问题的关键在于''strstr''函数在不同平台上的行为差异很大,而且很少有人工页实际上会提供任何函数的工作示例,让独自一人。我不得不写has_slash。功能和使用已保留的工作SLASH,以解决strstr()给我的问题。

我知道C被吹捧为提供可移植性的标准化开发环境(不朽的短语我们通过键入
''make''来转换到另一个平台;我现在没有看到。

任何建议/评论?分享和享受,Ian



是:使用最高警告级别和标准C

模式进行编译。例如

I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the ''strstr'' function differs wildly in its
behaviour on different platforms, and precious few man pages will actually
provide a worked example of any function, let alone this one. I have had to
write the "has_slash" function and use the reserved work SLASH already, to
get around the problems that strstr() gives me.

I know C is touted as an standardised development environment that provides
portability (the immortal phrase "we converted to another platform by typing
''make''" springs to mind); I''m not seeing that right now.

Any advise / comments? Share and Enjoy, Ian

Yes: Compile with the highest warning level and in a standard C
mode. E.g.


gcc -std = c99 -pedantic -W -Wall -O strstr.c -c

strstr.c:在函数`has_slash''中:

strstr.c:9:警告:指针和整数之间的比较

strstr.c:顶级:

strstr.c:24:警告:返回类型''main''不是'int''

strstr.c:在函数`main'':

strstr.c:35:警告:隐式声明函数`strcpy''

strstr.c:36:警告:隐式声明函数`strstr''

strstr .c:36:错误:缺少终止角色

strstr.c:37:错误:在程序中迷路''\''

strstr.c:37:错误:`slash''未声明(首次使用)在这个函数中)

strstr.c:37:错误:(每个未声明的标识符只报告一次

strstr.c:37:错误:对于它出现的每个函数。)

strstr.c:37:错误:在n之前解析错误

strstr.c:36:警告:if语句中的空体

strstr.c:37:错误:缺少终止字符

strstr.c:38:错误:'n''未声明(首次使用此函数)

strstr.c:38:错误:缺少终止字符

strstr.c:39:错误:缺少终止字符


其中include指令在第1行。


你的程序没有编译 - 所以你怎么能指望我们

看看它吗?


干杯

Michael
#include< stdio.h>

#define SLASH" /"

int has_slash(char in_str [24])
{i / 0;

(in_str [i]!= * SLASH&& in_str [i]!= NULL&& i< 100)i ++;

/ *格式错误* /
如果(in_str [i]!= * SLASH)
{
i = 0;
}其他
{i ++;
}
printf(&功能 - i =%i \ n",i);
返回(i);

}

void main()
{
int test_true;

/ *同时测试PA_RISC和SPARC * /
test_true = has_slash(" copy / f file");
if(test_true!= 0)printf(具有斜线=真\\ n \\ n};);

/ *新测试,strstr结果* /
char * str_result;
char test_date [24];

strcpy( test_date," 02_04_2005");
if(strstr(test_date,SLASH)!= NULL)printf("通过自我发现
slash \ n;;
if(strstr(test_date,SLASH)== NULL)printf(自我未找到
slash \
n");

str_result = strstr(test_date," /");

if(str_result!= NULL)printf(" Found slash:%s :!\ n",str_result);
if(str_result == NULL)printf(未找到斜杠:%s:!\ n,
str_result);
}
/ * server1:带有c99兼容编译器的PA_RISC * /
/ * server1:/ tmp> ./a.out
在功能 - i = 6
有斜线=真
由自己没有找到斜线
未找到斜线::!
server1:/ TMP> * /

/ *服务器2:SPARC与gcc * /
/ *#。/ a.out
在功能 - i = 6
有斜线=真
自我未找到斜线
分段错误 - 核心倾倒

/
gcc -std=c99 -pedantic -W -Wall -O strstr.c -c
strstr.c: In function `has_slash'':
strstr.c:9: warning: comparison between pointer and integer
strstr.c: At top level:
strstr.c:24: warning: return type of ''main'' is not `int''
strstr.c: In function `main'':
strstr.c:35: warning: implicit declaration of function `strcpy''
strstr.c:36: warning: implicit declaration of function `strstr''
strstr.c:36: error: missing terminating " character
strstr.c:37: error: stray ''\'' in program
strstr.c:37: error: `slash'' undeclared (first use in this function)
strstr.c:37: error: (Each undeclared identifier is reported only once
strstr.c:37: error: for each function it appears in.)
strstr.c:37: error: parse error before "n"
strstr.c:36: warning: empty body in an if-statement
strstr.c:37: error: missing terminating " character
strstr.c:38: error: `n'' undeclared (first use in this function)
strstr.c:38: error: missing terminating " character
strstr.c:39: error: missing terminating " character

where the include directive is on line 1.

Your program does not compile -- so how can you expect us to
look at it?

Cheers
Michael
#include <stdio.h>

#define SLASH "/"

int has_slash(char in_str[24])
{
int i=0;

while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++;

/* wrong format */
if (in_str[i] != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);

}

void main()
{
int test_true;

/* PROVEN ON BOTH PA_RISC AND SPARC */
test_true=has_slash("copy /f file");
if (test_true != 0) printf("Has slash = true\n");

/* New test, strstr results */
char *str_result;
char test_date[24];

strcpy(test_date, "02_04_2005");
if (strstr(test_date, SLASH) != NULL) printf(" By self- found
slash\n");
if (strstr(test_date, SLASH) == NULL) printf(" By self- not found
slash\
n");

str_result=strstr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */

/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/



-

电子邮件:我的是/ at / gmx / dot / de地址。


--
E-Mail: Mine is an /at/ gmx /dot/ de address.




" Michael Mair" <弥********** @ invalid.invalid>在消息中写道

news:42 ************* @ individual.net ...

"Michael Mair" <Mi**********@invalid.invalid> wrote in message
news:42*************@individual.net...
OzBob写道:
我在家里开发一些基本的字符串/文件操作C程序
(Sparcstation运行Solaris 9和gcc)用于我的工作环境(PA-RISC运行HP-UX 11.11和c99兼容编译器) 。但是我似乎在执行最基本的操作和比较时遇到了问题。

我在下面发布了我的测试代码,以及我在每个编译器上得到的结果。问题的关键在于''strstr''函数在不同平台上的行为差异很大,而且很少有人工页面会实际提供任何函数的工作示例,让独自一人。我不得不写has_slash。功能和使用保留的工作
SLASH已经解决了strstr()给我的问题。

我知道C被吹捧为标准化的开发环境,它提供了可移植性(不朽的短语我们通过键入''make'''转换到另一个
平台;我现在没有看到这个。

任何建议/评论?分享和享受,Ian
I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the ''strstr'' function differs wildly in
its behaviour on different platforms, and precious few man pages will
actually provide a worked example of any function, let alone this one. I
have had to write the "has_slash" function and use the reserved work
SLASH already, to get around the problems that strstr() gives me.

I know C is touted as an standardised development environment that
provides portability (the immortal phrase "we converted to another
platform by typing ''make''" springs to mind); I''m not seeing that right
now.

Any advise / comments? Share and Enjoy, Ian



是:使用最高警告级别和标准C />模式进行编译。例如


Yes: Compile with the highest warning level and in a standard C
mode. E.g.


这篇关于可移植性/兼容性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆