I’m totally new to flex.
I’m getting a build error when using flex. That is, I’ve generated a .c file using flex, and, when running it, am getting this error:
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
here is the lex file I’m using (grabbed from 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;
}
As well, why do I have to put a ‘main’ function in the lex syntax code? What I’d like is to be able to call yylex(); from another c file.
Q1 Link Error
It looks a bit as if something is confused about the isatty() function. It doesn’t show in the code you show – but it might be referenced in the code generated by flex. If so, it appears that you are compiling with a C++ compiler, and the isatty() function is being treated as a function with C++ linkage and is not being found – it is normally a function with C linkage and would need to be declared with ‘
extern "C" int isatty(int);‘ in C++ code.To resolve, track down whether
isatty()appears in the generated C. If so, also track down where it is declared (the POSIX standard header for it is<unistd.h>).Q2 Main
You do not have to put the main program in the file with the lexer. Indeed, you often won’t do that, or the main program in there will simply be a dummy used to test the lexer in isolation (and only compiled into the code conditionally – inside
#ifdef TEST / #endifor equivalent).What makes you think that you have to do it?