gcc错误:未定义的对“itoa”的引用

时间:2022-11-23 20:35:55

if I include stdlib.h then also itoa() is not recognized. My code :

如果我包括stdlib。然后,也不承认itoa()。我的代码:

%{
#include "stdlib.h"
#include <stdio.h>
#include <math.h>
int yylex(void);
char p[10]="t",n1[10];
int n ='0';

%}
%union
{
char *dval;
}
%token ID
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%type <dval> S
%type <dval> E
%%
S : ID '=' E {printf(" x = %sn",$$);}
;
E : ID {}
| E '+' E {n++;itoa(n,n1,10);printf(" %s = %s + %s ",p,$1,$3);strcpy($$,p);strcat($$,n1);}
| E '-' E {n++;itoa(n,n1,10);printf(" %s = %s – %s ",p,$1,$3);strcpy($$,p);strcat($$,n1);}
| E '*' E {n++;itoa(n,n1,10);printf(" %s = %s * %s ",p,$1,$3);strcpy($$,p);strcat($$,n1);}
| E '/' E {n++;itoa(n,n1,10);printf(" %s = %s / %s ",p,$1,$3);strcpy($$,p);strcat($$,n1);}
;
%%

main()
{
yyparse();
}

int yyerror (char *s)


{


}

After running the code I got :

运行代码后,我得到:

gcc lex.yy.c y.tab.c -ll
12.y: In function ‘yyparse’:
12.y:24: warning: incompatible implicit declaration of built-in function ‘strcpy’
12.y:24: warning: incompatible implicit declaration of built-in function ‘strcat’
12.y:25: warning: incompatible implicit declaration of built-in function ‘strcpy’
12.y:25: warning: incompatible implicit declaration of built-in function ‘strcat’
12.y:26: warning: incompatible implicit declaration of built-in function ‘strcpy’
12.y:26: warning: incompatible implicit declaration of built-in function ‘strcat’
12.y:27: warning: incompatible implicit declaration of built-in function ‘strcpy’
12.y:27: warning: incompatible implicit declaration of built-in function ‘strcat’
/tmp/ccl0kjje.o: In function `yyparse':
y.tab.c:(.text+0x33d): undefined reference to `itoa'
y.tab.c:(.text+0x3bc): undefined reference to `itoa'
y.tab.c:(.text+0x43b): undefined reference to `itoa'
y.tab.c:(.text+0x4b7): undefined reference to `itoa'

Where am I going wrong ? Why cant it find the reference to itoa ? I have also tried with <> brackets for itoa.

我哪里出错了?为什么它不能找到对itoa的引用呢?我还尝试了itoa的<>括号。

2 个解决方案

#1


9  

itoa is a non-standard function which is supported by some compilers. Going by the error, it's not supported by your compiler. Your best bet is to use snprintf() instead.

itoa是一个非标准函数,由一些编译器支持。根据错误,编译器不支持它。最好的方法是使用snprintf()。

#2


2  

I use sprintf in following way:

我使用sprintf:

#include <stdio.h>

char *my_itoa(int num, char *str)
{
        if(str == NULL)
        {
                return NULL;
        }
        sprintf(str, "%d", num);
        return str;
}

int main()
{
        int num = 2016;
        char str[20];
        if(my_itoa(num, str) != NULL)
        {
                printf("%s\n", str);
        }
}

I hope this will save someone's time ;)

我希望这能挽救某人的时间。

#1


9  

itoa is a non-standard function which is supported by some compilers. Going by the error, it's not supported by your compiler. Your best bet is to use snprintf() instead.

itoa是一个非标准函数,由一些编译器支持。根据错误,编译器不支持它。最好的方法是使用snprintf()。

#2


2  

I use sprintf in following way:

我使用sprintf:

#include <stdio.h>

char *my_itoa(int num, char *str)
{
        if(str == NULL)
        {
                return NULL;
        }
        sprintf(str, "%d", num);
        return str;
}

int main()
{
        int num = 2016;
        char str[20];
        if(my_itoa(num, str) != NULL)
        {
                printf("%s\n", str);
        }
}

I hope this will save someone's time ;)

我希望这能挽救某人的时间。