2014-02-10 72 views
-2

我正在做一些工作在unix與c + +我試圖創建一個命名管道之間我的兩個程序,併發送一些文本來回之間。我讓我的系統調用運行server.cpp我收到此錯誤信息。語法錯誤附近意想不到的令牌'''''

./server.cpp: line 8: syntax error near unexpected token '(' 
./server.cpp: line 8: 'void test()' 

是什麼原因造成這個錯誤?我沒有與UNIX或太多的經驗命名管道,所以我也有點爲難。

這是我的代碼

client.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; 
} 

server.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); 
} 
+4

你不應該運行的二進制文件,而不是.cpp文件? – Xarn

+0

server.cpp還需要一個'main()'函數(除其他外) – ryyker

回答

5

這不是一個C++錯誤,而是UNIX錯誤。通過運行system("./server.cpp"),您試圖運行.cpp文件,就好像它是一個編譯的可執行文件。系統認爲它是一個shell腳本,只要它經過了#include(它在shell中被解析爲註釋並因此被忽略)就會觸發語法錯誤。

您需要編譯server.cpp並運行生成的二進制文件。 (注:你可能要重新命名test()main()。)

g++ -Wall -o server server.cpp 

然後在client.cpp,更改系統調用:

system("./server"); 
相關問題