LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2004, 06:36 AM   #1
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101

Rep: Reputation: 15
Unhappy 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)
 
Old 10-01-2004, 06:55 AM   #2
m00t00
Member
 
Registered: Sep 2004
Distribution: Slackware 10, Gentoo
Posts: 292

Rep: Reputation: 30
It _would_ be g++ test.cpp main.cpp, but your code doesnt compile. Try again.
 
Old 10-01-2004, 07:19 AM   #3
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101

Original Poster
Rep: Reputation: 15
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..
 
Old 10-01-2004, 08:23 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 10-01-2004 at 09:03 AM.
 
Old 10-01-2004, 09:32 AM   #5
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101

Original Poster
Rep: Reputation: 15
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
 
Old 10-09-2004, 11:56 AM   #6
kinn
LQ Newbie
 
Registered: Jul 2003
Distribution: gentoooooo
Posts: 3

Rep: Reputation: 0
thanks

Thanks a lot deiussum. I was struggling with this problem all evening. Finally I am relieved!!! And the link you gave was interesting.
 
Old 10-23-2007, 12:57 PM   #7
jk814u
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling driver files AngryDwarf Linux - Hardware 1 09-28-2005 07:05 AM
Compiling C++ Header Files ?? JinBaba Programming 3 07-20-2005 03:23 AM
compiling files from .tar.gz ungua Linux - Software 6 12-04-2004 01:34 PM
compiling .h and .cpp files with g++ arnab_be Linux - Newbie 2 03-03-2004 02:23 PM
Compiling files bacloff Linux - Software 3 08-27-2003 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration