Undefined, although functions are present

Even though the "Undefined Reference error" questions have been asked several times, I could not find a solution to my problem.

I am trying to get flex and bison to work in a Qt application. I only ran into a problem in communication.

These are the relevant files -

1) icd.l - Lex specification - generates iclexer.c and contains function definitions -

void yyerror(char const *s){...}
void* setUpBuffer(char const* text){...}
void tearDownBuffer(void* buffer){...}
int nextToken(){...}

      

2) iclexer.h

#ifndef ICLEXER_H
#define ICLEXER_H

extern void  yyerror(char const *);
extern int   yylex();
extern char* yytext;
extern void* setUpBuffer(char const* text);
extern void  tearDownBuffer(void* buffer);
extern int nextToken();

#endif // ICLEXER_H

      

3) icparser.h

#ifndef ICPARSER_H
#define ICPARSER_H
#include "iclexer.h"
#include <string>

extern void initLexer(std::string const& t);
extern void clearLexer();
extern int yyparse();

class Parser
{
public:
    Parser(std::string const& s)
    {
        initLexer(s);
    }
    void parse()
    {
        yyparse();
    }
    ~Parser()
    {
        clearLexer();
    }
};
#endif // ICPARSER_H

      

4) icd.y - Bison spec - generates icparser.cpp and contains function definitions -

void initLexer(std::string const& t);
void clearLexer();
int yylex();

      

In my GUI code, I include icparser.h and use the interface class Parser

. But I am getting the following error -

/home/vinayak/codes/qt/dic_sim/icparser.cpp:1402:
error: undefined reference to `yyerror (char const *) '

And similar errors for setUpBuffer(char const* text);

, tearDownBuffer(void* buffer);

and nextToken();

. Basically iclexer.c functions are not linked properly.

Please tell me how to change this code or how to change the binding order in Qt (Qt Creator)?

+2


source to share


2 answers


If the generated header file exactly matches the declared one, i.e. without any extern "C"

, then the problem you may run into is that the C compiler generates an untangled symbol when the function is compiled, but the C ++ compiler will generate a dependency on the mangled name. The compiler will see the dependency and symbol, but does not have the same name so it cannot match them.



Take a look at the documentation of the tool used to generate the code and find out how to generate C ++ compatible code (it usually just needs to include additional code extern "C"

in the headers).

+1


source


As for yours error: undefined reference to 'yyerror(char const*)'

, you can read the Bison documentation, more specifically the part about yyerror . It reads:

— Method on parser: void error (const location_type& l, const std::string& m)

The definition for this member function must be supplied by the user: the parser
uses it to report a parser error occurring at l, described by m.

      

This definition is probably missing from your code. Note that yyerror

in fact yy::parser::error

, with the yy::

default namespace for the parser (which can be changed with %define namespace "foo"

) and the parser::

default parser class name (which can be changed with %define parser_class_name foo

).

Your other errors are not related to Bison.



Edit: oh, you seem to be generating C parsers, not C ++ parsers. Well, it's the same for C parsers: the yyerror is user supplied .

In simple programs, the following definition is sufficient:

 void
 yyerror (char const *s)
 {
   fprintf (stderr, "%s\n", s);
 }

      

+2


source







All Articles