Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic C++ linker question

Options
  • 21-03-2006 5:25pm
    #1
    Registered Users Posts: 1,722 ✭✭✭


    How's it going everyone,
    I'm after running into a problem trying to get a class file to link using g++. I've spent an hour reading through the man pages and google and failed to get anything and I've tried just about everything I can think of to get around this. I've a fair bit of experience with C++ its just been ages since I've actually had to compile anything on the command line and I think I'm mixing up a few steps somewhere but I can't seem to remember it correctly. Anyway here's my code and the errors I'm getting.

    First file is a header for the linkErr class linkErr.h and is saved in a separate file
    using namespace std;
    
    class linkErr{
     public:
      linkErr();
    
      void bang();
    
    };
    

    Next file is called linkErr.c and is an implementation of the linkErr class
    #include <iostream>
    #include "linkErr.h"
    
    using namespace std;
    
    inline linkErr::linkErr(){
      cout << "hello I was made" << endl;
    }
    
    inline void linkErr::bang(){
      cout << "bang" << endl;
    }
    

    And the last file is a simple program to call the class called linkMain.c
    #include "linkErr.h"
    
    using namespace std;
    
    int main(){
      
      linkErr jack;
    
      jack.bang();
    
      return 1;
    }
    

    I'm using gcc 3.4.1 to compile them and I get no errors with the following command

    # g++ -c linkErr.c linkMain.c

    which gives me linkErr.o and linkMain.o

    then I try to link them using the following

    #g++ linkErr.o linkMain.o -o linkMain

    and I get the following error:

    linkMain.o(.text+0x24): In function `main':
    : undefined reference to `linkErr::linkErr()'
    linkMain.o(.text+0x33): In function `main':
    : undefined reference to `linkErr::bang()'
    collect2: ld returned 1 exit status

    I'm not sure what's going wrong, its been around 2 years since I've programmed in c++ so I'm sure I'm making a stupid mistake somewhere along here. Any help would really be apprecicated as this is killing me.


Comments

  • Registered Users Posts: 1,481 ✭✭✭satchmo


    You're compiling & linking fine alright, your problem is the 'inline' keyword.

    If you define a member function as inline when it's used by another file, the whole function definition must be in the header file, otherwise the linker won't be able to "see it" in order to inline it in that other file, and you'll get an unresolved external symbol or equivalent.

    You could put the whole function definition inside the class definition, so it's implicitly inlined and you don't need the keyword. Alternatively if you move the inline void linkErr::bang(){...} to the header file but outside the class definition, it'll link correctly (same with the ctor). But you can only specify it as inline and put it in the .cpp file if it will only be called by other code in that same file, since it won't be visible to any other code outside that translation unit.


  • Registered Users Posts: 1,722 ✭✭✭Thorbar


    Thanks for the help. You were right I took out the inlines and just put everything into one file, and saved it as a .h and compiled that. What's the correct way to separate the .ccp and .h files from each other without causing the linker to fall over?


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    No you have it right the way they're separated at the moment, just remove the inline keyword and it'll be fine. I meant either move the functions to the header file with the inline keyword still attached, or just remove the inline and leave them in the implementation (.c) file.

    In this case the latter makes more sense - unless you really need the inline keyword for performance reasons (ie inlining a small function that gets called thousands of times a second where the overhead of all the function calls approaches the cost of executing the function itself), I'd leave it out altogether. Besides, it's only a suggestion to the compiler which doesn't necessarily have to be honoured.

    You can read more about inlining here.


Advertisement