LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-17-2008, 08:52 PM   #1
linuxmthomson
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Rep: Reputation: 0
Problem creating shared lib containing templates. Help......


hey guys, any help would be appreciated as this is driving me nuts.

I am basically trying to create a shared lib that contains templates. This code used to work on solaris under Sunwspro but when i try and compile under RH linux CC:

Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)


It doesn't work.

Here is my code ( any help would be greatly appreciated )

//AClass.h
#include <string>

using namespace std;

template <class T>
class AClass
{
public:
AClass(){};
~AClass(){};
T getName();
private:
T name;


};

//AClass.cc
#include "AClass.h"

template <class T>

T AClass<T>::getName()
{
return name;
}

//TestA.cc
#include "AClass.h"
int main()
{
AClass<string> a;
a.getName();
return 0;

}


/usr/bin/cc -m32 -c -o AClass.o AClass.cc - COMPILES OK

/usr/bin/cc -shared -o libTest.so AClass.o -g -m32 -lstdc++ - CREATES SHARED LIB

Here is the nm map for the shared lib. Interestingly enough, no reference to getName() ??

nm libTest.so
00000000000013fc a _DYNAMIC
00000000000014d0 a _GLOBAL_OFFSET_TABLE_
w _Jv_RegisterClasses
00000000000013e8 d __CTOR_END__
00000000000013e4 d __CTOR_LIST__
00000000000013f0 d __DTOR_END__
00000000000013ec d __DTOR_LIST__
00000000000003e0 r __FRAME_END__
00000000000013f4 d __JCR_END__
00000000000013f4 d __JCR_LIST__
00000000000014e4 A __bss_start
w __cxa_finalize@@GLIBC_2.1.3
0000000000000390 t __do_global_ctors_aux
00000000000002d0 t __do_global_dtors_aux
00000000000013f8 d __dso_handle
w __gmon_start__
0000000000000387 t __i686.get_pc_thunk.bx
00000000000014e4 A _edata
00000000000014ec A _end
00000000000003c4 T _fini
0000000000000258 T _init
00000000000002a0 t call_gmon_start
00000000000014e8 b completed.5803
00000000000014e4 b dtor_idx.5805
0000000000000350 t frame_dummy



/usr/bin/cc -g -m32 -Wno-deprecated -I. -L. -l Test -lstdc++ TestA.cc -o TestA.exe

This fails with:

/usr/bin/cc -g -m32 -Wno-deprecated -I. -L. -l Test -lstdc++ TestA.cc -o TestA.exe
/var/tmp/cccRx1dy.o: In function `main':
/home/mark/workspace/ITSMarketInfo/src/cpp/Sandbox/TestA.cc:5: undefined reference to `AClass<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getName()'
collect2: ld returned 1 exit status
 
Old 11-17-2008, 09:12 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
What you're doing is not possible because templates aren't classes, functions, or data; they don't exist unless they're used. That means T AClass<T>::getName() doesn't exist until it's used, and it's never used in the context of the compilation of AClass.cc. For that reason, T AClass<T>::getName() must be available in the context of every parameter T being used, otherwise the function is never compiled. In other words, main.cc needs to include the file that defines T AClass<T>::getName(). Normally this is done by having the header include its own source, or by putting them both in the same file.
ta0kira

Last edited by ta0kira; 11-17-2008 at 09:19 PM.
 
Old 11-19-2008, 01:03 AM   #3
linuxmthomson
LQ Newbie
 
Registered: Jun 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks, that worked a treat.

Interestingly enough my old implementation compiles / links and runs on Solaris. I do remember something about the Solaris Sunwspro compiler generating all of the template implemenations for you.
 
  


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
C++ templates and shared libraries??? panagopoulos Programming 3 04-18-2006 10:09 PM
Problem: creating a shared library allomeen Programming 3 02-09-2006 07:03 PM
Creating templates in OO.org 1.1.2 bruno buys Linux - Software 0 02-18-2005 06:36 PM
shared lib orlymeir Linux - Software 1 11-28-2004 05:50 AM

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

All times are GMT -5. The time now is 03:45 PM.

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