I have some classes that I am trying to compile to an external static library. My linked_list.hpp (header for linked_list I’m trying to compile) looks like:
#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
All of my code headers go in the “header” directory and the implementation goes in the implementation folders I have. These are passed as -I directories.
My make file command for compiling looks like this:
static: $(DEPENDENCIES)
ar rcs liblist_templates.a node.o list_base.o linked_list.o
and upon running it gives me this error …
/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)
I created a master header file that includes the “linked_list.hpp” and a few other pieces of the library but whenever I include it, it still seems to be looking for the cpp files that are included. What am I doing wrong with this? I want to create a portable library that I can drop into existing projects from this code.
If your library only contains template code, it can not be compiled in itself: for example, the compiler does not know which type will be used as
Tin the linked list.When you compiled
linked_list.cpp, the compiler only realized basic syntax checks and generated an empty object file.ranlibis then complaining since you ask him to create an empty library out of all these empty object files.The solution is simple: do nothing! Your client code only needs the source files; it doesn’t need to be linked against any library. And you’ll always need to ship the
.cppfiles, since the compiler can not do anything without them.