undefined reference to std list during linking

This is a discussion on undefined reference to std list during linking within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a header file: Code: #ifndef _TX_SERVER_H #define _TX_SERVER_H #include<tx-types.h> #include<nssi_xdr.h> #include<list> #include "nssi_server.h" #define NUMCLIENTS 1 extern ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    undefined reference to std list during linking

    Hi,

    I have a header file:

    Code:
    #ifndef _TX_SERVER_H
    #define _TX_SERVER_H
    
    #include<tx-types.h>
    #include<nssi_xdr.h>
    #include<list>
    #include "nssi_server.h"
    
    #define NUMCLIENTS 1
    
    extern tx_info current_push_tx;
    extern nssi_service xfer_svc;
    extern nssi_remote_pid my_procid;
    
    extern std::list<client_response*> push_respond_list;
    extern std::list<client_response*> pull_respond_list;
    extern std::list<tx_info*> tx_history_list;
    
    void init_server();
    void push_transaction(
                          const unsigned long request_id,
                          const nssi_remote_pid *caller,
                          const push_tx_data *tx,
                          const nssi_rma *data_addr,
                          const nssi_rma *res_addr
                          );
    void process_transaction(
                             tx_info * info, tx_status STATUS
                             );
    #endif
    I am able to create the .o files with no problem. but when I can't link... for example, I get the following error...

    tx-server.cpp.text+0x1f): undefined reference to `tx_history_list'

    I get this for each one of the lists above.

    I have a file tx-server.cpp which includes tx-server.h

    Here are the calls I make for tx_history_list

    Code:
     for(list<tx_info*>::iterator it = tx_history_list.begin(); it != tx_history_list.end(); it++)
    and in another function

    Code:
    tx_history_list.push_front(info);
    in tx-server.cpp, I do have using namespace std

    Is there something I'm missing during the linking? Something specific to which I have to link?

    Currently, I am only linking to in-house libraries...

    Code:
    g++ -Wall -L $HOME/lib -L /ascldap/users/xxxx/lib/ -lcommandlineprocessor -lnx_server -lnx_client -lnx_support -lezxml -o tx-test tx-server.o tx-test.o
    I'm extremely new to C/C++, so this is a bit confusing for me.

    Thanks for the patience!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you actually make a tx_history_list? If so, where?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    245
    In the header file I just did this:

    extern std::list<tx_info*> tx_history_list;


    I'm guessing that means I just declared it, but did not instantiate it?

    I kind of followed this: http://www.yolinux.com/TUTORIALS/Lin...+STL.html#LIST

    Code:
    list<int> L;
     L.push_back(0);              // Insert a new element at the end
    L.push_front(0);             // Insert a new element at the beginning
     L.insert(++L.begin(),2);     // Insert "2" before position of first argu
    I'm guessing since I declared the list in the header file, I have to instantiate it differently? Not sure of the syntax to do that...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Basically yes. That line means "I claim that such an object exists somewhere, but It's Not My Job to make one." Which is all well and good, and it's what you have to have in a header file, since header files go everywhere. But at the end of the day, somebody has to make that object.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    245
    I see. I was following that example. I just see them doing list<int> L, then immediately start calling functions on L. I guess, what's the syntax then to instantiate a list that I declared in the header file...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just what you said.
    Code:
    std::list<tx_info*> tx_history_list;  //hey look it's a list
    But under no circumstances can you put this into a header file. It must be in a code file.
    It may sound crazy, but you might want to look into a book so that you know what all the words mean (like "extern").

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    245
    if I remove extern, I get this:

    tx-server.cpp:21: error: redefinition of ‘std::list<tx_info*, std::allocator<tx_info*> > tx_history_list’

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So for the love of all that is good, stop being an idiot. Which part of "you can't put this in a header file" is hard to understand?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    245
    considering I said you declare it in a headerfile and instantiate it in the .cpp file, then proceeded to say "the code is the same", which was not corrected...

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    245
    With Java you can do things like that.. declare them in some parts, and instantiate them in others. That's what I guess is confusing here.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dayalsoap View Post
    considering I said you declare it in a headerfile and instantiate it in the .cpp file, then proceeded to say "the code is the same", which was not corrected...
    Which post are you referring to? None of the posts that I can see in this thread have those words in them, or anything synonymous that I can find.

    When you see examples on the web that do different things in the header files and the code files, and then it is pointed out here that you do different things in the header files and the code files, and you yourself even claim to know the difference between declaring and instantiating, and then you don't do different things in the header files and the code files, then what exactly do you expect?

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    245
    me: I'm guessing since I declared the list in the header file, I have to instantiate it differently? Not sure of the syntax to do that...

    you: Basically yes.

    So you've said yes that I can declare it in the header file.

    me: I guess, what's the syntax then to instantiate a list that I declared in the header file...

    You: Just what you said.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    245
    me: I'm guessing since I declared the list in the header file, I have to instantiate it differently? Not sure of the syntax to do that...

    you: Basically yes.

    So you've said yes that I can declare it in the header file.

    me: I guess, what's the syntax then to instantiate a list that I declared in the header file...

    You: Just what you said.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So I agreed that you had to instantiate it differently. In no way did I say you can insta it in the header file. If you know anything about computers, then you yourself know (with or without me telling you) that you can't instantiate it in the header file.

    And then I agreed that the syntax to instantiate the list was exactly as you had posted in your post #3 and #5 -- which as you know was never in your program, for reasons known only to you -- and reemphasized the fact that you can't instantiate it in the header file.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    245
    "So I agreed that you had to instantiate it differently"

    Then why did you say yes to "I declare it in the header file, I have to instantiate it differently"

    When you say Yes, you are agreeing with two points. And I later discuss "declare in the header file" and you say "just what you said".

    No where, until after that, did you say "you can't declare objects in a header file like this"

    Which is also confusing, because this person declares his list in his headerfile

    Declaring a list in a header file - C++

    "If you know anything about computers, then you yourself know (with or without me telling you) that you can't declare it in the header file."

    C++ != computers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error: undefined reference
    By somedude in forum C++ Programming
    Replies: 16
    Last Post: 05-21-2009, 03:55 PM
  2. undefined Reference when linking a library
    By steve1_rm in forum C Programming
    Replies: 7
    Last Post: 03-12-2008, 05:34 PM
  3. undefined reference error while linking..help
    By rapture in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 03:54 AM
  4. Linking problem - undefined reference to load_parameters
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:28 AM
  5. Linking Error: Undefined Symbol
    By viclapurga in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2005, 05:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21