I'm trying to integrate a small awk script into a C program, but every time I try to compile it, I get errors regarding the syntax, because gcc thinks that it's C even though it isn't.
我正在尝试将一个小的awk脚本集成到一个C程序中,但每次我尝试编译它时,我都会得到关于语法的错误,因为gcc认为它是C,即使它不是。
terminal_monitor = popen("awk '{if(l1){
download=($2-l1)/1024
download_round=sprintf("%.2f kB/s", download)
upload=($10-l2)/1024
upload_round=sprintf("%.2f kB/s", upload)
print download_round, upload_round
}
else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev) <(sleep 1; grep eth0 /proc/net/dev)", "r");
Here's the output of gcc(http://pastebin.com/fm8FAZjD):
这是gcc的输出(http://pastebin.com/fm8FAZjD):
root@ubuntu:~/Desktop# gcc monitor.c -o monitor
monitor.c: In function ‘main’:
monitor.c:8:26: warning: missing terminating " character [enabled by default]
terminal_monitor = popen("awk '{if(l1){
^
monitor.c:8:1: error: missing terminating " character
terminal_monitor = popen("awk '{if(l1){
^
monitor.c:9:5: error: ‘download’ undeclared (first use in this function)
download=($2-l1)/1024
^
monitor.c:9:5: note: each undeclared identifier is reported only once for each function it appears in
monitor.c:9:15: error: ‘$2’ undeclared (first use in this function)
download=($2-l1)/1024
^
monitor.c:9:18: error: ‘l1’ undeclared (first use in this function)
download=($2-l1)/1024
^
monitor.c:10:5: error: expected ‘)’ before ‘download_round’
download_round=sprintf("%.2f kB/s", download)
^
monitor.c:14:1: error: too few arguments to function ‘popen’
}
^
In file included from monitor.c:1:0:
/usr/include/stdio.h:873:14: note: declared here
extern FILE *popen (const char *__command, const char *__modes) __wur;
^
monitor.c:14:1: error: expected ‘;’ before ‘}’ token
}
^
monitor.c: At top level:
monitor.c:15:1: error: expected identifier or ‘(’ before ‘else’
else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev) <(sleep 1; grep eth0 /proc/net/dev)", "r");
^
monitor.c:15:21: error: expected identifier or ‘(’ before ‘}’ token
else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev) <(sleep 1; grep eth0 /proc/net/dev)", "r");
^
monitor.c:15:22: warning: missing terminating ' character [enabled by default]
else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev) <(sleep 1; grep eth0 /proc/net/dev)", "r");
^
monitor.c:15:21: error: missing terminating ' character
else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev) <(sleep 1; grep eth0 /proc/net/dev)", "r");
^
monitor.c:16:1: error: expected identifier or ‘(’ before ‘if’
if (terminal_monitor == NULL) {
^
monitor.c:20:1: error: expected identifier or ‘(’ before ‘while’
while (terminal_monitor!= NULL){
^
monitor.c:25:1: error: expected identifier or ‘(’ before ‘return’
return 0;
^
monitor.c:26:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
Is there any way to tell gcc that this isn't C and that it should be handled like a normal string?
有没有办法告诉gcc这不是C,它应该像普通字符串一样处理?
2 个解决方案
#1
2
What does "integrate" even mean? If you just want your C program to contain the AWK source, it needs to be a proper C string.
什么“整合”甚至意味着什么?如果您只是希望您的C程序包含AWK源,则它需要是一个合适的C字符串。
You can embed double quotes by escaping them with a backslash, "here is a quoted string: \"hello\""
.
您可以通过使用反斜杠转义它们来嵌入双引号,“这里是引用的字符串:\”hello \“”。
For longer pieces of text, it's handy to use C's automatic concatenation of adjacent string literals, it saves you from having to use line-continuation:
对于较长的文本片段,使用C的相邻字符串文字的自动连接很方便,它使您不必使用行继续:
const char *awkcode =
"awk '{if(l1){"
"download=($2-l1)/1024"
"download_round=sprintf(\"%.2f kB/s\", download)"
"upload=($10-l2)/1024"
"upload_round=sprintf(\"%.2f kB/s\", upload)"
"print download_round, upload_round"
"}"
"else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev)"
" <(sleep 1; grep eth0";
" /proc/net/dev)\", \"r\");"
This is hardly readable, but it can be improved.
这几乎不可读,但可以改进。
#2
1
Try adding backslash '\' at all the line endings. As every line in your program treated as separate instruction for C, it gives those errors you mentioned.
尝试在所有行结尾添加反斜杠'\'。由于程序中的每一行都被视为C的单独指令,因此它会给出您提到的错误。
#1
2
What does "integrate" even mean? If you just want your C program to contain the AWK source, it needs to be a proper C string.
什么“整合”甚至意味着什么?如果您只是希望您的C程序包含AWK源,则它需要是一个合适的C字符串。
You can embed double quotes by escaping them with a backslash, "here is a quoted string: \"hello\""
.
您可以通过使用反斜杠转义它们来嵌入双引号,“这里是引用的字符串:\”hello \“”。
For longer pieces of text, it's handy to use C's automatic concatenation of adjacent string literals, it saves you from having to use line-continuation:
对于较长的文本片段,使用C的相邻字符串文字的自动连接很方便,它使您不必使用行继续:
const char *awkcode =
"awk '{if(l1){"
"download=($2-l1)/1024"
"download_round=sprintf(\"%.2f kB/s\", download)"
"upload=($10-l2)/1024"
"upload_round=sprintf(\"%.2f kB/s\", upload)"
"print download_round, upload_round"
"}"
"else{l1=$2; l2=$10;}}' <(grep eth0 /proc/net/dev)"
" <(sleep 1; grep eth0";
" /proc/net/dev)\", \"r\");"
This is hardly readable, but it can be improved.
这几乎不可读,但可以改进。
#2
1
Try adding backslash '\' at all the line endings. As every line in your program treated as separate instruction for C, it gives those errors you mentioned.
尝试在所有行结尾添加反斜杠'\'。由于程序中的每一行都被视为C的单独指令,因此它会给出您提到的错误。