how do i compile this simple program???

D

David

i am trying to compile the following with gcc on a RH 9.0 box:

#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>

/* resource.c */

int main(int argc,char* argv[]){
struct rusage r_usage;
struct rlimit r_limit;
getrusage(RUSAGE_SELF, &r_usage);
printf("User: %ld.%06ld\n",r_usage.ru_utime.tv_sec,
r_usage.ru_utime.tv_usec);
getrlimit(RLIMIT_FSIZE,&r_limit);
printf("Current FSIZE limit: soft = %ld, hard = %ld\n",
r_limit.rlim_cur,r_limit.rlim_max);
return 0;
}

with:

gcc -o resource resource.c

but it came back with the error that says 'struct rusage r_usage' is
undeclared (it also complain about 'struct rlimit'). i am sure that i
have missed something so gcc is not able to find the definition of the
rusage strrct but since this is the first time i am trying to program
in linux, i don't know how to solve that. i have googled around for a
while with no avil. can someone tell me what's wrong? thanks!
 
R

Russell Hanneken

David said:
i am trying to compile the following with gcc on a RH 9.0 box:

#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>

> [ . . . ]

but it came back with the error that says 'struct rusage r_usage' is
undeclared (it also complain about 'struct rlimit').

David,

Since your problem isn't really C-language-related, you'll have better
luck asking this on a newsgroup that deals with Linux. See the welcome
message for this group:

http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

For what it's worth, I compiled your code without problems on my FreeBSD
machine and on some Linux machine (I don't know if it was Red Hat).

Regards,

Russell Hanneken
(e-mail address removed)
 
M

Martin Ambuhl

David said:
i am trying to compile the following with gcc on a RH 9.0 box:

#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>

/* resource.c */

int main(int argc,char* argv[]){
struct rusage r_usage;
struct rlimit r_limit;
getrusage(RUSAGE_SELF, &r_usage);
printf("User: %ld.%06ld\n",r_usage.ru_utime.tv_sec,
r_usage.ru_utime.tv_usec);
getrlimit(RLIMIT_FSIZE,&r_limit);
printf("Current FSIZE limit: soft = %ld, hard = %ld\n",
r_limit.rlim_cur,r_limit.rlim_max);
return 0;
}

with:

gcc -o resource resource.c

but it came back with the error that says 'struct rusage r_usage' is
undeclared (it also complain about 'struct rlimit').

As you know, none of <sys/types.h>, <sys/resource.h>, <sys/time.h>, and
<unistd.h> are standard C, but are properties of your implementation & OS.
Not only does that make questions about the off-topic here, but may be
the key to your problem.

Apparently your installed version of gcc does not recognize the struct
definitions in <sys/resource.h>. This is what you expect when using -ansi
or -std=c89 or -std=c99 or their synonyms. Compiling your code with the
flags to specify the non-standard language -std=gnu89 or -std=gnu99 (I had
thought the default was -std=gnu89, but it seems otherwise for you), I find
those diagnostics disappear.

Even if that works (and check with a gcc newsgroup where it is topical),
you have an error in your code. In
> printf("User: %ld.%06ld\n",r_usage.ru_utime.tv_sec,
> r_usage.ru_utime.tv_usec);
r_usage.ru_utime.tv_sec should be a time_t, not a long. Simply casting it
with (long) should suffice. This would not normally be the way to do
things in comp.lang.c, since there is no reason to suppose that a time_t's
value will in general be representable in a long; however, your
non-standard application will -- for your particular and off-topic
implementation -- have no problem.
 
D

David

Russell Hanneken said:
David said:
i am trying to compile the following with gcc on a RH 9.0 box:

#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>

[ . . . ]

but it came back with the error that says 'struct rusage r_usage' is
undeclared (it also complain about 'struct rlimit').

David,

Since your problem isn't really C-language-related, you'll have better
luck asking this on a newsgroup that deals with Linux. See the welcome
message for this group:

http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

For what it's worth, I compiled your code without problems on my FreeBSD
machine and on some Linux machine (I don't know if it was Red Hat).

sorry for the OT question and i thank everyone for the answer so far.
i will repost the questino to the right group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top