LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-24-2004, 10:26 PM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Linking problem while using templates


Ok! I wrote my first list class in C++ using templates. It worked great when I tried using it in a test program. However, when I tried to include it in a bigger project, I got linking errors that the list class members had undefined references (all of them that used templates). After doing some testing, I found that if the member code was in the same object file as the code that was using them, the compiler didn't complain. But when I used the members in a seperate object file there was undefined references. Here is an example:

Test.h:
Code:
template <class DataType>
class MyTestClass
{
	public:
		int Func(DataType &, int);
};
Test.cpp:
Code:
#include <iostream>
#include <string>
#include "test.h"
using namespace std;

template <class DataType>
int MyTestClass<DataType>::Func(DataType &Value, int Count)
{
	int i;
	for (i=0;i<Count;i++)
		std::cout << Value;
}

int main(int argc, char **argv)
{
	MyTestClass<string> Test;
	
	Test.Func("Hello!", 5);
}
g++ -O3 -o test test.cpp

Now this works fine, compiling and working without errors (I haven't acctually tried, but you get the idea! )

This however, doesn't work:

Template.cpp
Code:
#include "test.h"

template <class DataType>
int MyTestClass<DataType>::Func(DataType &Value, int Count)
{
	int i;
	for (i=0;i<Count;i++)
		std::cout << Value;
}
Test.cpp
Code:
#include <iostream>
#include <string>
#include "test.h"
using namespace std;

int main(int argc, char **argv)
{
	MyTestClass<string> Test;
	
	Test.Func("Hello!", 5);
}
g++ -c -O3 -o template.o template.cpp
g++ -c -O3 -o test.o test.cpp
g++ -O3 -o test test.o template.o

This gives an error something like:
Undefined reference to MyTestClass<(some very long thing about the string class>::Func(DataType &Value, int) when [DataType = string]

What do I do to fix the issue?

Last edited by The_Nerd; 06-24-2004 at 10:28 PM.
 
Old 06-25-2004, 01:44 AM   #2
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Hi,

You're looking for the "export" keyword. You need to prefix the template *definition* with "export" to tell the compiler that this definition of the template might be required for instantiations in other files or modules.

So, the correct way to do this is:

In template.cpp:

export template <class DataType> int MyTe.... (..., int Count) {
.
.
.
}

But, don't bother using it that way. g++ doesn't yet support the "export" keyword. So, it won't work anyway. In short, what you're trying to do (seperate template definition and instantiation) is (nearly?) impossible in gcc.

Some links:
Explaination: http://www.linux.ie/pipermail/ilug/2...er/008941.html
Won't be in gcc: http://gcc.gnu.org/ml/gcc/2004-01/msg01982.html

gcc bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=8380
gcc bug: http://lists.debian.org/debian-gcc/2.../msg00142.html

Arvind
 
Old 06-25-2004, 01:52 AM   #3
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Hi,

More links:

http://www.tek-tips.com/gfaqs.cfm/pid/208/fid/2798
In depth look into export: http://www.gotw.ca/publications/mill23.htm

Arvind

PS: AFAIK, no C++ compiler supports it.

Last edited by arvind_sv; 06-25-2004 at 01:53 AM.
 
Old 06-25-2004, 12:19 PM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
So I have to put the function bodies into the header file?
 
Old 06-25-2004, 12:25 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Yes. That's how it's done.
 
Old 06-25-2004, 05:21 PM   #6
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Thanks guys!
 
Old 07-11-2004, 03:18 PM   #7
QtCoder
Member
 
Registered: Aug 2003
Location: USA
Distribution: Slackware 12.0 RC1
Posts: 129

Rep: Reputation: 15
Hi there.

I was building a templatized version of a linked list and came across the same problem. The solution suggested does work, but doesn't it seem to go against the philosophy of encapsulation in C++? I mean, anyone who wants to use my linked list must use the header file, and in doing so, they can see (not to mention change) all the hidden implementation details. This really isn't a big deal, but why don't compilers support the separation of class template member function declaration and definition?

It just seems odd to me, but perhaps there is a technical detail that is very difficult for compiler creators to overcome.

Last edited by QtCoder; 07-11-2004 at 03:19 PM.
 
Old 07-11-2004, 09:11 PM   #8
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
Look at one of the pages, whose link I posted earler: "In depth look into export".
It answers your question.

Arvind
 
  


Reply



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
problem with templates please help..... max_rsr Linux - Newbie 1 04-13-2005 06:51 PM
ld linking problem captain-mythos Linux - Software 1 06-20-2004 08:46 AM
C++ Linking Problem acjt Programming 4 01-31-2004 08:48 AM
linking issues when using templates dhanakom Programming 1 09-02-2003 01:51 PM
linking problem with ld ! gluon Programming 2 05-19-2002 01:59 PM

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

All times are GMT -5. The time now is 10:35 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