Search code examples
cundefined

undefined reference to "Fucntion" from another file


i dont know where is the problem,i have defined the function in the header file and im calling it in the main using include "stack.h", undefined reference to`init' all 3 are located in the same file

main

#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main()
{

struct Stack s1;
int choice;
init(&s1,5);

stack.c

#include "stack.h"
void init(struct Stack *st, int size)
{
st->pin = (int *) malloc(size * sizeof(int));
st->head = 0;
st->size = size;
}
int push(struct Stack *st, int element)

{
if (st->head == st->size)
{
    return 0;
}
st->pin[st->head] = element;
st->head++;
return 1;
}


int pop(struct Stack *st, int *element)
{
if(st->head == 0)
{
    return 0;
}
st->head--;
*element = st->pin[st->head];
return 1;
}

stack.h

#ifndef stack
#define stack


struct Stack{
   int *pin;
   int head;
   int size;
};

void init(struct Stack *st, int size);

int push(struct Stack *st, int element);

int pop(struct Stack *st, int *element);

#endif

Solution

  • Are you using Linux makefile/vc++ project to build executable.

    In linux makefile parlance, you need to link stack.o and main.o (created from compiling stack.c and main.c) to create the final executable. (vc++ project should do it for you behind the scene)

    What is the exact error that you are seeing