LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Old C program gives error only on new compiler (https://www.linuxquestions.org/questions/programming-9/old-c-program-gives-error-only-on-new-compiler-703449/)

davidstvz 02-09-2009 04:50 PM

Old C program gives error only on new compiler
 
I'm trying to compile this software called OSP. If you are familiar with it, this is OSP version one (written circa 1990). It compiles fine an this old Sun Solaris server, but on this new server running SUSE Linux 10 I get errors. Here's the command I'm using to compile:

cc -lm -w *.c

I'm probably just using bad command line options causing it to link incorrectly. I'm really rusty on my C code, but I need to get this program working so a professor can teach his OS course.

Here are the errors for the first file (all the remaining errors are similar):

Code:

cpu.c:198: error: static declaration of âfind_highest_priority_pcbâ follows non-static declaration
cpu.c:43: error: previous declaration of âfind_highest_priority_pcbâ was here
cpu.c:249: error: static declaration of âduplicate_pcbâ follows non-static declaration
cpu.c:94: error: previous implicit declaration of âduplicate_pcbâ was here

Here is the section of code containing Line 43 (43 is the last line in this snippet)

Code:

#include  "cpu.h"
#include  "cpu.private.h"

#define NULL          0

#include "ospmasks.h"

#define cost(pcb)      ( pcb->last_dispatch + pcb->last_cpuburst + start_cost(pcb) )

#define is_queue_empty() ( (ready_q.head == NULL) && (ready_q.tail == NULL) ?\
                                        true : false )

extern int    ______trace_switch;
extern int    ______MASK;

extern check_pcb();
extern check_page_table();
extern osp_abort();

PCB  *find_highest_priority_pcb();

Line 94 is the first if statement //i.e. if (!duplicate_pcb(pcb)) {

Code:

PUBLIC
insert_ready(pcb)
PCB *pcb;
{
    READY_Q *new_process;

    /* check PCB pointer */

    check_pcb(pcb,1,"CPU.insert_ready","","upon entering routine");
    if (!duplicate_pcb(pcb)) {
        pcb->status = ready;
        pcb->priority = cost(pcb);
        new_process = (READY_Q *)malloc(sizeof(READY_Q));
        new_process->pcb  = pcb;
        new_process->next = NULL;

      if (is_queue_empty() == true) {  /* is_queue_empty  is a macro  */
          ready_q.head = new_process;
          ready_q.tail = new_process;
      }
      else {
          ready_q.tail->next = new_process;
          ready_q.tail      = new_process;
      }
    }
    else
        printf("CLOCK> %6d#*** WARNING: CPU.insert_ready>\n\t\tTrying to insert duplicate PCB %d; duplicate discarded\n\n\n",get_clock(),pcb->pcb_id);

}  /* insert ready */

And here's how the find_highest_priority_pcb function is written. Line 198 is actually the line with the bracket on it following the function declaration.

Code:


PRIVATE
PCB *find_highest_priority_pcb()
{
    READY_Q  *lowest, *next_rdy, *tmp_ptr;  /* pointer to READY_Q  */
    PCB      *ret_pcb;

    lowest    = ready_q.head;    /* assume head pcb has the lowest cost */
    next_rdy  = ready_q.head;
    while(next_rdy != NULL){
      if (next_rdy->pcb->status != waiting &&
          next_rdy->pcb->priority < lowest->pcb->priority)
          lowest = next_rdy;
      next_rdy = next_rdy->next;
    }

    /* remove the lowest from the ready_q */
    if (lowest == ready_q.head)
        ready_q.head = ready_q.head->next;
    else {
          tmp_ptr = ready_q.head;
          while(tmp_ptr->next != lowest)
                tmp_ptr = tmp_ptr->next;
          if (tmp_ptr->next == ready_q.tail)
              ready_q.tail = tmp_ptr;
          tmp_ptr->next = lowest->next;
    }
    ret_pcb = lowest->pcb;
    free(lowest);
    return(ret_pcb);
}

And finally, here is how the duplicate_pcb function is written (this function doesn't appear in the declarations section)

Code:

PRIVATE
int duplicate_pcb(pcb)
PCB  *pcb;
{
  READY_Q *tmp_ptr;
  int    found;

  found  = FALSE;
  tmp_ptr = ready_q.head;
  while(tmp_ptr != NULL){
      if (tmp_ptr->pcb->pcb_id == pcb->pcb_id)
        found = TRUE;
      tmp_ptr = tmp_ptr->next;
  }
  return(found);
}

Any ideas?

ntubski 02-09-2009 06:36 PM

I can reproduce this with the following
Code:

int func();

static int func() {
    return 42;
}

cc compiles this, but gcc does not. Putting
Code:

static int func()
satisfies gcc. I'm guessing PRIVATE is #defined as static. So you'll need:
Code:

PRIVATE PCB  *find_highest_priority_pcb();
// not putting a declaration is like int function_name();
PRIVATE int duplicate_pcb(PCB *pcb);


davidstvz 02-09-2009 06:45 PM

I was hoping to avoid having to edit all the files. Oh well. Where do I need my PRIVATE/static? On both the declarations and the actual function I guess? NM, that is obvious. PRIVATE precedes each function so apparently it needs to precede each declaration as well. At least they should all be in a neat list.

EDIT:

Actually, I just noticed there are files like:

simcore.private.h

I don't see the word PRIVATE defined in any of the files.

ntubski 02-10-2009 01:21 PM

Quote:

I don't see the word PRIVATE defined in any of the files.
It's got to be somewhere.
Code:

grep -R '^#define[ \t]*PRIVATE' *
?

Quote:

I was hoping to avoid having to edit all the files.
If you can find where PRIVATE is #defined, and none of the different PRIVATE functions have the same name, you could get away with not having static in the declaration or definition. Just #define PRIVATE as nothing.

gnashley 02-11-2009 01:10 AM

Try using the '-ansi' option to the compiler, or '-std=C99'

davidstvz 02-11-2009 09:18 AM

Thanks guys, I will try both of these. I was busy all day yesterday with some administrative junk..

grep was my next move for finding the PRIVATE identifier and I thought there might be command line options for running gcc in an older mode for compatibility. Keeping my fingers crossed!

Ok, grep shows that PRIVATE is defined as... you guessed it... static (in three different files: cpu.private.h, files.private.h and memory.private.h ).

First I will try the compatibility options in gcc. I don't think I *can* remove the static identifier. A lot of these functions (this being an OS simulator) redefine things that are already defined which is probably why they are defined as static. Removing the static identifier will probably just give me a different set of errors.

EDIT: maybe I'm wrong about redefining PRIVATE to mean nothing. It's worth a shot anyway.

EDIT2: Darn, the standards change didn't help. Time to try the PRIVATE hack.

davidstvz 02-11-2009 10:43 AM

Ah crap, that didn't work because while some functions are declared as PRIVATE, some just use regular old "static"

Ok... how is this possible?

Code:

# grep page_in *
simcore.c:              total_num_page_in=0,
simcore.c:              total_num_lock_page_in=0,
simcore.c:      total_num_lock_page_in++;
simcore.c:        page_in(pcb, page_id, frame_id);  /* record that page is in  */
simcore.c:/*                    page_in()                                      */
simcore.c:static page_in(pcb, page_id, frame_id)
simcore.c:            total_num_page_in++;
simcore.c:                                                      total_num_page_in);
simcore.c:                                                      total_num_lock_page_in);
simcore.c:                                                  total_num_page_in
simcore.c:                                                  - total_num_lock_page_in);

page_in isn't even declared anywhere, it's merely called without it even being defined. I didn't know that was even possible, but it explains why it is referred to as an implicit declaration on line 477. Maybe I should just add declarations... although it's a pretty huge list. IF I could grep the lines where static functions are (without getting the lines where static variables are declared) it wouldn't be too hard to patch that in. Here are the errors.

simcore.c:503: error: static declaration of âpage_inâ follows non-static declaration
simcore.c:477: error: previous implicit declaration of âpage_inâ was here


All times are GMT -5. The time now is 01:55 PM.