Solved

Problems Porting Code

Posted on 2003-11-10
7
388 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:
Kdo 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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:Kdo
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:Kdo
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now