0

Undefined reference to All Functions as "push , pop ,createstack, etc.." I included all the header files correctly , but when I run it I get the error: undefined reference to '(function name)'. I get this error for all my functions. Below you can find the source code of all my three files.

Test.c

#include <stdio.h>
#include "Stack.h"

void printMessage() {
    printf("\n(a) Read an element then Push it.\n");
    printf("(b) Pop an element then display it.\n");
    printf("(c) Returns a copy of the top-element of the stack without deleting it\n");
    printf("(d) Exit.\n");
}

void userStackTop(Stack *ps, StackEntry *pe) {
    pop(ps, pe);
    push(ps, *pe);
}

Stack S;
StackEntry E;
int Size;
char Choice;

int main() {
    setvbuf(stdout, NULL, _IONBF, 0);  //Eclipse Bug for scanf and printf
    createStack(&S);
    printf("Welcome: ");
    printMessage();
    while (scanf("\n%c", &Choice)) {
        if (Choice == 'a' || Choice == 'A') {
            printf("Enter Your Element: ");
            scanf("\n%c", &E);
            if (!stackFull(&S)) {
                push(&S, E);
                printf("Element (%c) is pushed into stack.\n\n",E);
            }
            else
                printf("\n\nSorry , The Stack is Full!\n\n");
        } else if (Choice == 'b' || Choice == 'B') {
            if (!stackEmpty(&S)) {
                pop(&S, &E);
                printf("\n\nThe Element (%c) is popped.\n\n", E);
            } else
                printf("\n\nSorry , The Stack is Empty!\n\n");
        } else if (Choice == 'c' || Choice == 'C') {
            if (!stackEmpty(&S)) {
                //stackTop(&S, &E);
                userStackTop(&S, &E);
                printf("Last Element Pushed in the stack is (%c).\n",E);
            } else
                printf("\n\nSorry , The Stack is Empty!\n\n");
        } else if (Choice == 'd' || Choice == 'D')
            return 0;
        printMessage();
    }

    return 0;
}

Stack.c

#include "Stack.h"

void createStack(Stack *ps) {
    ps->top = 0;
}
void push(Stack *ps, StackEntry e) {
    ps->Data[ps->top++] = e;
}
void pop(Stack *ps, StackEntry *pe) {
    *pe = ps->Data[ps->top - 1];
    ps->top--;

}
int stackEmpty(Stack *ps) {
    if (ps->top <= 0)
        return 1;
    else
        return 0;
}
int stackFull(Stack *ps) {
    if (ps->top == MAXSTACK - 1)
        return 1;
    else
        return 0;
}
int stackSize(Stack *ps) {
    return ps->top + 1;
}
void stackTop(Stack *ps, StackEntry *pe) {
    *pe = ps->Data[ps->top-1];
}

Stack.h

#ifndef STACK_H_
#define STACK_H_

typedef char StackEntry;
#define MAXSTACK 10

typedef struct stack {
    int top;
    StackEntry Data[MAXSTACK];
} Stack;

void createStack(Stack *ps);
void push(Stack *ps, StackEntry e);
void pop(Stack *ps, StackEntry *pe);
int stackEmpty(Stack *ps);
int stackFull(Stack *ps);
int stackSize(Stack *ps);
void stackTop(Stack *ps, StackEntry *pe);


#endif /* STACK_H_ */
2
  • It seems that when the linker kicks in, it doesn't link the object file for Stack.C. How do you build your program? Do you use an IDE (which one?), or do you use GNU Make (if so add your Makefile to the question), or CMake (if so add your CMakeLists.txt) to the question. Commented Oct 24, 2015 at 20:12
  • I've tried both Visual Studio and Codeblocks Codeblocks occurs that error "Undefined Reference" While in visual Studio occurs that error " error LNK2019: unresolved external symbol "void " Commented Oct 24, 2015 at 20:23

1 Answer 1

1

Somewhat surprisingly maybe, the problem is in your file names...

Unless you tell the compiler otherwise, a .C (upper case) file is taken to mean a C++ file, and .c (lower case) to mean a C file. If you mix them, the object files that the compiler produces are not "compatible".

You are mixing both (Test.C and Stack.c), so the linker will not find the functions that are used in Main.obj in Test.obj and will issue an unresolved external symbol error. (The linker cannot find the corresponding functions because of name mangling differences between C and C++.)

One solution is to rename Test.C to Test.c.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.