Compiling c++ files :(
Hi,
Could some body help me solve this compilation problem?? ive' got test.h test.cpp and main.cpp and i need to compile them.. when i type g++ test.cpp main.cpp it says undefined reference ... following are the files.. could some body tell me how to compile them..!! //test.h Code:
#ifndef __TEST_H__ Code:
#include "test.h" Code:
#include "test.h" i use g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7) |
It _would_ be g++ test.cpp main.cpp, but your code doesnt compile. Try again.
|
g++ test.cpp main.cpp
/tmp/ccBOhDTz.o(.text+0x1a): In function `main': : undefined reference to `Test<int>::Test[in-charge](int)' /tmp/ccBOhDTz.o(.text+0x29): In function `main': : undefined reference to `Test<int>::~Test [in-charge]()' collect2: ld returned 1 exit status This is the output when i compile those files.. |
The full implementation of a template class must be in that template class's header. The ANSI/ISO standards for C++ allow for a way to put the implementation in a separate compilation unit using the export keyword (I think that's right, anyway...), but I don't think that there are currently any compilers that actually support this.
Edit: Just thought I'd elaborate a bit more. I am not a compiler writer, so the following might not be completely accurate. I believe that the reason for needing the full implementation in the header is that the compiler will typically only compile a template class when a "specialization" of that class is instantiated. The compiler will also only compile the parts of the template class that actually get used. So, when the compiler is compiling a single .cpp file that instantiates a specialization of a template class, it needs to have the full definition of that template class to correctly compile it. Edit2: In searching Google a bit to make sure I didn't make any claims that were too drastically wrong, I found a link that explains exporting templates a bit here. |
Wow!!! That was really nice.. thanks man...
i was breaking my head on this.. ofcourse couple of my friends too :) Well nice reading that link too :) |
thanks
Thanks a lot deiussum. I was struggling with this problem all evening. Finally I am relieved!!! And the link you gave was interesting.
|
Herb Sutter's Article on Export (New Location)
Well the thread helped a lot .... believe me ... For people like us who are trying out C++ after working for a long time in Java, small blocks held u up for days :(.
I tried to search for the article but did not get it (the above link is dead). The new location for the same is http://www.ddj.com/cpp/184401563. Thought it would help poor souls like me banging their heads. |
All times are GMT -5. The time now is 03:03 PM. |