Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
答网友问:用GCC开发控制台程序的,可以使用ncurses替代conio.h
Apps Section
1149
views ·
2
replies ·
To
floor
Go
xliang9550
deepin
2016-03-06 23:44
Author
最近有网友对gcc不支持conio.h控制台I/O表示困惑。本人借了个梯子出去找到了一些解决方案。
conio.h不属于C语言标准库,即使是在DOS/Windows平台,微软和Borland的编译器所支持的conio.h也提供了不同的函数和接口。(转自stackoverflow上的回帖)
由于conio.h属于非标准并且事实上已被废弃的功能,当然不建议各位继续使用。目前比较一致的建议是使用ncurses作为替代解决方案。
从软件源安装ncurses之后即可用以下简单的程序测试:
#include
int main(void)
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}
附注:1,包含ncurses.h将会自动包含stdio.h
2,编译的时候需要手动链接ncurses库,需要为gcc命令行添加-lncurses参数
最终的编译命令行如下:
gcc 源文件名 -lncurses
Reply
Like 0
Favorite
View the author
All Replies
linux_5120
deepin
2016-03-07 04:58
#1
谢谢楼主!
Reply
Like 0
View the author
xliang9550
deepin
2016-03-07 08:57
#2
补充一下,conio.h可以作为鉴别C语言垃圾教材的方法。代码中仍然在使用conio.h的教材都是劣书,因为这些程序完全不具备可移植性。
另外,ncurses已经通过MinGW移植到Windows平台,具备了一定的通用性和可移植性。
回忆一件往事,当年我学的编程课用的老谭的昏昏昭昭教材,结果几位获得优秀成绩的学霸用的大多是conio.h中的控制台I/O功能实现各种效果。
幸亏当年我完全没有下功夫去学,不然需要下更多功夫来解毒。当时中毒最深的就是那几位学霸,后来每次交谈之时,对方必然痛批当年的以其昏昏使人昭昭的课程……
Reply
Like 0
View the author
Please
sign
in first
Featured Collection
Change
Nuntia - Modern IMAP/SMTP mail client for Deepin (Beta tester wanted!)
[Tutorial] deepin25 WSL Offline Installation Guide
UOS AI 2.8 Released! Three New Intelligent Agents & Major Evolution
Solid Q&A | deepin 25 Common Questions – The Immutable System Edition
New Thread
Popular Ranking
Change
Adding a Dancing Spectrum Bar to the Deepin Taskbar | deepin-spectrum
Popular Events
More
conio.h不属于C语言标准库,即使是在DOS/Windows平台,微软和Borland的编译器所支持的conio.h也提供了不同的函数和接口。(转自stackoverflow上的回帖)
由于conio.h属于非标准并且事实上已被废弃的功能,当然不建议各位继续使用。目前比较一致的建议是使用ncurses作为替代解决方案。
从软件源安装ncurses之后即可用以下简单的程序测试:
#include
int main(void)
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}
附注:1,包含ncurses.h将会自动包含stdio.h
2,编译的时候需要手动链接ncurses库,需要为gcc命令行添加-lncurses参数
最终的编译命令行如下:
gcc 源文件名 -lncurses