在使用ncurses时遇到“Undefined reference to 'stdscr'”错误

16

我正在尝试在Ubuntu 11.10中编译我的代码,但是出现了这些错误和更多错误。通过谷歌搜索,我认为这是一个链接错误。具体来说,有建议要确保您拥有正确的头文件并链接-lncurses库。我已经做到了。但是我仍然收到此错误。我还读到可能需要安装libncurses,但我已经安装了它。

My MakeFile:
CPP           = g++
CPPFLAGS      = -c -Wall -g
LINK          = g++
LDFLAGS_LINUX = -lpthread -lncurses
LDFLAGS       = $(LDFLAGS_LINUX)
RM            = rm


.SUFFIXES:
.SUFFIXES: .o .cpp

.cpp.o:
    $(CPP) $(CPPFLAGS) $*.cpp -o $(SRC_DIR)$*.o

all: skygrid

skygrid: skygrid.o commServer.o pose.o robot.o
    $(LINK) $(LDFLAGS) -o $@ $^

clean:
    $(RM) -rf *.o skygrid

skygrid.o:  skygrid.cpp definitions.h commServer.h pose.h robot.h
commServer.o:   commServer.cpp commServer.h
pose.o:     pose.cpp pose.h
robot.o:    robot.cpp robot.h pose.h

我的错误:

/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1094: undefined reference to `stdscr'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1094: undefined reference to `stdscr'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1094: undefined reference to `stdscr'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1094: undefined reference to `stdscr'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1104: undefined reference to `werase'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1106: undefined reference to `wprintw'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1107: undefined reference to `wprintw'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1109: undefined reference to `wprintw'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1111: undefined reference to `stdscr'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1111: undefined reference to `wgetch'
/home/fari/Desktop/FarahSkygrid/skygrid/src/skygrid.cpp:1116: undefined reference to `wtouchln'

1
惯例是使用CPP作为预处理器,CPPFLAGS作为预处理器的标志,CXX作为C++编译器,CXXFLAGS作为CXX的标志。将CPP指定为C++编译器会破坏make的隐式规则。 - William Pursell
1
感谢所有尝试帮助我的人。实际上,我已经不再使用这段代码,也无法访问它。因此,除了Keith Thompson建议的内容外,我没有尝试任何其他方法,但那对我没有起作用。对于其他可能遇到相同问题的人,请尝试其他答案,并在它们有用时标记它们。 - Fari
1
我曾经遇到过同样的问题,解决方法是在末尾使用-l:gcc -o hello hello.c -lncurses - jcubic
8个回答

33

我在Centos 6.2上遇到了一个与ncurses程序有关的问题。原来ncurses有时会被分为两个库,即ncursestinfo。在我的情况下,stdscr存在于libtinfo中,而不是libncurses中,因此在链接行中添加-ltinfo(在-lncurses之后)可以解决这个问题。


2
我只是缺少了 -lncurses 标志。如果没有这篇文章,我就不会想到它 :) - simgineer

5

2

user1246043,

我遇到了类似的问题。基本上,我无法使用g++ 4.5或g++ 4.6进行编译,所以我安装了g++ 4.4并使用它。这特别解决了我与ncursesw链接的问题。

# Makefile:
CXX=g++-4.4                                                                  
CXXLIBS=-lncursesw                                                           
CXXFLAGS=-Wall
# Other stuff omitted

1
由于错误信息指向您的skygrid.cpp源文件中的特定行,所以这些不是链接器错误。
您可能需要添加
#include <curses.h>

返回该源文件的顶部。

谢谢您的回复。我已经在我的文件中包含了#include <ncurses.h>。这段代码可以编译,并且已经在其他安装较旧版本Ubuntu和Mac(忘记确切的操作系统)的计算机上运行,但是在我的Ubuntu 11.10计算机上无法运行。 - Fari
奇怪。我没有你的源代码,但是我在我的Ubuntu 11.04系统上自己编写了一个小例子,它可以正常工作。https://gist.github.com/1968401 - Keith Thompson
谢谢,感谢您的帮助。我会尽快在我的电脑上尝试您的示例代码,并告诉您它是否有效。 - Fari
你好, 我尝试了你的代码,但仍然出现相同的未定义引用错误。 fari@ubuntu:/Desktop/test$ make g++ -c -Wall -g skygrid.cpp -o skygrid.o g++ -lpthread -lncurses -o skygrid skygrid.o skygrid.o: In function main': /home/fari/Desktop/test/skygrid.cpp:4: undefined reference to stdscr' collect2: ld returned 1 exit status make: *** [skygrid] Error 1 fari@ubuntu:/Desktop/test$ - Fari

1

在构建cscope 15.8时,我遇到了同样的问题。

在运行./configure后,首先出现了错误,提示找不到“ncurses.h”。这是因为我的操作系统上没有安装libncurses-dev。

安装了libcurses-dev之后,我直接运行了make。然后得到了几十个“undefined reference to”错误。

重新从头开始重建后,这些错误就消失了。

./configure

make


0

我也遇到了同样的问题。

我需要使用 "strftime" C 函数。 看起来 "ncurses" 库包含它,在现代 Linux/Unix 发行版上可以轻松下载和安装(我正在使用 FreeBSD 13.1)。

然后,我必须在 C/C++ 文件中包含 "curses.h",并在编译时还必须使用 ncurses 标志:"-lncurses"

这是我用于编译的命令:

clang++ -v -g strftime.cpp -o strftime -lncurses 

我需要在C++中使用"strftime",并且它对我起作用。


ncurses不使用strftime - Thomas Dickey

0
我曾经遇到过类似的问题,当使用Qt计时器和ncurses时:timeout宏破坏了Qt代码=) 解决方案是在#include <ncurses.h>之后添加"#undef timeout"以防止宏起作用。如果在某些情况下您需要ncurses超时,则可以直接运行wtimeout(stdscr,delay)

由于某些原因,我得到了相同的错误。undefined reference to stdscr; undefined reference to wgetch - David G

0

通过使用正确的ncurses库和标志,我解决了我的gcc链接问题,这些库和标志是通过ncurses5-config获得的。

`ncurses5-config --cflags --libs`添加到Makefile编译参数中,应用程序最终链接成功。

为您的目的使用正确的ncurses配置工具版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接