如何使模板类成为静态库的一部分? [英] HowTo make template class part of static library?

查看:174
本文介绍了如何使模板类成为静态库的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的一位开发人员发布这个问题,他们并不像新闻组那么精彩。有什么建议吗?


问题如下,以及相关的源代码。


-Matt


我有一个模板化的队列类作为静态可链接的

库的一部分(在GQueue.h和.cpp下面)。库编译好,但是当我在可执行文件中使用它时,我得到链接器错误抱怨

它找不到任何队列函数的引用。我将
编译为队列类作为目标文件,然后在队列对象上执行''nm''

并返回以下内容:


00000000 b .bss

00000000 d .data

00000000 t .text


无功能目标文件。我将队列硬编码为字符串

队列,一切正常。如何获取模板化版本

以生成有效的目标文件,以便我可以离开队列

模板化并仍然在静态库中使用它而不是直接

用可执行文件编译它?


---------------- GQueue.h ------- ----------

#ifndef GQUEUE_H

#define GQUEUE_H


#include< string>

#include< queue>


使用命名空间std;


模板< class QE>

class GQueue

{

public:


GQueue();

~GQueue();


void push(QE元素);

QE pop();

int size() ;


私人:


队列< QE>数据;

};


#endif

---------------- GQueue.cpp -----------------

#include" GQueue.h"


template< ; QE级>

GQueue< QE> :: GQueue()

{

}


模板< class QE>

GQueue< QE> ::〜GQueue()

{

}

模板< class QE>

void GQueue< QE> :: push(QE元素)

{

数据。 push(element);

}


模板< class QE>

QE GQueue< QE> :: pop()

{

QE tmp = data.front();

data.pop();

返回tmp ;

}


模板< class QE>

int GQueue< QE> :: size()

{

int size = data.size();

返回大小;

}

-

删除downwithspammers-给我发电子邮件的文字。

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.

推荐答案

我忘了提及:


这两个问题都发生在gcc中Windows-MingW和Redhat-Linux

环境。我目前还不知道gcc版本。


如果需要更多信息,请告诉我。


-Matt

星期六,2005年9月3日14:19:31 -0500,Matt

< ma ** @ downwithspammers-mengland.net>写道:
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?



-

删除downwithspammers-给我发电子邮件的文字。


--
Remove the "downwithspammers-" text to email me.


Matt写道:
我正在为我的一位开发人员发布这个问题
新闻组精明。有什么建议吗?

问题如下,以及相关的源代码。

-Matt

我有一个模板化的队列类作为静态的一部分可链接的库(在GQueue.h和.cpp下面)。库编译得很好,但是当我在可执行文件中使用它时,我得到链接器错误抱怨
它找不到任何队列函数的引用。我只将队列类编译为目标文件,然后对队列对象执行''nm''
并返回以下内容:
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:




你不能将模板代码放在带有现代编译器的库中。

将所有模板代码放在头文件中,一切都会正常工作

罚款。模板代码应该放在头文件中。


原因是模板代码在使用

之前没有正确编译(实例化是技术术语),因此模板尚未被使用,因此没有什么可以进入库中。


只需扔掉.cpp文件并放入所有文件.h文件中的代码。

分发.h文件。


John



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


Matt写道:

---------------- GQueue.h ---------------- -
#ifndef GQUEUE_H
#define GQUEUE_H
#include< string>
#include< queue>

使用命名空间std ;

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

#include <string>
#include <queue>

using namespace std;



人们什么时候学会不把它放在标题中?


Ian


When will people learn not to put this in headers?

Ian


这篇关于如何使模板类成为静态库的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆