意外标记"("附近的语法错误
syntax error near unexpected token '('
我正在用 c++ 在 unix 中做一些工作。我正在尝试在我的两个程序之间创建一个命名管道,并在它们之间来回发送一些文本。一切都编译正常,但是当我进行系统调用以运行服务器时.cpp我收到此错误消息。
./server.cpp: line 8: syntax error near unexpected token '('
./server.cpp: line 8: 'void test()'
导致此错误的原因是什么?我对 unix 或命名管道没有太多经验,所以我有点难倒。
这是我的代码
客户端.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int fd;
mkfifo("home/damil/myPipe", 0666);
fd=open("home/damil/myPipe", O_WRONLY);
write(fd,"test", sizeof("test")+1);
system("./server.cpp");
close(fd);
return 1;
}
服务器.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
void test()
{
int fd;
char * comm;
fd = open("home/damil/myPipe", O_RDONLY);
read(fd, comm, 1024);
printf(comm);
close(fd);
}
这不是C++错误,而是UNIX错误。通过运行system("./server.cpp")
,您尝试运行.cpp
文件,就好像它是编译的可执行文件一样。系统认为它是一个 shell 脚本,一旦超过#include
s(在 shell 中,它被解析为注释,因此被忽略),就会遇到语法错误。
您需要编译server.cpp
并运行生成的二进制文件。(注意:您可能希望将test()
重命名为main()
。
g++ -Wall -o server server.cpp
然后在client.cpp
中,将系统调用更改为:
system("./server");
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- 如何摆脱错误c2061;语法错误 cout?
- 语法错误:D3DX10Core 中的标识符'TEXTMETRICA'
- 错误 C2146:语法错误:标识符"A1"之前缺少","
- main.cpp(11):错误 C2059:语法错误:"字符串"
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 如何修复此 FSM C++语法错误 C3867
- 函数错误 C2059:语法错误:'>'不起作用
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- c++default-int中的语法错误
- "pip install openexr"上的语法错误:错误的编译器?
- 为什么我收到语法错误:在"*"之前缺少";"
- 错误 C2059:语法错误:从结构成员声明'constant'
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误