Solved

Problems Porting Code

Posted on 2003-11-10
7
390 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pgpool-II on Ubuntu 14.04... ARGH! 5 843
Passing command line arguments in C 10 85
C++ finding a sting in a char* string from a text file 3 99
Problem to save 10 149
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now