joł buły mam problem. rozdzieliłem program kalkulator RPN z książki K&R na różne pliki źródłowe + 1 nagłówek lecz podczas linkowania gcc wywala że main nie widzi deklaracji metod które są w nagłówku.
Kompiluję tak: gcc -Wall stack.c main.c getch.c getop.c -o RPNcalculator
A tu są poszczególne pliki:
calc.h:
#define NUMBER '0'
void Push(double);
double Pop(void);
int GetOp(char[]);
int GetCh(void);
void UnGetCh(int);
stack.c:
#include<stdio.h>
#include"calc.h"
#define stack_depth 100 //maximum stack depth
int stackpointer = 0;
double stack[stack_depth];
void push(double value)
{
if(stackpointer < stack_depth)
stack[stackpointer++] = value;
else
printf("stack full!\n");
}
double pop(void)
{
if (stackpointer > 0)
return stack[--stackpointer];
else
{
printf("Stack empty!\n");
return 0.0;
}
}
main.c:
#include<stdio.h>
#include<stdlib.h>
#include"calc.h"
#define OP_MAX_SIZE 100 // maximum operand size
int main(void)
{
int type;
double operand2;
char input[OP_MAX_SIZE];
while((type = GetOp(input)) != EOF)
{
switch(type)
{
/* OPERANDS */
case NUMBER:
Push(atof(input));
break;
/* OPERATORS */
case '+':
Push(Pop() + Pop());
break;
case '*':
Push(Pop() * Pop());
break;
case '-':
operand2 = Pop();
Push(Pop() - operand2);
break;
case '/':
operand2 = Pop();
if (operand2 != 0.0)
Push(Pop() / operand2);
else
printf("Cannot divide by zero!");
break;
case '%':
operand2 = Pop();
if(operand2 != 0.0)
Push((int) Pop() % (int) operand2);
else
printf("Cannot divide by zero!");
break;
/* OTHER */
case '\n':
printf("= %.8g\n", Pop());
break;
}
}
return 0;
}
getch.c:
#include<stdio.h>
#define BUFFER_SIZE 100
char Buffer[BUFFER_SIZE];
int bfp = 0; //buffer's next free position
int GetCh(void)
{
return (bfp > 0) ? Buffer[--bfp] : getchar();
}
void UnGetCh(int character)
{
if(bfp >= BUFFER_SIZE)
printf("Too many characters!\n");
else
Buffer[bfp++] = character;
}
getop.c:
#include<stdio.h>
#include<ctype.h>
#include"calc.h"
int GetOp(char OpArray[])
{
int i, next_character;
//Skip all the blanks and tabs:
while((OpArray[0] = next_character = GetCh()) == ' ' || next_character == '\t');
OpArray[1] = '\0';
if(!isdigit(next_character) && next_character != '.')
return next_character; //if it's not a digit or deciman, return (not a number)
i = 0;
if(isdigit(next_character))
while(isdigit(OpArray[++i] = next_character = GetCh()));
if(next_character == '.')
while(isdigit(OpArray[++i] = next_character = GetCh())); //Collect the fraction part
OpArray[i] = '\0';
if(next_character != EOF)
UnGetCh(next_character);
return NUMBER;
}
A to jest output od gcc:
/tmp/ccuaWFnU.o: In function `main':
main.c:(.text+0x47): undefined reference to `Push'
main.c:(.text+0x51): undefined reference to `Pop'
main.c:(.text+0x5e): undefined reference to `Pop'
main.c:(.text+0x6b): undefined reference to `Push'
main.c:(.text+0x75): undefined reference to `Pop'
main.c:(.text+0x82): undefined reference to `Pop'
main.c:(.text+0x8f): undefined reference to `Push'
main.c:(.text+0x99): undefined reference to `Pop'
main.c:(.text+0xa3): undefined reference to `Pop'
main.c:(.text+0xad): undefined reference to `Push'
main.c:(.text+0xb7): undefined reference to `Pop'
main.c:(.text+0xd7): undefined reference to `Pop'
main.c:(.text+0xe1): undefined reference to `Push'
main.c:(.text+0x102): undefined reference to `Pop'
main.c:(.text+0x122): undefined reference to `Pop'
main.c:(.text+0x141): undefined reference to `Push'
main.c:(.text+0x15c): undefined reference to `Pop'
collect2: ld returned 1 exit status
Z góry dzięki.