473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

HowTo make template class part of static library?

I'm posting this question for one of my developers who's not quite as
newsgroup-savvy. Any suggestions?

The question follows, along with relevant source code.

-Matt

I have a templated queue class as part of a statically linkable
library (in GQueue.h and .cpp below). The library compiles fine, but
when I go to use it in an executable, I get linker errors complaining
that it can not find references for any of the queue functions. I
compile just the queue class as an object file, and then perform 'nm'
on the queue object and it returns the following:

00000000 b .bss
00000000 d .data
00000000 t .text

No functions in the object file. I hard coded the queue as a string
queue and everything works fine. How can I get the templated version
to generate a valid object file so that I can leave the queue
templated and still use it in a static library rather that directly
compiling it with the executable?

---------------- GQueue.h -----------------
#ifndef GQUEUE_H
#define GQUEUE_H

#include <string>
#include <queue>

using namespace std;

template <class QE>
class GQueue
{
public:

GQueue();
~GQueue();

void push(QE element);
QE pop();
int size();

private:

queue<QE> data;
};

#endif
---------------- GQueue.cpp -----------------
#include "GQueue.h"

template <class QE>
GQueue<QE>::GQueue()
{
}

template <class QE>
GQueue<QE>::~GQueue()
{
}

template <class QE>
void GQueue<QE>::push(QE element)
{
data.push(element);
}

template <class QE>
QE GQueue<QE>::pop()
{
QE tmp = data.front();
data.pop();
return tmp;
}

template <class QE>
int GQueue<QE>::size()
{
int size = data.size();
return size;
}
--
Remove the "downwithspammers-" text to email me.
Sep 3 '05 #1
11 9821
I forgot to mention:

This problem happens with gcc in both a Windows-MingW and Redhat-Linux
environment. I don't know the gcc version at the moment.

Please let me know if more info is required.

-Matt
On Sat, 03 Sep 2005 14:19:31 -0500, Matt
<ma**@downwithspammers-mengland.net> wrote:
How can I get the templated version
to generate a valid object file so that I can leave the queue
templated and still use it in a static library rather that directly
compiling it with the executable?

--
Remove the "downwithspammers-" text to email me.
Sep 3 '05 #2
Matt wrote:
I'm posting this question for one of my developers who's not quite as
newsgroup-savvy. Any suggestions?

The question follows, along with relevant source code.

-Matt

I have a templated queue class as part of a statically linkable
library (in GQueue.h and .cpp below). The library compiles fine, but
when I go to use it in an executable, I get linker errors complaining
that it can not find references for any of the queue functions. I
compile just the queue class as an object file, and then perform 'nm'
on the queue object and it returns the following:


You can't put template code in a library with contemporary compilers.
Put all the template code in the header files and everything will work
fine. Template code should go in header files.

The reason is that template code is not properly compiled until it is
used (instantiated is the technical term), so there is nothing to go
into the library because the templates haven't been used yet.

Just throw away the .cpp file and put all the code in the .h files.
Distribute the .h files.

John
Sep 3 '05 #3
Ian
Matt wrote:

---------------- GQueue.h -----------------
#ifndef GQUEUE_H
#define GQUEUE_H

#include <string>
#include <queue>

using namespace std;

When will people learn not to put this in headers?

Ian
Sep 3 '05 #4
> You can't put template code in a library with contemporary compilers. Put
all the template code in the header files and everything will work fine.
Template code should go in header files.

The reason is that template code is not properly compiled until it is used
(instantiated is the technical term), so there is nothing to go into the
library because the templates haven't been used yet.

Just throw away the .cpp file and put all the code in the .h files.
Distribute the .h files.

John


If you know before hand a set of types to be passes as template arguments,
though, you theoretically can put explicit template instantiations in a
static library. For example:

file: mylib.hpp
--------------------------------

#ifndef HEADER_MYLIB_HPP_INCLUDED
#define HEADER_MYLIB_HPP_INCLUDED

// define the interface:

template <typename T>
class C
{
public:
T clone(void) const;
// ...
};

#endif // inc. guard...

file: mylib.cpp
---------------------------------

#include <mylib.hpp>

// implement the class (template)

template <typename T>
T C<T>::clone(void) const
{
return T();
}

// explicitly instantiate C<> with a bunch
// of built-in types

template class C<int>;
template class C<double>;
template class C<char>;
template class C<bool>;
// ...
Note, however, unless C<T> is explicitly instantiated in mylib.cpp, you will
most likely get linker errors.

Regards,
Ben
Sep 4 '05 #5
not ever. It is far more simpler to type that out than qualify
everything with a std:: prefix.

man perl | grep -l virtues

The three principal virtues of a programmer are Laziness, Hubris and
Impatience. See the Camel book for why.

Note "Laziness" :)

-vijai.

Sep 5 '05 #6
"Vijai Kalyan" <vi**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
not ever. It is far more simpler to type that out than qualify
everything with a std:: prefix.

man perl | grep -l virtues

The three principal virtues of a programmer are Laziness, Hubris and
Impatience. See the Camel book for why.

Note "Laziness" :)

-vijai.


Not really. I prefer the standard include which gives a nicer list of
auto-completion on my IDE :)

Ben
Sep 5 '05 #7
:) Sure. I do the same

-vijai.

Sep 5 '05 #8
I don't see you using "string" anywhere in your source. If it's not
needed remove it.

-vijai.

Sep 5 '05 #9
On 4 Sep 2005 21:13:23 -0700, "Vijai Kalyan" <vi**********@gmail.com> wrote:
not ever. It is far more simpler to type that out than qualify
everything with a std:: prefix.


It's not nice (especially if you're programming in a team environment) to
pollute the namespace of the module that happens to include your header with
another namespace, even if it is the std namespace.

I use "using namespace xxx" quite often to simplify my modules, however, I
always fully qualify all namespaced entities in my _header_ files.

-dr
Sep 5 '05 #10
Davem

I meant that sarcastically.

-vijai.

Sep 7 '05 #11
Dave

I meant that sarcastically.

-vijai.

Sep 7 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
1
by: Roland Raschke | last post by:
Hi, I'm a novice in using templates and want to write a static library with some communication classes. One of these classes uses two instances of a ringbuffer template as class members: ...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
2
by: Rune Vistnes | last post by:
Hey, I am trying to wrap an unmanaged library in managed c++ so that I can use this library in other .NET languages, such as C#. I've been successful for the most part this far, but I'm having a...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.