Linker errors in Dev C++

This is a discussion on Linker errors in Dev C++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I experience i weird problem with the linker in DEV C++ that drives me crazy.. Any help would be ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Linker errors in Dev C++

    Hi,
    I experience i weird problem with the linker in DEV C++ that drives me crazy..
    Any help would be grateful.. Here we go..

    I have defined a class named Date, an instance of which is declared in another class named MainNode and is given the name "date".In the file MainNode.cpp i define some functions that are applied to the Date object. Each cpp file is compiled ok alone, but when i compile the whole project i SOMETIMES get the following linker error:

    [Linker error] Undefined reference to 'MainNode::date'

    I am most sure that the same code is compiled but i don't always get the above error.

    I know that i don't help you much with the above description, below are 2 screenshots that might help you..
    Thanks in advance!
    Attached Images Attached Images   

  2. #2
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    I am not the most experienced programmer here, but it seems to me that since MainNode contains and object of type Date that you might need to #include Date.h before MainNode.h
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    MainNode.h already includes Date.h, so that should not be a problem.

    Have all the source files been compiled?

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by Dark_Phoenix
    I am not the most experienced programmer here, but it seems to me that since MainNode contains and object of type Date that you might need to #include Date.h before MainNode.h
    Order doesn't matter since it's all compiled, not interpreted.

    Did you add all the files you need to your project, and that they are not just in the same folder? Perhaps, show the header and cpp for MainNode and Date. Or it might be Dev-C++ just acting weird, since you said it picks up the error sometimes.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Sometimes, after updating code, I need to "rebuild" in order to get the linker working right. If that doesn't solve it, then it's probably the code.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    All the files i use are in the same directory.
    The compilation of each single cpp file is successful.
    Below are the MainNode and Date header and cpp files..
    I hope it's not another one of the rookie mistakes that i usually do.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    In your image above, date was a regular member variable, but in the files above, it is a static member variable. It is the static that is causing the linker error. If you want it to be a static variable, you must define the single instance of date in your cpp file.

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Daved, please, if you could expand on that. I am having a similar problem as tezcatlipooca (with the undefined references). I have a class with some static members that are set by a static memeber function. It seems, however, that no matter what I do, the linker will not see a declaration. Here's some of my code:
    Code:
    //in file_manager.h
    class file_manager
    {
    public:
            enum {TAKEN = false, AVAILABLE = true};
            static std::ofstream * bad_writer;
            static std::ifstream * bad_reader;
            static void set_bad_pointers();
            file_manager();
            ~file_manager();
    //so on and so forth.......
    //.................
    };
    Then, in that class's source file "file_manager.cpp"
    Code:
    #include "file_manager.h"
    //..............
    file_manager::file_manager() : reader(), writer(), ws(AVAILABLE), rs(AVAILABLE)
    {
     //every time file_manager() is called, these statics are reinitialized. Fine.
     set_bad_pointers();
    }
    
    file_manager::~file_manager()
    {
     if(ws == TAKEN || rs == TAKEN) throw unsafe_destruction();
    }
    //.................
    void file_manager::set_bad_pointers()
    {
     bad_reader = reinterpret_cast<std::ifstream*>(0);
     bad_writer = reinterpret_cast<std::ofstream*>(0);
    }
    and in the main file "main.cpp"
    Code:
    #include "file_manager.h"
    
    int main(int argc, char *argv[])
    {
        file_manager fman;
        std::ofstream * osptr;
        file_manager::set_bad_pointers();   //even though file_manager() called it already...
        
        if((osptr = fman.aquire_writer()) == file_manager::bad_writer)
        {
         std::cout << "Couldn't aquire ofstream pointer" << std::endl;
         return EXIT_SUCCESS;
        }
    //...................
        std::system("PAUSE");
        return EXIT_SUCCESS;
    }
    And my grand total number of linking errors:
    Code:
      [Linker error] undefined reference to `file_manager::bad_writer'
      [Linker error] undefined reference to `file_manager::bad_reader'
      [Linker error] undefined reference to `file_manager::bad_writer' 
      [Linker error] undefined reference to `file_manager::bad_reader' 
      [Linker error] undefined reference to `file_manager::bad_writer' 
      [Linker error] undefined reference to `file_manager::bad_writer' 
      [Linker error] undefined reference to `file_manager::bad_reader' 
      [Linker error] undefined reference to `file_manager::bad_writer'
    Any help is much appreciated.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You have to define a non-const static member variable once in a source file so that it can be linked to. This is also where you initialize them (or set them to 0 if you don't have anything to initialize them with). So in a cpp file you would do something like this:
    Code:
    std::ofstream * file_manager::bad_writer = 0;

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Ok, I did that in file_manager.cpp but got the same error.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Oh, now I see. Putting it in main.cpp, which uses them solved the issue. Is there anyway to have this happen "behind-the-scenes" so as to avoid the overhead in main.cpp?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There should be no difference between putting it in file_manager.cpp or main.cpp. Are you compiling and linking file_manager.cpp with main.cpp?

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Yes -- file_manager.cpp includes file_manager.h, as does main.cpp. They are included in the same project and are compiled and linked together using Bloodshed.
    I wonder why the linker doesn't see the definition when it's in file_manager.cpp .

    Fortunately, it no longer matters because I have drawn a new method that doesn't use static members, and it's better, anyway. Still, it's a strange issue.

    Thanks for your help.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker errors.
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2009, 03:18 AM
  2. Linker errors with 2005 Express
    By thetinman in forum Windows Programming
    Replies: 4
    Last Post: 12-30-2006, 09:04 AM
  3. Linker errors when compiling
    By The Wazaa in forum C++ Programming
    Replies: 4
    Last Post: 10-07-2006, 01:55 PM
  4. dev & allegro compile errors
    By Calgore in forum C++ Programming
    Replies: 10
    Last Post: 08-11-2004, 05:41 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 10:38 AM

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