Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems Porting Code

Posted on 2003-11-10
7
Medium Priority
?
417 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 100 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 46

Accepted Solution

by:
Kent Olsen earned 100 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 46

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 46

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

810 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