C++ 使用 ncurses 时对 `stdscr' 的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9541679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:57:17  来源:igfitidea点击:

Undefined reference to `stdscr' while using ncurses

c++compilationlinkerncurses

提问by Fari

I am trying to compile my code in Ubuntu 11.10 and getting these errors and more.So far by googling it I think it is a linking error. Specifically, there have been suggestions to make sure you have the right headers and link the -lncurses library. I have already done that. I'm still getting this error. I also read that may be i should install the libncurses, but I already have it installed.

我正在尝试在 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

My Errors:

我的错误:

/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'

回答by Ogre

I was having this problem with an ncurses program on Centos 6.2. It turns out that ncurses is sometimes split into two libraries, ncursesand tinfo. In my case, stdscrexists in libtinfo, not in libncurses, so adding -ltinfoto the link line, after -lncurses, solved the problem.

我在 Centos 6.2 上的 ncurses 程序中遇到了这个问题。事实证明,ncurses 有时会分成两个库,ncursestinfo. 在我的情况下,stdscr存在于 中libtinfo,而不是在 libncurses 中,因此添加-ltinfo到链接行之后-lncurses,解决了问题。

回答by ezra buchla

I know you've moved on, but for future reference: when I encountered the same problem, it was due to the placement of the -l argument. Try:

我知道你已经继续前进,但为了将来参考:当我遇到同样的问题时,这是由于 -l 参数的位置。尝试:

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

For more information, see the gcc manual: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

有关更多信息,请参阅 gcc 手册:http: //gcc.gnu.org/onlinedocs/gcc/Link-Options.html

and this thread on SO: Placement of `-l' option in gcc

以及关于 SO 的这个线程: Placement of `-l' option in gcc

回答by Keith Thompson

Since the error messages refer to specific lines in your skygrid.cppsource file, they're not linker errors.

由于错误消息涉及skygrid.cpp源文件中的特定行,因此它们不是链接器错误。

You probably need to add

您可能需要添加

#include <curses.h>

to the top of that source file.

到该源文件的顶部。

回答by baffled

user1246043,

用户1246043,

I've been having a similar problem. Basically, I can't compile wiht g++ 4.5 or g++ 4.6, so I installed g++ 4.4 and used that. This specifically solved my problem with linking to ncursesw.

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

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

回答by langdead

I came up with the same problem when building cscope 15.8.

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

After ./configure, I first get error saying "ncurses.h" not found. It's because there is no libncurses-dev installed on my os.

之后./configure,我首先收到错误消息,说找不到“ncurses.h”。这是因为我的操作系统上没有安装 libncurses-dev。

After installing libcurses-dev, I run makedirectly. Then got dozens of the "undefined reference to" error.

安装libcurses-dev后,我make直接运行。然后得到了几十个“未定义的引用”错误。

After rebuild from start again, these errors were gone.

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

./configure

./configure

make

make

回答by Andrey Suvorov

I faced with similar problem, when use Qt timer and ncurses: timeout macros ruin out Qt code =) Solution was "#undef timeout"after #include <ncurses.h>to prevent macros from working. And if in some cases You will need ncurses timeout, you can run wtimeout(stdscr,delay)directly.

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