我尝试编译下面的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <term.h>
//#include "/usr/include/term.h"
void clear_screen(void) {
if (!cur_term) {
int result;
setupterm( NULL, STDOUT_FILENO, &result );
if (result <= 0)
return;
}
putp( tigetstr( "clear" ) );
}
int main(void) {
puts("I am going to clear the screen");
sleep(1);
clear_screen();
puts("Screen Cleared");
sleep(1);
clear_screen();
return 0;
}
term.h
此程序测试使用头文件清除终端屏幕的函数。我已经安装了所需的所有库:libncurses5
和,并且当我使用或不使用、和参数libncurses5-dev
编译程序时,我都会得到上述输出:-lncurses
-lcurses
-lterminfo
gcc
In file included from clear_screen_UNIX.c:5:0:
clear_screen_UNIX.c:9:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
void clear_screen(void) {
^
clear_screen_UNIX.c: In function ‘main’:
clear_screen_UNIX.c:23:14: error: called object is not a function or function pointer
clear_screen();
^
clear_screen_UNIX.c:26:14: error: called object is not a function or function pointer
clear_screen();
^
此外,当我term.h
使用其完整路径明确包含标题时/usr/include/term.h
,我得到此输出:
In file included from clear_screen_UNIX.c:7:0:
/usr/include/term.h:125:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
#define CUR cur_term->type.
^
/usr/include/term.h:202:40: note: in expansion of macro ‘CUR’
#define clear_screen CUR Strings[5]
^
clear_screen_UNIX.c:9:6: note: in expansion of macro ‘clear_screen’
void clear_screen(void) {
^
clear_screen_UNIX.c: In function ‘main’:
clear_screen_UNIX.c:23:14: error: called object is not a function or function pointer
clear_screen();
^
clear_screen_UNIX.c:26:14: error: called object is not a function or function pointer
clear_screen();
^
根据上述内容,term.h
头球内部存在一些错误,这是我不相信的。
那么我是否需要安装更多软件包或配置一些设置才能使代码成功编译?
附言:
- 代码示例来自这里
- 我的 Ubuntu 版本是 14.04
答案1
确实,clear_screen
是一个宏:
$ grep clear_screen /usr/include/term.h
#define clear_screen CUR Strings[5]
您必须使用其他名称,如链接的页面所示:
void Clear_screen (void)
编译器消息也说明了这一点:
clear_screen_UNIX.c:9:6: note: in expansion of macro ‘clear_screen’
这就是为什么错误似乎出现在那里term.h
——宏扩展导致了那里。