C语言 意外标记“(”附近的语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18450014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
syntax error near unexpected token `('
提问by user2718341
Below is my code, it keeps telling me that line 10 is causing this "syntax error near unexpected token `('" but I cannot figure out why. I am adding to a code that was already written but the part where it says there is an error is not part of what I added. So I'm very confused about why I'm getting this error. Also I would like a good definition of what this error actually means.
下面是我的代码,它一直告诉我第 10 行导致了这个“意外标记附近的语法错误‘(’”,但我不知道为什么。我正在添加一个已经写好的代码,但它说的部分是错误不是我添加的内容的一部分。所以我对为什么会收到此错误感到非常困惑。此外,我想对这个错误的实际含义有一个很好的定义。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "applanix_data.h"
#include "applanix_pos_out.h"
#define DEGREES2RADIANS (3.141592654 / 180.0)
int output_group_1(FILE *fp, /* This is line 10 */
FILE *fpout,
unsigned short myyear,
unsigned short mymonth,
unsigned short myday,
double time_sod,
double double_time_met)
{
struct applanix_data_group1 data1;
struct pospacsbet sbet;
if(fread(&data1,sizeof(struct applanix_data_group1),1,fp)==1)
{
sbet.gpstime = time_sod;
sbet.latitude = data1.latitude * DEGREES2RADIANS;
sbet.longitude = data1.longitude * DEGREES2RADIANS;
sbet.altitude = data1.altitude;
sbet.x_velocity = data1.eVelocity;
sbet.y_velocity = data1.nVelocity;
sbet.z_velocity = data1.dVelocity;
sbet.roll = data1.aircraftRoll * DEGREES2RADIANS;
sbet.pitch = data1.aircraftPitch * DEGREES2RADIANS;
sbet.platform_heading = data1.aircraftHeading * DEGREES2RADIANS;
sbet.wander_angle = data1.aircraftWanderAngle * DEGREES2RADIANS;
sbet.x_body_acceleration = data1.aircraftTransverseAcceleration;
sbet.y_body_acceleration = data1.aircraftLongitudinalAcceleration;
sbet.z_body_acceleration = data1.aircraftDownAcceleration;
sbet.x_body_angular_rate = data1.aircraftAngularRateAboutDownAxis;
sbet.y_body_angular_rate = data1.aircraftLongitudinalAcceleration;
sbet.z_body_angular_rate = data1.aircraftAngularRateAboutDownAxis;
if(fwrite(&sbet,sizeof(struct pospacsbet),1,fpout)!=1)
{
fprintf(stderr,"Error writing POSPAC SBET output!\n");
exit(-2);
}
sbet.latitude1 = sbet.latitude * (180/3.141592654);
sbet.longitude1 = sbet.longitude * (180/3.14592654);
sbet.day = sbet.gpstime/86400;
sbet.time = sbet.gpstime/86400;
sbet.hour1 = (sbet.time - sbet.day);
sbet.hour = sbet.hour1*24;
sbet.time = sbet.hour1*24;
sbet.minute1 = (sbet.time - sbet.hour);
sbet.minute = sbet.minute1*60;
sbet.time = sbet.minute1 * 60;
sbet.second1 = (sbet.time - sbet.minute);
sbet.second = sbet.second1*60;
printf("%12.8f, %12.8f, %6.3f, %i:%i:%4.2f\n",sbet.longitude1,sbet.latitude1,sbet.altitude,sbet.hour, sbet.minute, sbet.second);
return 0;
}
else
return -1;
}
Editing the OP's comment into the question:
将 OP 的评论编辑为问题:
unix> g++ applanixraw2out.c
unix> ./applanixraw2out.c applanix_raw_20120508.bin > test.txt
./applanixraw2out.c: line 10: syntax error near unexpected token ('
回答by Keith Thompson
That's not a compiler syntax error, it's a shell error.
这不是编译器语法错误,而是 shell 错误。
You're trying to execute your C source code directly. The system is assuming the file is a shell script.
您正在尝试直接执行 C 源代码。系统假设该文件是一个 shell 脚本。
You need to compile it to an executable, and then run the executable:
您需要将其编译为可执行文件,然后运行该可执行文件:
$ gcc applanixraw2out.c -o applanixraw2out
$ ./applanixraw2out [arguments]
In a comment, you said you did the following:
在评论中,您说您执行了以下操作:
unix> g++ applanixraw2out.c
unix> ./applanixraw2out.c applanix_raw_20120508.bin > test.txt
./applanixraw2out.c: line 10: syntax error near unexpected token ('
The g++command is for C++ code; your code is C, so you should use gccinstead.
该g++命令用于 C++ 代码;你的代码是 C,所以你应该gcc改用。
You have to specify the name of the executable file, which is most commonly the source file name with .cremoved. If you don't, both g++and gccproduce an executable named a.outby default (for historical reasons). Use the -ooption to override that default.
您必须指定可执行文件的名称,最常见的是.c已删除的源文件名。如果你不这样做,既g++和gcc生成名为可执行a.out默认情况下(由于历史原因)。使用该-o选项覆盖该默认值。
Also, in order to get that syntax error you must have done something like:
此外,为了获得该语法错误,您必须执行以下操作:
$ chmod +x applanixraw2out.c
Setting execute permission on files that aren't meant to be executed is mostly harmless, but should be avoided since it can make errors like this more difficult to track down.
对不打算执行的文件设置执行权限通常是无害的,但应该避免,因为它会使此类错误更难以追踪。
回答by Brian Kintz
As starbluementioned in his comment, check the code in the two included header files. The error message should tell you exactly where the error occurred. Look at the line or two beforethe line listed in the error and make sure the syntax is correct. This error is almost always caused by a missing ;, (, {, etc on a preceding line.
正如starblue在他的评论中提到的,检查两个包含的头文件中的代码。错误消息应该告诉您错误发生的确切位置。查看错误中列出的行之前的一两行,并确保语法正确。这个错误几乎总是引起的缺失;,(,{前面的行等。

