不同gcc版本的语法错误?

时间:2021-10-06 22:43:47

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:

我用Ubuntu Linux在C中编写了一个程序,现在我需要将它移植到UNIX机器上(或者我认为是UNIX机器)。它在我的Ubuntu和GCC上编译得很好但是当我尝试在UNIX框上用GCC编译它时,它会出现这个错误:

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)

主要问题似乎是char之前的解析错误(其他相关)

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?

这些只是简单的C风格的字符串声明。是否有一个头文件,我需要包含它以使其在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.

附:什么是命令,看看你正在使用什么版本的unix和哪个版本的gcc?知道这一点将使我在我的问题中更具体。

Thanks

9 个解决方案

#1


3  

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.

如果要编译纯C,则必须在函数的初始化时声明变量。我提到这一点是因为大多数人使用C ++编译器编译他们的C程序,这些编译器提供了一些通常不能用于纯C编译器的资源,最常见的例子是//注释行。

#2


2  

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

如果要确保代码是可移植的,请始终使用-pedantic或-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.

虽然我们在这个问题上。您可能还应该打开所有警告。编译器警告你的原因很充分;当代码从一个平台移动到另一个平台时,这些警告是潜在错误的根源,因为新的硬件/ OS /编译器可能与您当前的操作不同。

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前端可执行文件:g ++会将* .c文件视为C ++文件,除非您明确告诉它。所以,如果你正在编译真正的C,那么使用gcc而不是g ++。

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

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.

为了帮助您解决特定问题,可能会看到第43行。虽然错误说明了第44行,但很多问题都是由于行程有问题而且问题没有被解析器检测到,直到你找到第一个lexeme在下一行。

#3


1  

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

#4


1  

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:

如果您只需要在其他平台上定位GCC,而不是任何其他编译器,那么您可以尝试:

-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.

但我猜这可能允许在较新的GCC上使用GNU扩展,而旧版GCC不支持这种扩展。我不确定。

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.

请注意,尽管如果-pedantic保证警告所有非标准程序会很好,但事实并非如此。还有一些它错过的东西。

#5


1  

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.

您将不得不为错误提供更多上下文...至少有一行,可能是第44行和第54行之前的几行。猜测一下,如果您在第44行之前给出了函数定义开头的代码(也许第40行左右)到第54行(或者几行之后 - 可能是第60行 - 然后我们也可以提供帮助。第44行之前的某些信息是因为它导致它除了'char'以外的某些东西第44行;第54行同上(可能是同一个问题)。

#6


1  

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.

信息不足。上面的代码至少是有趣的,人们必须知道是否谈论ANSI C89或ANSI C99。从广义上讲,第一个答案是错误的。

Regards Friedrich

#7


1  

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?

第一条错误消息显示“在'char'之前解析错误”。 char之前的代码是什么?这是函数声明吗?它是否包含任何用户定义的类型或任何类型的东西?

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.

这个错误的最可能的来源是第44行上方的某些内容使用了在头文件中声明的类型或宏...该头文件可能在您自己的Ubuntu系统和您尝试编译的系统之间有所不同。

#8


0  

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

UNIX是什么? AIX,Ultrix,Minix,Xenix?

GCC has a "--version" flag:

GCC有一个“--version”标志:

gcc --version

#9


0  

To display the GCC version:

要显示GCC版本:

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.

它可能有助于显示函数,以便我们可以看到周围的代码。这通常是“先解析错误”类型错误的问题。

#1


3  

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.

如果要编译纯C,则必须在函数的初始化时声明变量。我提到这一点是因为大多数人使用C ++编译器编译他们的C程序,这些编译器提供了一些通常不能用于纯C编译器的资源,最常见的例子是//注释行。

#2


2  

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

如果要确保代码是可移植的,请始终使用-pedantic或-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.

虽然我们在这个问题上。您可能还应该打开所有警告。编译器警告你的原因很充分;当代码从一个平台移动到另一个平台时,这些警告是潜在错误的根源,因为新的硬件/ OS /编译器可能与您当前的操作不同。

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前端可执行文件:g ++会将* .c文件视为C ++文件,除非您明确告诉它。所以,如果你正在编译真正的C,那么使用gcc而不是g ++。

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

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.

为了帮助您解决特定问题,可能会看到第43行。虽然错误说明了第44行,但很多问题都是由于行程有问题而且问题没有被解析器检测到,直到你找到第一个lexeme在下一行。

#3


1  

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

#4


1  

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:

如果您只需要在其他平台上定位GCC,而不是任何其他编译器,那么您可以尝试:

-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.

但我猜这可能允许在较新的GCC上使用GNU扩展,而旧版GCC不支持这种扩展。我不确定。

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.

请注意,尽管如果-pedantic保证警告所有非标准程序会很好,但事实并非如此。还有一些它错过的东西。

#5


1  

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.

您将不得不为错误提供更多上下文...至少有一行,可能是第44行和第54行之前的几行。猜测一下,如果您在第44行之前给出了函数定义开头的代码(也许第40行左右)到第54行(或者几行之后 - 可能是第60行 - 然后我们也可以提供帮助。第44行之前的某些信息是因为它导致它除了'char'以外的某些东西第44行;第54行同上(可能是同一个问题)。

#6


1  

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.

信息不足。上面的代码至少是有趣的,人们必须知道是否谈论ANSI C89或ANSI C99。从广义上讲,第一个答案是错误的。

Regards Friedrich

#7


1  

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?

第一条错误消息显示“在'char'之前解析错误”。 char之前的代码是什么?这是函数声明吗?它是否包含任何用户定义的类型或任何类型的东西?

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.

这个错误的最可能的来源是第44行上方的某些内容使用了在头文件中声明的类型或宏...该头文件可能在您自己的Ubuntu系统和您尝试编译的系统之间有所不同。

#8


0  

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

UNIX是什么? AIX,Ultrix,Minix,Xenix?

GCC has a "--version" flag:

GCC有一个“--version”标志:

gcc --version

#9


0  

To display the GCC version:

要显示GCC版本:

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.

它可能有助于显示函数,以便我们可以看到周围的代码。这通常是“先解析错误”类型错误的问题。