Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My main.hpp looks like this :

#include "json.tab.h"
#include "ob.hpp"

extern Ob *ob;

In my test.l, I wrote :

%{
    #include "main.hpp"
%}


%token  KEY
%token  COMMA

%%
KEY_SET         : KEY                                                 {ob->someOp();}
                | KEY_SET COMMA KEY                                   {ob->someOp();}
%%

But this gives me :

C:......c:(.text+0x37a): undefined reference to `ob'
C:......c:(.text+0x38e): undefined reference to `Ob::someop()'

So how can I call that Ob object from anywhere in my parser?

My Ob class(Ob.hpp) :

#include <bits/stdc++.h>
using namespace std;

#ifndef O_H_
#define O_H_

using namespace std;

class Ob {
public:
    Ob();
    ~Ob();
    someop();
};

#endif /*O_H_*/

And Ob.cpp:

Ob::someop()
{
    cout << "c++ is lit" << endl;
}

Now I have made all method in Ob static, so that no need to have an instance. And my build rule looks something like this :

g++ lex.yy.c test.tab.c main.cpp *.hpp -o test.exe

And I made the parser generator plain, without any method call, and it works fine, no error, no warning :

%%
KEY_SET         : KEY     
                | KEY_SET COMMA KEY    
%%

And when I added {Ob::someOp();}, then it gives me the same error again.

All of my codes are here : https://gist.github.com/maifeeulasad/6d0ea58cd70fbe255a4834eb46f2e1fd

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
213 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should pass all the the .cpp files, and no .hpp, to the compile command. The .hpp will be automatically included by the preprocessor. If you don't do it (you are not including Ob.cpp in your command), then it can't find the definition of the functions contained in them.

So your compilation command should be this:

g++ lex.yy.c test.tab.c main.cpp Ob.cpp -o test.exe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...