how do i compile this simple program???

Discussion in 'C Programming' started by David, Aug 27, 2003.

  1. David

    David Guest

    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!
     
    David, Aug 27, 2003
    #1
    1. Advertising

  2. David wrote:
    > 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
     
    Russell Hanneken, Aug 27, 2003
    #2
    1. Advertising

  3. David wrote:

    > 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.



    --
    Martin Ambuhl
     
    Martin Ambuhl, Aug 27, 2003
    #3
  4. David

    David Guest

    Russell Hanneken <> wrote in message news:<DTT2b.4780$>...
    > David wrote:
    > > 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.
     
    David, Aug 27, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    658
    Stig Brautaset
    Jul 23, 2003
  2. Replies:
    5
    Views:
    467
    mlimber
    Oct 12, 2005
  3. Replies:
    14
    Views:
    557
    Karl Heinz Buchegger
    Nov 2, 2005
  4. Nagaraj
    Replies:
    1
    Views:
    917
    Lionel B
    Mar 1, 2007
  5. RichardOnRails
    Replies:
    3
    Views:
    153
    RichardOnRails
    Jul 21, 2008
Loading...

Share This Page