本文介绍了未定义的参考错误,即使函数存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然未定义参考错误问题已被问过几次,我找不到解决我的问题。



我想让flex和bison在Qt应用程序中工作。

这些是相关文件 -



1) icd .l - Lex规范 - 它生成 iclexer.c 并包含函数的定义 -

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

2) iclexer.h

  #ifndef ICLEXER_H 
#define ICLEXER_H

extern void yyerror 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
#includeiclexer.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
}
void parse()
{
yyparse();
}
〜Parser()
{
clearLexer();
}
};
#endif // ICPARSER_H

4) icd.y - Bison规范 - 它生成 icparser.cpp 并包含函数的定义 -

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

在我的GUI代码中,我包括 icparser.h 并使用 class Parser 的接口。但我收到以下错误 -

类似的错误setUpBuffer (char const * text); tearDownBuffer(void * buffer); nextToken(); 。基本上 iclexer.c 中的函数未正确链接。



请告诉我如何修改此代码或如何更改链接顺序如果生成的头文件是完全一样的,即没有任何

解决方案

> externC那么你可能面临的问题是C编译器将在编译函数时生成一个未汇编的符号,但是C ++编译器会生成一个依赖的名字。链接器将看到依赖项和符号,但没有相同的名称,它将无法将它们匹配在一起。



查看用于的工具的文档生成代码并找到如何生成C ++兼容的代码(通常只需要在头中包含额外的 externC代码)。


Although "Undefined Reference error" questions have been asked several times, I couldn't find solution to my problem.

I am trying to get flex and bison to work in a Qt app. I am facing problem in linking only.

These are the relevant files-

1) icd.l - Lex specification - It generates iclexer.c and contains definition of functions-

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 specification - It generates icparser.cpp and contains definition of functions-

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

In my GUI code I include icparser.h and use class Parser's interface. But I am getting following error-

And similar errors for setUpBuffer(char const* text);, tearDownBuffer(void* buffer); and nextToken();. Basically the functions in iclexer.c are not getting linked properly.

Please tell me how to either modify this code or how to change linking order in Qt (Qt Creator)?

解决方案

If the generated header file is exactly as stated, i.e. without any extern "C" then the problem you might be facing is that the C compiler will generate an unmangled symbol when compiling the function, but the C++ compiler will generate a dependency to the mangled name. The linker will see the dependency and the symbol, but not having the same name it will fail to match them together.

Take a look at the documentation of the tool used to generate the code and find how to generate C++ compatible code (usually just requires the inclusion of extra extern "C" code in the headers).

这篇关于未定义的参考错误,即使函数存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:14