C++ 使用外部实现将模板编译到静态库
我有一些类,我正试图编译成一个外部静态库。我的linked_list.hpp(我试图编译的linked_列表的标题)如下所示:C++ 使用外部实现将模板编译到静态库,c++,templates,makefile,static-libraries,C++,Templates,Makefile,Static Libraries,我有一些类,我正试图编译成一个外部静态库。我的linked_list.hpp(我试图编译的linked_列表的标题)如下所示: #include "list_base.hpp" template <typename T> class Linked_list : public List_base <T> { public:// Linked_list(); ~Linked_list(); public://acces
#include "list_base.hpp"
template <typename T>
class Linked_list : public List_base <T> {
public://
Linked_list();
~Linked_list();
public://accessor functions
Linked_list<T> * next();//go to the next element in the list
Node<T> * get_current_node() const;//return the current node
T get_current() const;//get the current data
T get(int index) const;//this will get the specified index and will return the data it holds
public://worker functions
Linked_list<T> * push(T value);//push a value into the front of the list
Linked_list<T> * pop_current();//deletes the current_node--resets the current as the next element in the list!
Linked_list<T> * reset();//this resets the current to the front of the list
Linked_list<T> * erase(int index);
private://variables
Node<T> * current;//store the current node
};
#include "linked_list.cpp"//grab the template implementation file
#endif
在运行时,它给了我这个错误
/usr/bin/ranlib: warning for library: liblist_templates.a the table of contents is empty (no object file members in the library define global symbols)
我创建了一个主头文件,其中包括“linked_list.hpp”和库的其他一些部分,但每当我包含它时,它似乎仍在查找包含的cpp文件。我做错了什么?我想创建一个可移植库,可以从此代码放入现有项目中。如果您的库仅包含模板代码,则它本身无法编译:例如,编译器不知道链接列表中将使用哪种类型作为
T
编译linked_list.cpp
时,编译器仅实现基本语法检查并生成空的对象文件ranlib
随后开始抱怨,因为您要求他用所有这些空对象文件创建一个空库
解决办法很简单:什么都不做!您的客户端代码只需要源文件;它不需要与任何库相链接。而且您始终需要发送
.cpp
文件,因为编译器不能没有它们做任何事情。还应该注意的是,链接列表.cpp
需要是\35;使用链接列表
将
d包含到任何翻译单元中。看起来像是链接列表。cpp
已经是链接列表.hpp
中的d,所以客户端代码只需要包含头文件.LOL——是的,这是真的。错过了。
/usr/bin/ranlib: warning for library: liblist_templates.a the table of contents is empty (no object file members in the library define global symbols)