undefined reference to <function name>

独自空忆成欢 提交于 2019-12-05 11:16:28

问题


I have this simple test file:

#include "stack.h"

int main()
{
  Stack* stck = init_stack();

  return 0;
}

and stack.h is defined as follows:

#ifndef STACK_H
#define STACK_H

#define EMPTY_STACK -1
typedef struct stack
{
  char ch;
  struct stack* prev;
} Stack;

extern Stack* init_stack();

extern char pop(Stack*);

extern void push(Stack*, char);

#endif

These two files are in the same directory. But when I do gcc .. to build it, I keep getting the error below:

$ ls
stack.c  stack.h  teststack.c
$ gcc -o testit teststack.c 
/tmp/ccdUD3B7.o: In function `main':
teststack.c:(.text+0xe): undefined reference to `init_stack'
collect2: ld returned 1 exit status

Could anyone tell me what I did wrong here?

Thanks,


回答1:


 gcc -o testit teststack.c stack.c

You need to compile both C source files and link the object files; this does it all in one command.



来源:https://stackoverflow.com/questions/12702187/undefined-reference-to-function-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!