Solved

Problems Porting Code

Posted on 2003-11-10
7
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 for-loops in the C programming language.

688 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