gcc linking stdlib?

Asked by vergarr

I am trying to compile a program that I am working on and I keep getting the error:
called object ‘rand’ is not a function.

The error specifies this line:
x += drifter.oxy = 10+ GetNum(10);
with GetNum defined as:
#define GetNum(d) ( rand() %d +1 )

After looking through the lib and usr/lib directories AND using
grep -l rand * | more

I couldn't figure out which file to link to gcc I tried the ones that looked most promising, and even tried -l* but no luck. So I tried compiling another program I have that also calls for rand() and it compiles with no problems.

Both #include <the same files> accept that the one that wont compile also includes strings.h. I tried commenting that include out with the same effect.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gcc-defaults Edit question
Assignee:
No assignee Edit question
Solved by:
W. Prins
Solved:
Last query:
Last reply:
Revision history for this message
Best W. Prins (wprins) said :
#1

There must be something else else wrong in your source that you're not showing us. For the record, the stdlib library is linked in by default if I remember correctly, so you dont need a particular -l switch for it, however you do still need to have a corresponding #include<stdlib.h> in source files linking to it to ensure the compiler is using the correct function signatures.

Do you have some other variable or something declared in the scope where you're getting the error thats shadowing out the function in stdlib, perhaps?

Revision history for this message
W. Prins (wprins) said :
#2

As an aside, may I suggest you (perhaps preferentially/also ) post your programming related questions to one of the Ubuntu community programming subforums as these types of questions probably fit better there than here? They are here: http://ubuntuforums.org/forumdisplay.php?f=310

Revision history for this message
vergarr (moonsdad) said :
#3

Thank you for making me aware of the programing sub-forums, in the future I will post my programing related questions there.

For now though, perhaps you wont mind reviewing my source code(sorry I don't know how to format it for better readability):

01 #include <stdio.h>
02 #include <string.h>
03 #include <time.h>
04
05 #define GetNum(d) ( rand() %d +1 )
06 #define MALLOC(x) ( (x *)malloc(sizeof(x)) )
07
08 typedef struct {
09 int mas; /* mass units */
10 int ful; /* fuel */
11 int oxy; /* oxygen */
12 int wtr; /* water */
13 int fod; /* food */
14 int ppl; /* pasengers */
15 int wep; /* weapon class */
16 int shd; /* shields */
17 int dam; /* damage */
18 } SHIP;
19
20 typedef struct {
21 int typ; /* type: sun, habited, habitable, un, asteroid, station */
22 int hos; /* hostile? o=y, 1=neut, 3=opp */
23 int res[6]; /* resources available 0=x,1=o,2=#,3=f,4=w,5=trade(0=n,1=y) */
24 } LOCATION;
25
26 int main( void )
27 {
28 char player[] = "Cpt. "; /*user name*/
29 char craft[] = "The "; /*ship name*/
30 SHIP drifter;
31 LOCATION loc;
32 int rand, x=0;
33 char c;
34
35 srand(time());
36
37 x += drifter.ful = 10 + GetNum(10);
38 x += drifter.oxy = 10 + GetNum(10);
39 x += drifter.wtr = 10 + GetNum(10);
40 x += drifter.fod = 10 + GetNum(10);
41 x += drifter.ppl = GetNum(6);
42 drifter.wep = 1;
43 drifter.shd = 20;
44 drifter.dam = 0;
45 drifter.mas = (100 - x);
46
47 /*prompt for names*/
48 printf("What is your name, Captain? ");
49 while ((c = getchar()) != '\n')
50 {
51 player = realloc(player, (strlen(player) + sizeof(c) +1));
52 if( player != NULL )
53 strcat(message, c);
54 else{
55 fprintf(stderr, "Error allocating memory!\n");
56 exit(1);
57 }
58 puts(player); /*test*/
59 }/*end get name while*/
60
61 return 0;
62 }/*end main*/

Thank you.

Revision history for this message
vergarr (moonsdad) said :
#4

Please note that I attempted compiling it with #include<stdlib.h> originally. In the above code it has been removed, because the other programs that compiled successfully did not include stdlib.h.

Revision history for this message
vergarr (moonsdad) said :
#5

Thanks ByteJuggler, that solved my question.

Revision history for this message
vergarr (moonsdad) said :
#6

After reviewing the post of my code to be sure it was readable, I noticed that I had declared an int variable called rand on line 32, which overrided the rand function. Lesson learned: always double check that variable names are unique.
Thanks again for your help.