c - Getting error undefined reference -
diceroll.c
/*diceroll.c -- dice roll simulation */ #include "diceroll.h" #include <stdio.h> #include <stdlib.h> //for library function rand() int roll_count = 0; //external linkage static int rollem(int sides) /*private file */ { int roll; roll = rand() % sides + 1; ++roll_count; return roll; } int roll_n_dice(int dice, int sides){ int d; int total = 0; if(sides < 2){ printf("need @ least 2 sides.\n"); return -2; } if(dice < 1){ printf("need @ least 1 dice.\n"); return -1; } for(d = 0; d < dice; d++){ total += rollem(sides); } return total; }
header file
//diceroll.h extern int roll_count; extern int roll_n_dice(int dice, int sides);
the main program
/*manydice.c -- multiple dice rolls */ /*compile diceroll.c */ #include <stdio.h> #include <stdlib.h> //for library funcion srand() #include <time.h> //for time() #include "diceroll.h" //for roll_n_dice , roll_count int main(void){ int dice, roll; int sides; srand((unsigned int ) time(0)); /*randomized seed*/ printf("enter number of sides per die, 0 stop.\n"); while(scanf("%d", &sides) == 1 && sides > 0){ printf("how many dice ?\n"); scanf("%d", &dice); roll = roll_n_dice(dice, sides); printf("you have rolled %d using %d %d-sided dice.\n", roll, dice, sides); printf("how many sides ? enter 0 stop.\n"); } printf("the rollem() funcion called %d times.\n", roll_count); //used extern variable printf("good fortune you!!!\n"); return 0; }
here compiling with
gcc manydice.c diceroll.c
but keep saying undfined reference roll_count , roll_n_dice
how fix undfined reference though linking both the
source files.
and used 2 times:
gcc sourcefile.c -o sourcefile.o -c
for manydice , diceroll , compile using object code still getting same error.
i.e:
gcc manydice.c -o manydice.o -c gcc diceroll.c -o diceroll.o -c gcc -o myresult manydice.o diceroll.o
Comments
Post a Comment