简体   繁体   English

用诅咒在C程序中创建一个框

[英]Create a box in C Program with Curses

I am trying to create a box with a game inside the box, but for now I am using the text this is my box for testing. 我试图创建一个盒子,盒子里面有一个游戏,但是现在我正在使用文本, this is my box要测试的this is my box I am very confusing with curses for first time but I am trying to learn for myself in my spare time. 我第一次对诅咒感到困惑,但是我想在业余时间自己学习。 I do not have any problems with other C program before but this time, I keep getting the message error after when I compile on Repl.it, but #include <windows.h> does not existing in the system file or on the Linux system too. 我之前在其他C程序上没有任何问题,但是这次,在Repl.it上编译后,我一直收到消息错误,但是#include <windows.h>在系统文件或Linux系统中不存在太。

#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>

int main(int argc, char ** argv){

  initscr();
  int height, width, start_y, start_x;
  height = 10;
  width = 20;
  start_y = start_x = 10;

  WINDOW * win = newwin(height, width, start_y, start_x);
  refresh();

  box(win, 0, 0);
  mvwprintw(win, 1, 1, "this is my box");
  wrefresh(win);

  int c = getch();

  endwin();



return 0;
}

Error Messages: 错误讯息:

gcc version 4.6.3
exit status 1
/tmp/cc3HSdBS.o: In function `main':
main.c:(.text+0x10): undefined reference to `initscr'
main.c:(.text+0x3e): undefined reference to `newwin'
main.c:(.text+0x49): undefined reference to `stdscr'
main.c:(.text+0x51): undefined reference to `wrefresh'
main.c:(.text+0x82): undefined reference to `wborder'
main.c:(.text+0xa6): undefined reference to `mvwprintw'
main.c:(.text+0xb2): undefined reference to `wrefresh'
main.c:(.text+0xb9): undefined reference to `stdscr'
main.c:(.text+0xc1): undefined reference to `wgetch'
main.c:(.text+0xc9): undefined reference to `endwin'
collect2: error: ld returned 1 exit status

Compiled with: 编译:

g++ -Incurses project.c -o project

You have to pass a linker flag to the compiler so that the ncurses library is linked at compile time. 您必须将链接器标志传递给编译器,以便在编译时链接ncurses库。 This flag is -lncurses . 该标志是-lncurses

From comments made by OP, the compiler invocation was: 根据OP的评论,编译器调用为:

g++ -Incurses project.c -o project

The initial l (ell) was mistakenly made into an I in the linker flag (an easy mistake to make). 最初的l (ell)在链接器标志中被错误地设为I (容易犯错误)。 Further, the linker flag is in the wrong position in this invocation. 此外,链接器标志在此调用中位于错误的位置。 Linker flags must follow their source files. 链接器标志必须跟随其源文件。 A better invocation would be: 更好的调用是:

g++ -o project project.c -lncurses

I am not sure why OP is using g++ here for C code; 我不确定为什么OP在这里使用g++编写C代码。 it might be better to use gcc directly. 最好直接使用gcc I would additionally suggest always enabling some warnings: 我还建议始终启用一些警告:

gcc -std=c11 -Wall -Wextra -Wpedantic -o project project.c -lncurses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM