LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Compiling c++ files :( (https://www.linuxquestions.org/questions/programming-9/compiling-c-files-237471/)

gizmo_thunder 10-01-2004 06:36 AM

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__
#define __TEST_H__

template <class TYPE>
class Test
{
    TYPE tmp;
    public:
        Test( TYPE d );
        ~Test( );
};
#endif //__TEST_H__

//test.cpp
Code:

#include "test.h"

template <class TYPE>
Test<TYPE>::Test( TYPE d )
{
    tmp = d;
}

template <class TYPE>
Test<TYPE>::~Test( )
{

}

//main.cpp
Code:

#include "test.h"

int main( )
{
    Test<int> tmp(10);
}

Could somebody plz write a make file for compiling this file?
i use g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

m00t00 10-01-2004 06:55 AM

It _would_ be g++ test.cpp main.cpp, but your code doesnt compile. Try again.

gizmo_thunder 10-01-2004 07:19 AM

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..

deiussum 10-01-2004 08:23 AM

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.

gizmo_thunder 10-01-2004 09:32 AM

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 :)

kinn 10-09-2004 11:56 AM

thanks
 
Thanks a lot deiussum. I was struggling with this problem all evening. Finally I am relieved!!! And the link you gave was interesting.

jk814u 10-23-2007 12:57 PM

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.