Note that there are some explanatory texts on larger screens.

plurals
  1. POcompiling/linking template class as static library
    primarykey
    data
    text
    <p>I'm working on a program right now and to test template classes (which I will need) I wrote a small (and buggy, chances are 2 or 3 logic bugs in it, my goal is to get it to compile) stack class. What I want to do is to compile it to a static library (.a) then link it with the main program.</p> <p>The error is:</p> <pre><code>main.cpp:(.text+0x1c): undefined reference to `Stack&lt;int&gt;::Stack()' main.cpp:(.text+0x31): undefined reference to `Stack&lt;int&gt;::push(int)' main.cpp:(.text+0x42): undefined reference to `Stack&lt;int&gt;::push(int)' main.cpp:(.text+0x4e): undefined reference to `Stack&lt;int&gt;::pop()' main.cpp:(.text+0x5d): undefined reference to `Stack&lt;int&gt;::pop()' collect2: error: ld returned 1 exit status </code></pre> <p>This is the header file:</p> <pre><code>/* stack.h */ #ifndef _STACK_INCLUDED_ #define _STACK_INCLUDED_ template&lt;typename T&gt; struct Node { T* node; Node&lt;T&gt;* next; }; template&lt;typename T&gt; class Stack { private: Node&lt;T&gt;* bottom; public: Stack(); Stack(T first); Stack(T* arr, int amount); void push(T push); T* pop(); }; /* I added the following prototypes in an attempt to correct the error, did not work*/ template&lt;typename T&gt; Stack&lt;T&gt;::Stack(); template&lt;typename T&gt; Stack&lt;T&gt;::Stack(T first); template&lt;typename T&gt; Stack&lt;T&gt;::Stack(T* arr, int amount); template&lt;typename T&gt; void Stack&lt;T&gt;::push(T push); template&lt;typename T&gt; T* Stack&lt;T&gt;::pop(); #endif </code></pre> <p>Here is the implementation file:</p> <pre><code>/* stack.cpp */ #include "../heads/stack.h" #define null (void*)0 template&lt;typename T&gt; Stack&lt;T&gt;::Stack() { bottom = null; } template&lt;typename T&gt; Stack&lt;T&gt;::Stack(T first) { push(first); } template&lt;typename T&gt; Stack&lt;T&gt;::Stack(T* arr, int amount) { int i; for(i=0;i&lt;amount; i++) { push(arr[i]); } } template&lt;typename T&gt; void Stack&lt;T&gt;::push(T push) { Node&lt;T&gt;* tmp = new Node&lt;T&gt;(); tmp-&gt;node = push; tmp-&gt;next = null; Node&lt;T&gt;* node = bottom; while(node-&gt;next != null) { node = node-&gt;next; } node-&gt;next = tmp; } template&lt;typename T&gt; T* Stack&lt;T&gt;::pop() { int i=0; Node&lt;T&gt;* node = bottom; while(node-&gt;next != null) { i++; node = node-&gt;next; } node = bottom; for(;i&gt;1;i++) { node = node-&gt;next; } Node&lt;T&gt;* res = node-&gt;next; node-&gt;next = null; return res-&gt;node; } </code></pre> <p>You might have noticed the header file is included: "../heads/stack.h", this is because the structure looks like so:</p> <pre><code>- root -- CLASSNAME --- implementation of CLASSNAME -- heads --- all the headers -- obj --- object files (compiled) --bin ---final output </code></pre> <p>.</p> <p>The makefile looks like so:</p> <pre><code>CC=g++ CFLAGS=-c -fpermissive -Wall LFLAGS=-llua OBJ=obj BIN=bin HS=heads all: $(OBJ)/bind.a $(OBJ)/stack.a $(OBJ)/main.o $(CC) $(LFLAGS) -o $(BIN)/main $(OBJ)/main.o $(OBJ)/bind.a $(OBJ)/stack.a $(OBJ)/bind.o: binds/bind.cpp $(HS)/bind.h $(CC) $(CFLAGS) binds/bind.cpp -o $(OBJ)/bind.o $(OBJ)/bind.a: $(OBJ)/bind.o ar -cvq $(OBJ)/bind.a $(OBJ)/bind.o $(OBJ)/main.o: $(CC) $(CFLAGS) main/main.cpp -o $(OBJ)/main.o $(OBJ)/stack.o: $(HS)/stack.h stack/stack.cpp $(CC) $(CFLAGS) stack/stack.cpp -o $(OBJ)/stack.o $(OBJ)/stack.a: $(OBJ)/stack.o ar -cvq $(OBJ)/stack.a $(OBJ)/stack.o clean: touch $(OBJ)/dummy rm $(OBJ)/* </code></pre> <p>Compiler: g++ 4.7.2 (gcc-multilib)<br> OS: Arch Linux (x86_64) (kernel 3.6.6-1)</p> <p>You can get the whole file <a href="http://ge.tt/1JA070S/v/0" rel="nofollow">here</a> (I'm doing multiple test, so don't mind the -llua flag and other files, you can change the Makefile if needed, I just want to figure this out).</p> <p>After some research and testing, I noticed that the symbols aren't being exported, (<code>nm obj/stack.o</code> shows nothing, while <code>nm obj/bind.o</code> does. same thing for (.a)).<br> However I could not find any reference to what to do in this case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload