0

I am trying to declare two classes C1 and C2 in files nstest1.h and nstest2.h which are defined in files nstest1.cpp and nstest2.cpp respectively. Both the classes are defined under same namespace.

Following are the files :

//nstest1.h
namespace Mine{
    class C1{
        public:
        void callme();
    };
}

//nstest2.h
namespace Mine {
    class C2 {
        public:
            void callme();
    };
}

//nstest1.cpp
#include<iostream>
#include "nstest1.h"

using namespace std;
using namespace Mine;

void Mine::C1::callme(){
    std::cout << "Please call me " << std::endl;
}

//nstest2.cpp
#include<iostream>
#include "nstest2.h"

using namespace std;
using namespace Mine;

void Mine::C2::callme(){
    std::cout << "Please call me too" << std::endl ;
}

Following file tries to use this classes using namespace Mine.

//nstest.cpp
#include<iostream>
#include "nstest1.h"
#include "nstest2.h"

using namespace std;
using namespace Mine;

int main(){
    Mine::C1 c1;
    Mine::C2 c2;
    c1.callme();
    c2.callme();
    return 0;
}

When I compile using command "g++ nstest.cpp", I get following error :

/tmp/cc2y4zc6.o: In function `main':
nstest.cpp:(.text+0x10): undefined reference to `Mine::C1::callme()'
nstest.cpp:(.text+0x1c): undefined reference to `Mine::C2::callme()'
collect2: error: ld returned 1 exit status

If the definitions are moved to the declaration files (nstest1.h and nstest2.h), it works fine. Not sure whats happening here. Am I missing something ? Thanks in advance :) .

Chandrahas
  • 325
  • 4
  • 14
  • 1
    Compile with `g++ nstest1.cpp nstest2.cpp nstest.cpp`. You need to compile all source files. – James Moore Sep 23 '15 at 19:59
  • Thanks :). It works fine for this case. But is it possible to avoid compiling declarations and definitions every-time ? – Chandrahas Sep 23 '15 at 20:08
  • The only way would be to include the definition inside the header file. The compiler needs to see the implementation. You can also use a makefile to automatically compile source files, [example](http://stackoverflow.com/questions/231229/how-to-generate-a-makefile-with-source-in-sub-directories-using-just-one-makefil/2484343#2484343), then all you have to do is call `make` to compile everything. – James Moore Sep 23 '15 at 20:12
  • You can make a library from your definitions and link to it rather than compiling it every time. Or even just link to the `.o` files. – Galik Sep 23 '15 at 20:15
  • Thanks James :) It seems writing a makefile will be helpful here. – Chandrahas Sep 23 '15 at 20:16

2 Answers2

3

You need to include the other .cpp files when building the program.

Option 1: Compile all the files and build the executable in one command

g++ nstest.cpp nstest1.cpp nstest2.cpp -o nstest

Option 2: Compile each file separately and then build the executable after that

g++ -c nstext1.cpp
g++ -c nstest2.cpp
g++ -c nstest.cpp
g++ nstest.o nstest1.o nstext2.o -o nstest
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Your problem happens at link time. Your headers are fine. But you should compile the other cpp files aswell.

m8mble
  • 1,513
  • 1
  • 22
  • 30