代码之家  ›  专栏  ›  技术社区  ›  Cam

flex(lex,不是actionscript或其他)错误

  •  0
  • Cam  · 技术社区  · 15 年前

    我对灵活是全新的。

    使用flex时出现生成错误。也就是说,我已经使用flex生成了一个.c文件,当运行它时,会收到以下错误:

    1>lextest.obj : error LNK2001: unresolved external symbol "int __cdecl isatty(int)" (?isatty@@YAHH@Z)
    1>C:\...\lextest.exe : fatal error LNK1120: 1 unresolved externals
    

    这是我使用的lex文件(从 here ):

    /*** Definition section ***/
    
    %{
    /* C code to be copied verbatim */
    #include <stdio.h>
    %}
    
    /* This tells flex to read only one input file */
    %option noyywrap
    
    
    %%
        /*** Rules section ***/
    
        /* [0-9]+ matches a string of one or more digits */
    [0-9]+  {
                /* yytext is a string containing the matched text. */
                printf("Saw an integer: %s\n", yytext);
            }
    
    .       {   /* Ignore all other characters. */   }
    
    %%
    /*** C Code section ***/
    
    int main(void)
    {
        /* Call the lexer, then quit. */
        yylex();
        return 0;
    }
    

    另外,为什么我必须在lex语法代码中放置一个“main”函数?我想要的是能够从另一个C文件调用YYLEX();

    2 回复  |  直到 12 年前
        1
  •  5
  •   Jonathan Leffler    15 年前

    Q1链路错误

    看起来好像对isatty()函数有点困惑。它不会显示在您所显示的代码中,但它可能在flex生成的代码中被引用。如果是这样的话,看来你正在编译一个C++编译器,并且ISATTY()函数被视为一个C++链接的函数,它没有被发现——它通常是一个带有C链接的函数,需要用“ extern "C" int isatty(int); 在C++代码中。

    要解决,请跟踪 isatty() 出现在生成的C中。如果是,还应跟踪声明它的位置(它的posix标准头为 <unistd.h> )

    Q2主

    您不必使用lexer将主程序放入文件中。实际上,您通常不会这样做,或者其中的主程序只是一个用于单独测试lexer的虚拟程序(并且只在内部有条件地编译到代码中) #ifdef TEST / #endif 或等同的)。

    是什么让你觉得你必须这么做?

        2
  •  -1
  •   Chris Dodd    15 年前

    链接错误似乎是您试图在Windows上使用非Windows本机版本的flex,但它无法正常工作。如果使用Cygwin附带的flex版本,则需要编译该程序并将其与Cygwin的编译器和链接器链接。