【问题标题】:unistd.h related difficulty when compiling bison & flex program under vc++在 vc++ 下编译 bison 和 flex 程序时 unistd.h 相关的困难
【发布时间】:2011-02-17 02:46:54
【问题描述】:

我在 vc++ 中使用 bison 和 flex(通过 cygwin 下载)。当我编译程序时出现错误:

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

flex生成的文件中对应的代码是:

#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

如果我在 flex 文件 (.l) 中定义 YY_NO_UNISTD_H,此错误将消失,但我会收到其他几个错误:

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

我该如何解决这个问题?

所有这些错误都发生在 flex 生成的扫描器中。

我知道这是因为 unistd.h 在 Windows 中不存在。我必须编写自己的 unistd.h 吗?如果是这样,如何编写以消除这些错误?

【问题讨论】:

    标签: visual-c++ cygwin bison flex-lexer


    【解决方案1】:

    isatty 被词法分析器用来确定输入流是终端还是管道/文件。词法分析器使用此信息来更改其缓存行为(词法分析器在不是终端时读取输入的大块)。如果您知道您的程序永远不会以交互式方式使用,您可以将%option never-interactive 添加到您的词法分析器中。当程序使用用户输入运行时,使用%option interactive。当两种用途都需要时,您可以生成交互式词法分析器,在批处理模式下使用时会降低性能,或者提供您自己的 isatty 函数。

    【讨论】:

      【解决方案2】:

      在您的.l 文件中使用%option nounistd 以消除对unistd.h 的依赖。

      【讨论】:

      • 我在文档中找到了它,但它对我不起作用。 unrecognized %option unistd(尽管我指定了 nounistd,所以它似乎试图解决它。有什么想法吗?在手册中:flex.sourceforge.net/manual/…
      【解决方案3】:

      使用带有选项 --wincompat 的 win_flex.exe,您无需破解您的 lex 文件

      【讨论】:

        【解决方案4】:

        以防万一有人仍然存在这个问题,Flex 在其开发文件中带有 unistd.h。 我在这里找到了这个:

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

        简而言之,只要确保您的编译器可以访问它。在我的情况下,它只是将“C:\GnuWin32\include”添加到其他包含目录

        【讨论】:

          【解决方案5】:

          unistd.h 是 UNIX 标头,因此在 VC++ 中不存在;您最好的选择可能是在 Cygwin(或 mingw/msys)中使用 g++ 编译它。您还可以查看this question 以获取其他建议。

          【讨论】:

            【解决方案6】:

            我正在使用来自 GnuWin32 项目的 flex 2.5.4,它不检查 YY_NO_UNISTD_H

            在我的版本中,Flex 仅在编译为 C++ 时才查找 unistd.h,因此如果您的 yylval 不使用任何 C++ 结构,您就可以省去所有这些麻烦。

            我必须在 yylval 中使用 STL(使用指针使其成为 POD 类型),所以为了在 C++ 中进行 flex 编译,我创建了这个简单的 unistd.h

            #include <io.h>
            

            仅此而已(实际上,我可以复制 GnuWin32 附带的 unistd.h 文件,例如 flyontheweb suggests)。

            附:总结一下:在 Bison 中,我将 yylval 所需的 STL 头文件放在 %code requires {} 中,并将当前目录添加到我的 makefile 中的 INCLUDE 路径中。

            【讨论】:

            • 那个版本也有类似的问题
            【解决方案7】:

            我为时已晚,但无论如何我会分享我的发现以拯救仍在寻找答案的人。 在我的情况下,在编译器查找头文件的位置有一个空的 unistd.h 文件对我有用。

            【讨论】:

              【解决方案8】:

              好吧,这篇文章很旧,但我面临同样的问题,这里应该可以解决问题。 WinFlexBison

              【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-08-13
              • 1970-01-01
              • 2013-06-12
              相关资源
              最近更新 更多