unistd.h related difficulty when compiling bison &

2020-06-07 04:39发布

I'm using bison & flex (downloaded via cygwin) with vc++. When I compile the program I got an error:

...: fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory

The corresponding code in the flex-generated file is:

#ifndef YY_NO_UNISTD_H
/* Special case for "unistd.h", since it is non-ANSI. We include it way
 * down here because we want the user's section 1 to have been scanned first.
 * The user has a chance to override it with an option.
 */
/* %if-c-only */
#include <unistd.h>
/* %endif */
/* %if-c++-only */
/* %endif */
#endif

If I define YY_NO_UNISTD_H in the flex file(.l) this error will disappear, but I get several other errors:

...: error C2447: '{' : missing function header (old-style formal list?)
...: warning C4018: '<' : signed/unsigned mismatch
...: error C3861: 'isatty': identifier not found

How can I fix this problem?

All these errors occur in the flex-generated scanner.

I know it's because unistd.h doesn't exist in windows. Do I have to write my own unistd.h? If so how to write it in order to eliminate those errors?

8条回答
不美不萌又怎样
2楼-- · 2020-06-07 05:03

unistd.h is a UNIX header, so it's not present in VC++; your best bet is probably to compile it using g++ in Cygwin (or mingw/msys). You could also look at this question for other suggestions.

查看更多
劳资没心,怎么记你
3楼-- · 2020-06-07 05:10

Use %option nounistd in your .l file to remove the dependence on unistd.h.

查看更多
祖国的老花朵
4楼-- · 2020-06-07 05:13

just in case somebody's still this problem, Flex comes with unistd.h within its devel files. I found this here:

http://sourceforge.net/tracker/index.php?func=detail&aid=931222&group_id=23617&atid=379173

to put it short, just make sure your compiler can reach it. in my case it's just adding "C:\GnuWin32\include" to the additional inclusion directories

查看更多
ゆ 、 Hurt°
5楼-- · 2020-06-07 05:20

use win_flex.exe with option --wincompat and you dont need to hack your lex file

查看更多
萌系小妹纸
6楼-- · 2020-06-07 05:20

Well this post is old but I face the same problem and here is something that should work. WinFlexBison

查看更多
可以哭但决不认输i
7楼-- · 2020-06-07 05:21

isatty is used by the lexer to determine if the input stream is a terminal or a pipe/file. The lexer uses this information to change its caching behavior (the lexer reads large chunks of the input when it is not a terminal). If you know that your program will never be used in an interactive kind, you can add %option never-interactive to you lexer. When the program is run with user input, use %option interactive. When both uses are desired, you can either generate an interactive lexer, which gives a performance loss when used in batch mode, or provide your own isatty function.

查看更多
登录 后发表回答