473,661 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::GQu eue()
{
}

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

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

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

template <class QE>
int GQueue<QE>::siz e()
{
int size = data.size();
return size;
}
--
Remove the "downwithspamme rs-" text to email me.
Sep 3 '05 #1
11 9906
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**@downwiths pammers-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 "downwithspamme rs-" 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_HP P_INCLUDED
#define HEADER_MYLIB_HP P_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(voi d) 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**********@g mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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**********@g mail.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

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

Similar topics

6
3346
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 like that possible? Some workaround? Thank you, Patrick
1
3260
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: template <class T> class CRingBuffer { public: ... bool PutData( const T& value );
7
12471
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) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
6
2153
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 binary search tree. Note there is a requirement for this to be a Win32/MFC "friendly" class, thus the use of CObject and POSITION. There is also a requirement for there not to be a separate iterator class. template <class TYPE, class ARG_TYPE =...
8
11359
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 f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
5
1709
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 i havent correctly understood related to template specialization or is it some problem related to the compiler. Following is the code..
2
3361
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 hard time figuring out how to wrap template classes. I'm getting an C3231 compile error when I try to use the generic type as a template type. Here's an example from the MSDN C3231 compile error site:
2
2311
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 want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple set up,
8
2139
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 cross-platform scripting engine. When the scripting engine is embedded in an application, a platform-specific support library is linked in.) My first attempt goes here: ---code begin (library)---
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.