Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ compile error

  • 24-01-2008 11:13AM
    #1
    Registered Users, Registered Users 2 Posts: 357 ✭✭


    Trying to compile this code on ubuntu with g++. Getting these errors.

    utime.cpp: In function ‘int main(int, char**)’:
    utime.cpp:27: error: ‘exit’ was not declared in this scope
    utime.cpp:35: error: ‘atoi’ was not declared in this scope
    utime.cpp:57: error: ‘usleep’ was not declared in this scope

    Can anyone see where I might be going wrong?
    Cheers for any help.
    #include<stdio.h>
    
    #include<time.h>
    
    #include <sys/time.h>
    
    #include <sys/resource.h>
    
     
    
    int main(int argc, char** argv){
    
     
    
    struct  timeval tv;
    
    struct  timezone tz;
    
    int i,delay,num_iter;
    
     double init,start,stop;
    
    if (argc!=3) {
    
        fprintf(stderr, "Usage: %s <sleep time..usec><num_iteration>\n", argv[0]);
    
        exit(1);
    
      }
    
     
    
    //  progname=argv[0];
    
      delay=atoi(argv[1]);
    
      num_iter=atoi(argv[2]);
    
      printf("Delay is %d usec..num_iter is %d\n",delay,num_iter);
    
      gettimeofday( &tv,&tz);
    
      init=tv.tv_sec + tv.tv_usec*0.000001;
    
      for(i=0;i<num_iter;++i)
    
        {
    
          gettimeofday( &tv,&tz);
    
          start=tv.tv_sec + tv.tv_usec*0.000001;
    
         
    
          // Now sleep
    
          usleep(delay);
    
          gettimeofday( &tv,&tz);
    
          stop=tv.tv_sec + tv.tv_usec*0.000001;
    
               
    
          printf("Iteration %d..slept for %lf usec\n",i+1,(stop-start)*1000000);
    
        }
    
     
    
    printf("Total time taken : actual %lf  theory(excl. runtime): %d,   usec \n",(stop-init)*1000000,num_iter*delay);
    
    return 0;
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 3,875 ✭✭✭ShoulderChip


    sorry, no.


  • Subscribers Posts: 4,077 ✭✭✭IRLConor


    #include <stdlib.h>
    
    ?

    When you get errors like that, look up the manpage for the missing functions (e.g. "man atoi") and it will usually tell you what you need to include at the top.


  • Registered Users, Registered Users 2 Posts: 357 ✭✭apoch632


    Cheers man. I'd a feeling it was something silly like that i left out.


Advertisement