【C&C++】in function `main‘: main.cpp:(.text+0x9): undefined reference to `initscr‘

【C&C++】in function main‘: main.cpp:(.text+0x9): undefined reference to initscr‘

问题描述

/usr/bin/ld: /tmp/ccXkKcdN.o: warning: relocation against `stdscr' in read-only section `.text'
/usr/bin/ld: /tmp/ccXkKcdN.o: in function `main':
main.cpp:(.text+0x9): undefined reference to `initscr'
/usr/bin/ld: main.cpp:(.text+0x18): undefined reference to `move'
/usr/bin/ld: main.cpp:(.text+0x1f): undefined reference to `stdscr'
/usr/bin/ld: main.cpp:(.text+0x31): undefined reference to `wattr_on'
/usr/bin/ld: main.cpp:(.text+0x38): undefined reference to `stdscr'
/usr/bin/ld: main.cpp:(.text+0x4f): undefined reference to `waddnstr'
/usr/bin/ld: main.cpp:(.text+0x54): undefined reference to `refresh'
/usr/bin/ld: main.cpp:(.text+0x63): undefined reference to `endwin'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

原因分析

在链接阶段未能找到 ncurses 库中的函数定义。虽然在代码中包含了 curses.h 头文件,但在编译时没有链接 ncurses 库,因此链接器无法解析 initscrmovestdscr 等函数的引用,从而导致错误。

解决方案

方法1

如果使用 g++,在编译参数中加入 -lncurses 以链接 ncurses 库:

g++ main.cpp -o main -lncurses

方法2

如果使用 Visual Studio Code,在 tasks.jsonargs 中加入 "-lncurses" 以链接 ncurses 库:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lncurses"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: /usr/bin/g++"
        }
    ]
}
/tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'initscr' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'cbreak' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'noecho' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'keypad' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'curs_set' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'wtimeout' /tmp/ccURoooa.o:test.cpp:function main: error: undefined reference to 'endwin' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'clear' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'stdscr' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccURoooa.o:test.cpp:function Game::draw(): error: undefined reference to 'refresh' /tmp/ccURoooa.o:test.cpp:function Game::input(): error: undefined reference to 'wgetch' collect2: error: ld returned 1 exit status run.sh: line 2: ./main: No such file or directory
04-04
/tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'initscr' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'cbreak' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'noecho' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'stdscr' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'keypad' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'curs_set' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'stdscr' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'wtimeout' /tmp/ccGUDfOK.o:test.cpp:function main: error: undefined reference to 'endwin' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'clear' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'stdscr' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'stdscr' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'wmove' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'waddch' /tmp/ccGUDfOK.o:test.cpp:function Game::draw(): error: undefined reference to 'refresh' /tmp/ccGUDfOK.o:test.cpp:function Game::input(): error: undefined reference to 'wgetch' collect2: error: ld returned 1 exit status run.sh: line 2: ./main: No such file or directory
最新发布
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值