Solved

Problems Porting Code

Posted on 2003-11-10
7
395 Views
Last Modified: 2010-04-15
Hey everyone -

I wrote some source code on a Linux box (Red Hat), and I am trying to compile it on a different Linux Box using gcc.  Unfortunately, I am getting the error that there is a parse error on line 24.  (I marked line 24 in the code below).  Anyone know why this could be?  The Linux OS should be the same on both machines, the only difference is that one is an SMP, and the other is a laptop.  Thanks for any help you can give me.  

CODE SNIPPET -------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>


int main(int argc, char** argv)
{
      int n;

      if (argc < 2)
      {
            printf("Usage: %s <Times to Run>i\n", argv[0]);
            exit(0);      
      }

      else
      {
            n = atoi(argv[1]);
      }

      // initialize time & counter
// ERROR HERE------------->      time_t startTime = time(&startTime);      
      int counter = 0;
      
      // control program - runs ./primeGenerator n times
      while (counter < n)
      {
            pid_t PID0 = fork();
            
            if (PID0 == 0)
            {
                  // child process
                  execlp("./primeGenerator", "primeGenerator", "100000", NULL);
            }
            
            else
            {
                  counter++;
            }
      }
      wait(NULL);
      
      time_t endTime = time(&endTime);
      
      int secondsRun = difftime(endTime, startTime);
      printf("This program took %d seconds to run.\n", secondsRun);

      return 0;
}

-----------------------END CODE SNIPPET

Thanks!
~ace
0
Comment
Question by:Topace
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 25 total points
ID: 9717180

That line is the first place you used a c++ - ism.   I suspect you named this source file foo.c and it was foo.cpp on the other system.
Either that or the default on this compiler is to assume it's C, and on the other one it was vice-versa.

0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 25 total points
ID: 9717256

Hi Topace,

Grg99 doesn't come with English subtitles.  :)  But he does know his stuff!


Just move that line (and the next one) to the declaratives block (next to the "int n" statement).


Kent
0
 
LVL 22

Expert Comment

by:grg99
ID: 9717323
Subtitles, heck, I could use footnotes sometimes.

You can fix the program several ways:

rename foo.c   to foo.cpp

use the gcc compiler switch "-x c++"  to force a C++ compile.

Move all the declarations to the top of the function.

BTW your elapsed time variable  may work better if it's declared as a time_t.

 
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
LVL 1

Author Comment

by:Topace
ID: 9717920
I guess I didn't realize that C requires that you declare all of your variables in a specific place.  Is this true for all functions, or just the main function?

~ace
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9718511

All functions.

Actually, to C, main() is "just another function" so it must follow the same rules that apply to functions and function definitions.

Kent
0
 
LVL 1

Author Comment

by:Topace
ID: 9718553
Thanks a lot -

I appreciate your guys' speedy response.  I figured it wasn't too hard of a question, but you saved me a TON of headaches.  

~ace
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9719455

1 TON of headaches == 50 points

40 pounds of headaches == 1 point

Don't you wish T-Bones were that cheap?  ;)

Glad that we could help,
Kent

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL handling single and double quotes 3 97
Problem to scan all sheets 3 111
Computer slow / BSOD 10 64
Why isn't object file created? 6 68
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question