Thread: Linking error: undefined reference

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    11

    Linking error: undefined reference

    Before you completely ignore this, I have read the dozens of other people posting this error and their solutions, but I have no idea how to fix this. Because some libraries were given to us, and cannot be modified, I'm finding it difficult to diagnose the problem. Yes this is for an assignment, but the closing stages of it.

    I've attached the entire program, I'm a little desperate Nothing malicious. When you unzip it, run make search in the dir.

    The program should compile correctly, and then throws the linking error.

    Big thanks in advance,
    dude.

    Parser.zip
    Last edited by somedude; 05-21-2009 at 09:48 AM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Try adding

    -L /path/to/the/directory/containing/the/library

    to gcc/ld command.

    [edit]
    by the way, if you must attach files, use the attachment feature of the board. Chances are no one will go through all the hassle of downloading from rapidshare (all the countdowns and stuff) just to help you.
    [/edit]
    Last edited by cyberfish; 05-21-2009 at 09:35 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    11
    You aren't allowed to upload any sort of compressed file. But I'll upload it to another forum. One sec.

    Also, there isn't anything wrong with my linking of the library, it knows where it is, it's just one linking error after everything else worked.

    I will post the new download link in a minute.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And I suppose that if I try to do "make search", it probably won't compile on my Windows machine...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by somedude View Post
    You aren't allowed to upload any sort of compressed file.
    If you call it something.txt instead of something.zip then you can. Just let everybody know so they can rename the extension after downloading.

    Also, what OS is this for, what IDE/compiler? And what specific linker error is it showing you?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    11
    Sorry yes, this is for unix machines, or those with access to unix via putty.

    I've already uploaded to another forum.

    Oh yeah...the only error I've yet to clean up is:

    main.cc:(.text._ZN11NodeFactory14getNodeFactoryEv[NodeFactory::getNodeFactory()]+0x4): undefined reference to `NodeFactory::nodeFactory'

    Okay, that specific error comes from the uni unix machines, compiled on terminal, running Red Hat, gcc-4.1.2.

    ld: library not found for -lparser is the error I get on my Mac, gcc-4.0.2
    Last edited by somedude; 05-21-2009 at 11:27 AM.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    11
    I just realised that I omitted a .h file.

    I've reuploaded the corrected one. Link above also corrected.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you actually have something in NodeFactory called nodeFactory?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    11
    Quote Originally Posted by matsp View Post
    Do you actually have something in NodeFactory called nodeFactory?

    --
    Mats
    Code:
    class NodeFactory {
            // one will be needed so there is no point in deferring the creation
            // cf. Parser
            static NodeFactory nodeFactory;
    protected:
            // hidden constructor so nothing can create a NodeFactory
            NodeFactory(void) { }
    public:
            // this is the only means to get hold of the NodeFactory instance
            static NodeFactory *getNodeFactory(void) { return &nodeFactory; }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And I take it that you have declared your static variable somewhere else?

    Remember, static variables in classes are just there to say "there is such a thing, somewhere". You then have to declare the type classname::variable somewhere in a .cpp file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    11
    Quote Originally Posted by matsp View Post
    And I take it that you have declared your static variable somewhere else?

    Remember, static variables in classes are just there to say "there is such a thing, somewhere". You then have to declare the type classname::variable somewhere in a .cpp file.

    --
    Mats
    I can't change these classes, given as part of the assignment. That is Parser.h, NodeFactory.h

    Code:
    class Parser {
    	// although a Parser will be needed deferring the creation for illustrative purposes
    	// cf. NodeFactory
    	static Parser *parser;
    	static NodeFactory *nodeFactory;
    protected:
    	// hidden constructor so nothing can create a Parser
    	Parser(NodeFactory *nf) { nodeFactory = nf; };
    public:
    	// this is the only means to get hold of the Parser instance
    	static Parser& getParser(NodeFactory *);
    Other instances of NodeFactory:

    Code:
    #include "parser/Parser.h"
    #include "subNode.h"
    
    using namespace std;
    
    int main() {
    	Tokeniser tokeniser(cin);
    	Parser & parser = Parser::getParser(NodeFactory::getNodeFactory());
    	Node * query = parser.query(tokeniser);
    
    	Context acontext;
    	Hits result = (*query)(& acontext);
    	cerr << * query << endl;
    	return 0;
    }

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by somedude View Post
    I can't change these classes, given as part of the assignment. That is Parser.h, NodeFactory.h
    You're screwed, then. The instructor should have provided an object file or library which contains the definition of that static member. You could define it yourself, but that's seriously improper.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    11
    Quote Originally Posted by brewbuck View Post
    You're screwed, then. The instructor should have provided an object file or library which contains the definition of that static member. You could define it yourself, but that's seriously improper.
    Stupid me. I misread the instructions. Two other directories aren't copied, parser is okay.

    What do I need to change to fix this?

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by somedude View Post
    Stupid me. I misread the instructions. Two other directories aren't copied, parser is okay.

    What do I need to change to fix this?
    Like I said, if this static member is part of a class that the instructor is providing, then there should be an object file (.obj or .o) or a lib file (.lib or .a) also provided with the headers, that defines the symbol.

    The alternative is to define it yourself, in one of your source files:

    Code:
    NodeFactory NodeFactory::nodeFactory;
    But this is conceptually WRONG, and will cause a linker error on the instructor's test system if he/she actually does provide this definition. You need to find out for sure.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to provide the static variable with a location, which you do by declaring a variable in some .cpp file for NodeFactory::nodeFactory; And you probably need the Parser:arser and Parser::nodeFactory too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM