Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:46:56+00:00 2026-06-14T14:46:56+00:00

I’m working on a program right now and to test template classes (which I

  • 0

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.

The error is:

main.cpp:(.text+0x1c): undefined reference to `Stack<int>::Stack()'
main.cpp:(.text+0x31): undefined reference to `Stack<int>::push(int)'
main.cpp:(.text+0x42): undefined reference to `Stack<int>::push(int)'
main.cpp:(.text+0x4e): undefined reference to `Stack<int>::pop()'
main.cpp:(.text+0x5d): undefined reference to `Stack<int>::pop()'
collect2: error: ld returned 1 exit status

This is the header file:

/* stack.h */
#ifndef _STACK_INCLUDED_
#define _STACK_INCLUDED_
template<typename T>
struct Node
{
    T* node;
    Node<T>* next;
};

template<typename T>
class Stack
{
private:
    Node<T>* 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<typename T>
Stack<T>::Stack();

template<typename T>
Stack<T>::Stack(T first);

template<typename T>
Stack<T>::Stack(T* arr, int amount);

template<typename T>
void Stack<T>::push(T push);

template<typename T>
T* Stack<T>::pop();

#endif

Here is the implementation file:

/* stack.cpp */
#include "../heads/stack.h"
#define null (void*)0

template<typename T>
Stack<T>::Stack() {
    bottom = null;
}
template<typename T>
Stack<T>::Stack(T first) {
    push(first);
}
template<typename T>
Stack<T>::Stack(T* arr, int amount)
{
    int i;
    for(i=0;i<amount; i++)
    {
        push(arr[i]);
    }
}
template<typename T>
void Stack<T>::push(T push)
{
    Node<T>* tmp = new Node<T>();
    tmp->node = push;
    tmp->next = null;

    Node<T>* node = bottom;
    while(node->next != null)
    {
        node = node->next;
    }
    node->next = tmp;
}
template<typename T>
T* Stack<T>::pop()
{
    int i=0;
    Node<T>* node = bottom;
    while(node->next != null)
    {
        i++;
        node = node->next;
    }
    node = bottom;
    for(;i>1;i++)
    {
        node = node->next;
    }
    Node<T>* res = node->next;
    node->next = null;
    return res->node;
}

You might have noticed the header file is included: “../heads/stack.h”, this is because the structure looks like so:

- root
-- CLASSNAME
--- implementation of CLASSNAME
-- heads
--- all the headers
-- obj
--- object files (compiled)
--bin
---final output

.

The makefile looks like so:

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)/*

Compiler: g++ 4.7.2 (gcc-multilib)
OS: Arch Linux (x86_64) (kernel 3.6.6-1)

You can get the whole file here (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).

After some research and testing, I noticed that the symbols aren’t being exported, (nm obj/stack.o shows nothing, while nm obj/bind.o does. same thing for (.a)).
However I could not find any reference to what to do in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:46:57+00:00Added an answer on June 14, 2026 at 2:46 pm

    You can’t make a lib for every possible template parameters, but you can for some types.

    A templated class is used to generate code of a class that taking the given type.

    When there is a instance using it, it will generate the actual class, otherwise, it will be simply omitted when compile.

    According to Compile header-only template library into a shared library? this is possible by using Explicit Instantiation, and make the types you declared into the lib, and let the rest to generate in compile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have an array which has BIG numbers and small numbers in it. I
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a small JavaScript validation script that validates inputs based on Regex. I
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.