c++ - 当我似乎包含正确的标题时,找不到符号错误

标签 c++ c header-files bison flex-lexer

我希望这还没有被回答,很难阅读类似的问题,因为它们似乎都是关于 Xcode 中的 iOS 开发。我正在编写一个解析器,需要在我的语法 (.y) 和词法分析器 (.l) 文件中包含一个 header 。最初只包含标题而不定义函数会产生重复符号错误。我阅读了一些关于 C++ 的内容,并尝试制作一个头文件和一个 cpp 文件。现在我收到了一个symbol(s) not found error。我想我只需要比我更了解 C++ 的人的帮助。此外,我正在慢慢地将此代码从 C 转换为 C++,因此现在有一些非 C++ 的处理方式。

确切的错误是:

Undefined symbols for architecture x86_64:
  "symlook(char*)", referenced from:
      yylex() in lex.yy.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [outfile] Error 1

符号表.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NSYMS 20

struct symtab
{
    char *name;
    // ptr to C function to call if this entry is a fucntion name
    double (*funcptr)();
    double value;
};

extern symtab table[];

struct symtab *symlook(char *s);
void addfunc(char *name, double (*func)());

symtab.cpp

#include "symtab.h"

symtab table[NSYMS];

struct symtab *symlook(char *s) {
    char *p;
    struct symtab *sp;

    for(sp = table; sp < &table[NSYMS]; sp++) {
        /* is it already here? */
        if(sp->name && !strcmp(sp->name, s))
            return sp;
        if(!sp->name) { /* is it free */
            sp->name = strdup(s);
            return sp; 
        }
        /* otherwise continue to next */
    }
    fprintf(stderr, "%s\n", "Too many symbols");
    exit(1);    /* cannot continue */
} /* symlook */

void addfunc(char *name, double (*func)())
{
    struct symtab *sp = symlook(name);
    sp->funcptr = func;
}

简单.l

%{
#include "y.tab.h"
#include <math.h>
#include "symtab.h"
%}

%%

"if"    return IF;

"true"  return TRUE;
"false" return FALSE;

([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
        yylval.dval = atof(yytext); 
        return NUMBER; 
}

[ \t] ;  /* ignore whitespace */
[A-Za-z][A-Za-z0-9]*   { 
        yylval.symp = symlook(yytext);
        return NAME; 
}

"$"     return 0; /* logical EOF */

\n      |
.       return yytext[0];
%%

简单.y

%{
    #include "symtab.h"
    #include "ast.h"
    #include <string>
    #include <stdio.h>
    int yylex(void);
    void yyerror(char *);
%}

%union {
    double dval;
    int ival;
    struct symtab *symp;
}

%token <symp> NAME
%token <ival> INTEGER_LITERAL
%token <dval> NUMBER
%token IF TRUE FALSE WHILE
%left '-' '+'
%left '*' '/'
%nonassoc UMINUS

%type <dval> num_expression
%%

statement_list: statement '\n'
    |   statement_list statement '\n'
    ;

statement:  NAME '=' num_expression  { $1->value = $3; }
    |       num_expression           { fprintf(stderr, "= %g\n", $1); }
    |       if_expression
    |       bool_expression
    |       while_expression
    ;

num_expression: num_expression '+' num_expression   { $$ = $1 + $3; }
    |       num_expression '-' num_expression       { $$ = $1 - $3; }
    |       num_expression '*' num_expression       { $$ = $1 * $3; }
    |       num_expression '/' num_expression   
                { 
                    if($3 == 0.0)
                        yyerror("divide by zero");
                    else
                        $$ = $1 / $3;
                }
    |       '-' num_expression %prec UMINUS { $$ = -$2; }
    |       '(' num_expression ')'          { $$ = $2; }
    |       NUMBER                      
    |       NAME                            { $$ = $1->value; }
    |       NAME '(' num_expression ')'     { } //$$ = ($1->funcptr($3)); }
    ;
bool_expression: FALSE
    |            TRUE
    ;

if_expression:   IF '('  bool_expression ')' '{' statement '}'
    |            IF '(' ')' '{' '}'
    ;

while_expression: WHILE '('  bool_expression ')' '{' statement '}'
    |             WHILE '(' ')' '{' '}'
    ;

%% 

void yyerror(char *str)
{
    fprintf(stderr, "Error %s", str);
}


int main() {
    yyparse();
    return 0;
}

生成文件

CC=g++
outfile=simple
lexfile=simple
yaccfile=simple

outfile: compile
    ${CC} -o ${outfile}.out lex.yy.o y.tab.o -ll

compile: build
    ${CC} -c symtab.cpp
    ${CC} -c lex.yy.c y.tab.c -ll


build:
    yacc -d ${yaccfile}.y
    flex ${lexfile}.l

最佳答案

您的 compile: build 步骤会生成一个文件 symtab.o,您没有将其包含在链接行中。

关于c++ - 当我似乎包含正确的标题时,找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051235/

相关文章:

c++ - 无法执行 approxPolyDP()

c++ - 从 cmd 和 Codeblocks 执行时输出不同

C插入链表,第一个和最后一个节点的问题

c - 如何将二进制文件转换为包含整数和字符的文本文件

c - 将 *next 列表指针设置为 NULL

C++:通过简单的添加示例了解头文件和头文件保护

c++ - 导出 DLL - 删除头文件

c++ - 是否有标准库函数可以反转 STL 堆栈?

c - 如何将固件文件存储到头文件 (.h)

c++ - SQLITE:如何在 UPDATE 语句中使用 SELECT 语句更新表?