错误:ld在C返回1退出状态

时间:2015-09-26 10:19:26

标签: c linker-errors undefined-reference

我已经在DEV c ++和在线编译器中编译了我的CSMA-CD模拟器,但我得到了同样的错误,我不知道如何修复它。有什么想法吗?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


#define  MAX_STATIONS 10
#define  BUS_RATE 2000000.0
#define  PACKET_LENGTH 1000.0
#define  BUS_LENGTH 2000.0
#define  PERSIST 0.5
#define       MAX_BACKOFF 15.0
#define       JAM_PERIOD 5.0
#define       MAX_PACKETS 10000
#define       FACTOR 1000.0
#define        MAX_Q_SIZE 500
#define      ID_SIZE  5000
#define      DEGREES_FR 5



main(){
float arrival_rate;
float arrival_rate_slots;
float packet_time;
float t_dist_par[10]={12.706,4.303,3.182,2.776,2.571,2.447,2.365,2.306,2.262,2.226};
float start_time[ID_SIZE];
float event_time[MAX_STATIONS][4];
float delay_ci[DEGREES_FR+1];
float utilization_ci[DEGREES_FR+1];
float throughput_ci[DEGREES_FR+1];
float collision_rate_ci[DEGREES_FR+1];

float slot_size, p , ch_busy;
float rho, clock, d_clock, no_pkts_departed, next_event_time;
float x, logx, rand_size, infinite;
float delay,average_delay, total_delay;
float delay_sum, delay_sqr, delay_var, delay_sdv, delay_con_int ;
float utilization, utilization_sum, utilization_sqr;
float utilization_var,utilization_sdv,utilization_con_int;
float throughput, throughput_sum;
float collision_rate, collision_rate_sum, collision_end_time;
float select_prob, backoff_time, packet_slots;


int queue_size[MAX_STATIONS];
int queue_id[MAX_STATIONS][MAX_Q_SIZE];
int id_list[ID_SIZE];
int id_attempt_stn[MAX_STATIONS];

int i,j,ii,ic,next_station,next_event,next,id_number;
int no_attempts,no_trans, no_collisions,select_flag;

printf("The following results are for: \n");
printf("Degrees of freedom = %d\n",DEGREES_FR);
printf("Confidence Interval = 95 percent \n");
printf("========================================== \n");
printf("\n");
arrival_rate = 0.0;
slot_size = BUS_LENGTH * FACTOR / BUS_RATE;
packet_slots = (float) (int)  (packet_time/ slot_size)+1.0;
infinite = 1.0 * pow(10.0,30.0);
rand_size = 0.5 * pow(2.0,8.0 *sizeof(int));

for( ii=0; ii<10; ii++)
     {arrival_rate=arrival_rate+20.0;
     for(ic=0; ic<=DEGREES_FR; ic++)
     {
rho =0.0;
ch_busy =0.0;
clock =0.0;
d_clock=0.0;
collision_end_time=0.0;
utilization=0.0;
no_pkts_departed=0.0;
total_delay=0.0;
next_event_time=0.0;
average_delay=0.0;
no_collisions=0;
select_flag=0;
/* Compute the traffic tensity */
rho= arrival_rate * PACKET_LENGTH * MAX_STATIONS / BUS_RATE;
if(rho>=1.0)
 {printf("Traffic intensity too high");
 exit(1);
 }
/* Initialize all variables to their appropriate values */
arrival_rate_slots = arrival_rate * slot_size;
       for(i=0; i<MAX_STATIONS; i++) queue_size[i]=0;
       for(i=0; i<ID_SIZE; i++)
       {start_time[i]=0.0;
        id_list[i]=0;
       }
       for(i=0; i<MAX_STATIONS; i++)
       {
        for(j=0; j<MAX_Q_SIZE; j++)queue_id[i][j]=0;
 }
 for(i=0; i<MAX_STATIONS; i++)
  { for(j=0; j<4; j++)
    {event_time[i][j]=infinite;
     x = (float) rand();
     x = x * FACTOR/rand_size;
     if(j == 0) event_time[i][j]=x;
     }
  }
/* Scan the event list and pick the next event to be executed */ 
while(no_pkts_departed < MAX_PACKETS)
{ next_event_time= infinite;
  for(i=0; i<MAX_STATIONS; i++)
   { for(j=0; j<4; j++)
     {if(next_event_time > event_time[i][j])
      {next_event_time=event_time[i][j];
       next_station=i;
       next_event=j;
       }
     }
   } 
clock = next_event_time;
 if(next_event>3)
  {printf("Check the event list");  
   exit(1);
  }
 while(d_clock<=clock) d_clock++;
 switch(next_event)
 {
 case 0: /* This is an arrival event */
      { /*Select an identification for the arriving messaging */
       id_number = -1;
       for(i=0; i< ID_SIZE; i++)
        {if(id_list[i]==0)
         {id_number=i;
          id_list[i]=1;
          break;
         }
         if(id_number!= -1) continue;
        }
        if(id_number == -1)
        { printf("Check the ID-List");
         exit(1);
        }
        queue_size[next_station] ++;
        if( queue_size[next_station]> MAX_Q_SIZE)
          { printf("The queue size is large and is = %d\n",
            queue_size[next_station]);
            exit(1);
          } 
        queue_id[next_station][(queue_size[next_station]-1)]=id_number;
        start_time[id_number]= clock;
        if(queue_size[next_station] == 1 )
          { event_time[next_station][1]= d_clock;
            if(event_time[next_station][1]<= collision_end_time)
             event_time[next_station][1] = collision_end_time +1.0 ;
          }
    /* Schedule the next arrival */
     for(;  ;) 
     {
        x= (float)rand();
        if (x!=0.0) break;
     }
     logx= - log(x/rand_size) * FACTOR / arrival_rate_slots ;
     event_time[next_station][next_event] = clock + logx ;
     break;
     }     
 case 1: /* This is an attempt event */
 {
      no_attempts=0;
      for(i=0; i<MAX_STATIONS;i++)
      {
         if(event_time[i][1] == clock )
         { no_attempts++;
           id_attempt_stn[no_attempts-1]=i;
         }
      }  
      select_flag=0;
      if(no_attempts >1)
      { x=(float) rand();
        x= x/rand_size;
        for(i=0;i< no_attempts;i++)
        {
          select_prob=(float) (i+1)/((float)no_attempts);
          if(x<= select_prob)
          {
            next_station=id_attempt_stn[i];
            select_flag=1;
          }
        if(select_flag == 1) continue;
        }
      }
      if(ch_busy == 0.0 )
      {
       if(p == 0.0)
       { event_time[next_station][2] = clock +1.0;
         event_time[next_station][1] = infinite;
       }
       else
       {
        x=(float) rand();
        x= x/rand_size;
        if(x < p)
        {
          event_time[next_station][2] = clock +1.0;
          event_time[next_station][1]= infinite;
        }
        else
        {event_time[next_station][1] = clock + 1.0;
         if(event_time[next_station][1] <= collision_end_time)
         event_time[next_station][1]=collision_end_time+ 1.0;
         event_time[next_station][2]=infinite;
        } 
       }
      }
      if (ch_busy == 1.0)
      {
       if(p==0.0)
       {
        x=(float)rand();
        x = x/rand_size;
        backoff_time= (float) (int) (x * MAX_BACKOFF);
        if(backoff_time < 1.0)backoff_time=1.0;
        event_time[next_station][1]= clock + backoff_time;
        if(event_time[next_station][1] <= collision_end_time)
         event_time[next_station][1]=collision_end_time + backoff_time;
         event_time[next_station][2]=infinite;
        }
       else
       {
        event_time[next_station][1]=clock+ 1.0;
        if(event_time[next_station][1] <= collision_end_time)
         event_time[next_station][1]=collision_end_time + 1.0;
         event_time[next_station][2]=infinite;
       }
      }
      break;
   }
case 2: /* This is a transmission event */
{
     no_trans=0;
     for(i=0;i<MAX_STATIONS;i++)
     if(event_time[i][2] == clock ) no_trans++;
     if(no_trans > 1)
     {
       { collision_end_time= clock+ JAM_PERIOD + 2.0;
         no_collisions++;
       }
       for(i=0;i<MAX_STATIONS;i++)
       { if(event_time[i][2]== clock)
         {
          event_time[i][2]=infinite;
          x=(float) rand();
          x= x/rand_size;
          backoff_time= (float) (int) (x * MAX_BACKOFF);
          if(backoff_time< 1.0) backoff_time=1.0;
          event_time[i][1]=collision_end_time + backoff_time;
          }
          if(event_time[i][1]<=collision_end_time)
          {
           x=(float) rand();
           x= x/rand_size;
           backoff_time= (float) (int) (x * MAX_BACKOFF);
           if(backoff_time< 1.0) backoff_time=1.0;
           event_time[i][1]=collision_end_time + backoff_time;
          }
         }
       }
      else
       { if(ch_busy!=1.0)
         {
          event_time[next_station][3]= clock + packet_slots ;
          event_time[next_station][2] =infinite;
          ch_busy=1.0;
          }
          else
          { if(p==0.0)
           {x=(float) rand();
            x= x/rand_size;
            backoff_time= (float) (int) (x * MAX_BACKOFF);
            if(backoff_time<1.0) backoff_time=1.0;
            event_time[next_station][1]= clock + backoff_time;          
            if(event_time[next_station][1]<=collision_end_time)
            event_time[next_station][1]=collision_end_time + backoff_time;
            event_time[next_station][2]=infinite;
            }
            else
            {event_time[next_station][1]= clock +1.0;
             if(event_time[next_station][1]<=collision_end_time)
             event_time[next_station][1]=collision_end_time + 1.0;
             event_time[next_station][2]=infinite;
            }
           }
         }
         break;
     }
case 3: /* This is a departure event */
{ id_number=queue_id[next_station][0];
  ch_busy=0.0;
  queue_size[next_station]-- ;
  /* Push the queue forward */
  for(i=0;i< queue_size[next_station]; i++)
  queue_id[next_station][i]= queue_id[next_station][i+1];
  queue_id[next_station][queue_size[next_station]]=0;
  delay= clock - start_time[id_number];
  total_delay += delay;
  id_list[id_number]=0;
  no_pkts_departed += 1.0 ;
  utilization += packet_slots;
  event_time[next_station][3]= infinite ;
  if(queue_size[next_station] > 0 )
  {
    event_time[next_station][1]= clock +1.0 ;
    if(event_time[next_station][1] <= collision_end_time)
    event_time[next_station][1]= collision_end_time +1.0;
  }
  else
  { event_time[next_station][1]= infinite;
    event_time[next_station][2]=infinite;
  }
  break;
  }
 }
}
utilization=utilization/clock;
average_delay=total_delay *slot_size / (no_pkts_departed * FACTOR);
throughput=no_pkts_departed * FACTOR / (clock * slot_size);
collision_rate=(float) no_collisions * FACTOR / (clock * slot_size);
utilization_ci[ic]= utilization;
delay_ci[ic]=average_delay;
throughput_ci[ic]=throughput;
collision_rate_ci[ic]=collision_rate;
delay_sum=0.0;
delay_sqr=0.0;
utilization_sum=0.0;
utilization_sqr=0.0;
throughput_sum=0.0;
collision_rate_sum=0.0;
for(ic=0;ic<=DEGREES_FR;ic++)
   {
    delay_sum += delay_ci[ic];
    delay_sqr += pow(delay_ci[ic],2.0);
    utilization_sum += utilization_ci[ic];
    utilization_sqr += pow(utilization_ci[ic], 2.0);
    throughput_sum += throughput_ci[ic];
    collision_rate_sum += collision_rate_ci[ic];
   }
delay_sum=delay_sum / (DEGREES_FR +1);
delay_sqr=delay_sqr / (DEGREES_FR +1);
delay_var= delay_sqr - pow(delay_sum,2.0);
delay_sdv=sqrt(delay_var);
delay_con_int=delay_sdv + t_dist_par[DEGREES_FR-1]/sqrt(DEGREES_FR);
utilization_sum= utilization_sum / (DEGREES_FR +1);
utilization_sqr=utilization_sqr / (DEGREES_FR +1);
utilization_var=utilization_sqr - pow(utilization_sum,2.0);
utilization_sdv=sqrt(utilization_var);
utilization_con_int=utilization_sdv * t_dist_par[DEGREES_FR-1]/sqrt(DEGREES_FR);
throughput_sum= throughput_sum / (DEGREES_FR +1);
collision_rate_sum = collision_rate_sum / (DEGREES_FR +1);
printf("For an arrival rate = %g\n ",arrival_rate);
printf("The average delay = %g\n ",delay_sum);
printf(" +- %g \n", delay_con_int);
printf("The utilization = %g\n",utilization_sum);
printf(" +- %g \n", utilization_con_int);
printf("The throughput = %g\n",throughput_sum);
printf("The collision rate= %g\n",collision_rate_sum);
printf("\n");
getchar();
getchar();
}
}
}

这是错误消息

main.c:(.text+0x629): undefined reference to `log'                                                                                                                              
main.c:(.text+0x12a0): undefined reference to `pow'                                                                                                                             
main.c:(.text+0x1304): undefined reference to `pow'                                                                                                                             
main.c:(.text+0x13c4): undefined reference to `pow'                                                                                                                             
main.c:(.text+0x13ed): undefined reference to `sqrt'                                                                                                                            
main.c:(.text+0x1489): undefined reference to `pow'                                                                                                                             
main.c:(.text+0x14b2): undefined reference to `sqrt'                                                                                                                            
collect2: error: ld returned 1 exit status 

我将不胜感激任何帮助! 提前致谢

1 个答案:

答案 0 :(得分:1)

您应该指示链接器链接数学库。将-lm添加到您的gcc执行中。