views:

678

answers:

9

I wrote a program in C with Ubuntu Linux and now I need to port it over to a UNIX machine (or what I believe to be a UNIX box). It compiles fine on my Ubuntu with GCC but when I try to compile it with GCC on the UNIX box, it gives this error:

a.c: In function `goUpDir':
a.c:44: parse error before `char'
a.c:45: `newDir' undeclared (first use in this function)
a.c:45: (Each undeclared identifier is reported only once
a.c:45: for each function it appears in.)
a.c: In function `goIntoDir':
a.c:54: parse error before `char'
a.c:57: `newDir' undeclared (first use in this function)
a.c:57: `oldDir' undeclared (first use in this function)

The main problems seem to be the parse error before char (the others are related)

44  char newDir[50] = ""; 
54  char* oldDir = (char*)get_current_dir_name();

These are just simple C-style strings declarations. Is there a header file that I need to include to get it to work in UNIX?

P.S. what is the command to see what version of unix and which version of gcc you are using? Knowing this will allow me to be more specific in my question.

Thanks

+2  A: 

If you are compiling pure C, variables must be declared on the beggining of the functions. I mention this because most people compile their C programs using C++ compilers, which offers then some resources not normally available to pure C compilers, the most common example being the // comment lines.

Leahn Novash
Hahahah, nice. I took that one for granted, I thought most C coders knew that, but I suppose some take C99 semantics for granted too. :-P
Chris Jester-Young
It's 9 years later. I consider non-support of C99 to be a bug.
wnoise
This answer is incorrect in a number of ways. I don't know what "pure C" means, because in C99 you can certainly declare variables after statements.Also, even in C89, which is what I believe the answer relates to, it's correct to say they must be declared at the beginning of blocks, not functions.
Chris Young
@wnoise: you may be right, but GCC doesn't implement C99 (not fully), and it's GCC that was specified in the question.
Steve Jessop
@Leahn, while it's true that strict C89 doesn't allow variable declarations, GCC *does* understand these. It will *not* give a parse error, but instead the following warning: "ISO C90 forbids mixed declarations and code".
Dan
A: 

What UNIX is it? AIX, Ultrix, Minix, Xenix?

GCC has a "--version" flag:

gcc --version
JesperE
A: 

To display the GCC version:

gcc --version

It might help to show the function so we could see the surrounding code. That is often the problem with "parse error before" type errors.

crashmstr
+1  A: 

How did you copy the file over? Is it possible that you inserted something that shouldn't be there?

BTW: Please fix up your use of the code tag in your code - it's currently nearly impossible to read without using "view source" in my browser.

As for you end questions:

uname -a
gcc -v
Paul Tomblin
+2  A: 

If you want to make sure your code is portable always use the -pedantic or -pedantic-errors.

This will provide warnings/errors where your code strays from standards compliance.

While we are on the subject. You should probably also turn on all the warnings. There are good reasons why the compiler warns you; when moving code from one platform to another these warnings are the source of potential bugs as the new hardware/OS/Compiler may not act the same as your current one.

Also use the correct GCC frontend executable: g++ Will treat *.c files like C++ files unless you explicitly tell it not too. So if you are compiling real C then use gcc not g++.

gcc -pedantic -Wall -Werror *.c
g++ -pedantic -Wall -Werror *.cpp

To help with your specific problem it may be nice to see line 43. Though the error says line 44 a lot of problems are caused by the proceeding line having a problem and the problem not being detected by the parser until you get to the first lexeme on the next line.

Martin York
+1  A: 

When trying to write portable code, the following compiler flags will tell you about a lot of problems before you get as far as trying to compile the code on the next platform:

-std=c89 -pedantic -Wall

If you only need to target GCC on other platforms, not any other compilers, then you could try:

-std=gnu89 -pedantic -Wall

But I'd guess this might allow GNU extensions on a newer GCC that aren't supported on an older one. I'm not sure.

Note that although it would be nice if -pedantic was guaranteed to warn about all non-standard programs, it isn't. There are still some things it misses.

Steve Jessop
+1  A: 

You will have to provide us with more context for the errors...at least one, probably several lines before lines 44 and 54. At a guess, if you give us the code from the start of the function definition before line 44 (perhaps line 40 or so) through to line 54 (or a few lines later - maybe line 60 - then we may be able to help. Something is up with the information before line 44 that is causing it to expect something other than 'char' at line 44; ditto (probably the same problem) at line 54.

Jonathan Leffler
+1  A: 

The information is insufficient. The code above is at least as intersting and one has to know if ones talks about ANSI C89 or ANSI C99. The first answer is wrong in that broad sense.

Regards Friedrich

Friedrich
+1  A: 

Steve,

The first error message says "parse error before 'char'". What is the code that precedes char? Is it a function declaration? Does it include any user-defined types or anything of the sort?

The most likely source of this error is that something shortly above line 44 uses a type or macro that's declared in a header file... that header file may differ between your own Ubuntu system and the one you're trying to compile on.

Dan