How do I remove the following 'implicit declaration of function' warnings?

六月ゝ 毕业季﹏ 提交于 2019-12-01 15:17:36
paxdiablo

Neither strdup nor fileno are ISO C functions, they're part of POSIX.

Now whether they're available on your platform depends on your platform.


If you are using the Microsoft tools, you may want to look into _fileno for the latter (fileno was deprecated in VC2005). A rather excellent version of strdup can be found here.

Although, having blown my own horn with that code, you could also use _strdup since it replaces the also-deprecated strdup :-)

These should hopefully work okay as-is since they're in stdio.h and string.h, two of the include files you're already using.


If you're on a UNIX derivative, those functions should be available in stdio.h (for fileno) and string.h (for strdup). Given that it looks like you're already including those files, the problem is likely elsewhere.

One possibility is if you're compiling in one of the strict modes like __STRICT_ANSI__ in gcc), where neither would be defined.

You should have a look at the top of your generated lex.yy.c and lex.l files to confirm that the header files are being included and also check the command line parameters you're passing to the compiler.

I suggest this option (tell the compiler you are using POSIX):

#define _POSIX_C_SOURCE 1

People seem to have tightened up the feature controls in recent years and hopefully when the consistency is good and widespread we can throw away the automake garbage.

Consider adding the following line:

extern char *strdup(const char *s);

I faced the problem when I compiled with -std=c99 -pedantic -pedantic-errors. Adding the above line solved the problem for me.

I also had this problem while using flex.

I used -std=gnu99rather than -std=c99 which solved the problem.

flex lang.l && gcc -o lexer -std=gnu99 lex.yy.c -lfl                         

You declare the function before you use it:

//declare the function
int storeLexeme();

//use the function here

or include the header where the function is declared.

C implicitly assumes undeclared functions have return type int and deduces the parameters from how you call the function. This is deprecated in C++.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!