Thread: Undefined Reference to..

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    7

    Undefined Reference to..

    Hi,

    I'm trying to compile a simple program using CodeBlocks 8.02, mainly 3 files: main.cpp, matriz.h and matriz.cpp. However, I keep getting an "undefined reference error". I ve been looking for a solution on Internet and any of them fixes the problem. So far, I tried this:
    - Using WinMain instead of main().
    - Main with parameters.
    - Using END_OF_MAIN().
    and many more....

    Please HELP!!!

    Thanks.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Undefined reference means that your linker couldn't find a particular function. Link in the object file or library containing that function.

    Can't tell you much more. What was the error?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Trying random things to fix an error is not the way to handle an error.

    Next time give the full error and an idea of what you're actually doing. What you told is not that helpful other than to tell you that it's a linker error.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    Thanks for answering. Ok, the error appears for some strange reason, everything seems to be working fine I change some lines and then the error appears, so I change it back to its original state and the error does not disappear.

    It says undefined reference to every function declared on matriz.h.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Sounds like you're not linking both main.cpp and matriz.cpp.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    I pretty sure it is a CodeBlocks problem, any idea of how to fix it?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Exactly what I previously said. Check to make sure your project is compiling and linking both .cpp files.

    This reminds me of an original Knight Rider episode where Michael asks Bonnie if KITT is all ready after she was working on him. Bonnie replies that KITT is OK except for one factor: the nut behind the wheel.

    Case in point, don't blame your tools right away.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    Haha, I' ve been looking for a solution for a while now, that 's why I'm looking for help .

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    1. Make sure that all of the files that you have are included in the project, both debug and release.
    2. If you declare a function in a header file as inline, but have defined it in a .cpp file, move it into the header file.
    3. Make sure your function arguments match everywhere.
    4. Delete any object files that Code::Blocks has been using and "rebuild."

    These have been my most common problems.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    I ve tried all of them..unfortunately the problem is not fixed.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    With such vivid descriptions of your problem, it must be our fault.

    If you want help with the exact details of your IDE, you have to give us more to work on about what you are doing or not doing.

    This is, btw, why I think newbies should not be allowed to touch an IDE until they have a better idea of what it actually does. Getting lost in how to communicate to the linker.... Bleh.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    Ok.

    main.cpp
    Code:
    #include <iostream>
    #include "Matriz.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        Matriz a;
        a.Imprimir();
      
        return 0;
    }
    matriz.h

    Code:
    #ifndef MATRIZ_H
    #define MATRIZ_H
    #include <iostream>
    
    using namespace std;
    
    class Matriz
    {
    	private:
    
    
        int ** matriz;
        int filas;
        int columnas;
      public:
    		Matriz();
    		void Imprimir();
    };
    
    
    #endif // MATRIZ_H
    matriz.cpp

    Code:
    #include "Matriz.h"
    
    Matriz::Matriz()
    {
      filas=4;
      columnas=4;
       matriz = new int * [filas];
       for(int i=0; i<filas; i++){
         matriz [i] = new int [columnas];
       for(int j=0; j<columnas; j++)
           matriz[i][j]=0;
       }
    
    
    }
    
    void Matriz::Imprimir(){
        for(int i=0 ; i<filas ; i++){
          for(int j=0 ; j<columnas ; j++)
            printf("%5d",matriz[i][j]);
          cout<<endl;
        }
        cout<<"------------------------------"<<endl;
    }

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is not a description of the errors you are getting. Nor, for that matter, is it a complete description of your setup. Code::Blocks will let you open as many files as you want. It won't put them in a project for you unless you specify such. Have you done so? Are you trying to build a single file or build your project?

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    It is a project, containing those 3 files. It is a project and then I added a class, and then I used include statement in order to use it.

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