2

This simple piece of code really gives me hard time, so could anyone be some kind and explain me what could possibly be wrong? I've got simple cpp file that uses class included from header file.

lib.h

namespace tnamespace {    
class base{                                        
    virtual ~base() {};                            
};                                                 
class test/*: public base*/ {                                                                     
    public:                                                                                       

    test();                                                                                       
    test();                                                                                      
};                                                                                                
}  

lib.cxx

#include "lib.h"

namespace tnamespace{                                                                                             
    test::test() {};
    test::~test() {}
}

start.cpp

#include <iostream>
#include <lib.h>

int main() {
    tnamespace::test d;
    return 0;
}

i use gcc version 4.1.2 20080704 and compile project with

g++ start.cpp -I./ext_lib -Wall

got following linker error

/tmp/ccK2v6GD.o: In function `main':

start.cpp:(.text+0x7a): undefined reference to `tnamespace::test::test()'

start.cpp:(.text+0x88): undefined reference to `tnamespace::test::~test()'

collect2: ld returned 1 exit status

i managed to find solution. I forgot to compile my lib. Proper g++ command

g++ start.cpp ext_lib/lib.cxx -I./ext_lib -Wall

  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Luchian Grigore Oct 16 '12 at 10:14
  • http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400 – Luchian Grigore Oct 16 '12 at 10:15
  • Need lib.cxx on command line `g++ start.cpp lib.cxx -I./ext_lib -Wall` or some library... – PiotrNycz Oct 16 '12 at 10:15

1 Answers1

2

You didn't compile lib.cxx, so the symbols aren't exported.

g++ start.cpp lib.cxx -I./ext_lib -Wall
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625