G++尝试链接ncurses,尽管已经加入了-lncurses标志,但仍然失败。

huangapple go评论83阅读模式
英文:

G++ Failing to link ncurses even though it has the -lncurses flag

问题

以下是您要翻译的内容:

我正在尝试使用g++编译一个ncurses文件。它给出了以下输出,这表明它没有正确链接。有趣的是,它在clang++下编译得很好。我已经从源代码安装了ncurses,我知道它能工作,因为它在clang下编译成功。请忽略文件开头的警告,我将在下一步处理它们。另外,我知道main.cpp的代码很糟糕,我也在努力修复它。

CC main.cpp ./main
main.cpp: 在函数‘int main(int, char**)’中:
main.cpp:34:34: 警告:格式不是字符串文字,也没有格式参数 [-Wformat-security]
   34 |  wprintw(window_one, text.c_str());
      |                                  ^
main.cpp:38:34: 警告:格式不是字符串文字,也没有格式参数 [-Wformat-security]
   38 |  wprintw(window_two, text.c_str());
      |                                  ^
/usr/bin/ld: /tmp/cct3HGZR.o: 在函数‘main’中:
/home/ubuntu/side-projects/slides/main.cpp:13: 对‘initscr’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:14: 对‘cbreak’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:16: 对‘stdscr’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:16: 对‘keypad’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:18: 对‘start_color’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:19: 对‘init_pair’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:21: 对‘stdscr’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:21: 对‘wbkgd’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:23: 对‘LINES’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:24: 对‘COLS’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:27: 对‘printw’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:28: 对‘refresh’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:33: 对‘wmove’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:34: 对‘wprintw’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:35: 对‘wrefresh’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:37: 对‘COLS’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:37: 对‘COLS’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:37: 对‘LINES’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:38: 对‘wprintw’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:39: 对‘wrefresh’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:41: 对‘stdscr’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:41: 对‘wgetch’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:45: 对‘endwin’未定义的引用
/usr/bin/ld: 在函数‘create_newwin(int, int, int, int)’中:
/home/ubuntu/side-projects/slides/main.cpp:52: 对‘newwin’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:53: 对‘box’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:55: 对‘wrefresh’未定义的引用
/usr/bin/ld: 在函数‘destroy_win(_win_st*)’中:
/home/ubuntu/side-projects/slides/main.cpp:60: 对‘wborder’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:61: 对‘wrefresh’未定义的引用
/usr/bin/ld: 在文件‘/home/ubuntu/side-projects/slides/main.cpp’中:62: 对‘delwin’未定义的引用
collect2: 错误:ld 返回 1 退出状态
make: *** [Makefile:9: all] 错误 1
这是我的makefile:

CC = g++
CFLAGS = -g -lncurses -Wall
SRC = main.cpp
OUT = ./main

.PHONY: all
all: ${SRC}
        @echo "CC " ${SRC} ${OUT}
        @${CC} ${CFLAGS} ${SRC} -o ${OUT}
        @echo "Done"

.PHONY: clean
clean:
        @echo "Cleaning Directory"
        @rm -rf ${OUT}
        @echo "Done"
这是我正在尝试编译的C++代码:

#include <ncurses.h>
#include <string>

WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);

int main(int argc, char *argv[]) {
        WINDOW *window_one;
        WINDOW *window_two;
        int startx, starty

<details>
<summary>英文:</summary>

I&#39;m trying to compile an ncurses file using g++. It gives this output which would suggest that it isn&#39;t linking correctly. Interestingly it compiles fine under clang++. I have ncurses installed from source and I know that it&#39;s working because it compiles under clang. Ignore the warnings at the start of the file, I&#39;m going to work on those next. Also I know that main.cpp is godawful, I&#39;m working on fixing that too.

CC main.cpp ./main
main.cpp: In function ‘int main(int, char**)’:
main.cpp:34:34: warning: format not a string literal and no format arguments [-Wformat-security]
34 | wprintw(window_one, text.c_str());
| ^
main.cpp:38:34: warning: format not a string literal and no format arguments [-Wformat-security]
38 | wprintw(window_two, text.c_str());
| ^
/usr/bin/ld: /tmp/cct3HGZR.o: in function main&#39;:
/home/ubuntu/side-projects/slides/main.cpp:13: undefined reference to
initscr'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:14: undefined reference to cbreak&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:16: undefined reference to
stdscr'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:16: undefined reference to keypad&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:18: undefined reference to
start_color'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:19: undefined reference to init_pair&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:21: undefined reference to
stdscr'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:21: undefined reference to wbkgd&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:23: undefined reference to
LINES'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:24: undefined reference to COLS&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:27: undefined reference to
printw'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:28: undefined reference to refresh&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:33: undefined reference to
wmove'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:34: undefined reference to wprintw&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:35: undefined reference to
wrefresh'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:37: undefined reference to COLS&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:37: undefined reference to
COLS'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:37: undefined reference to LINES&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:38: undefined reference to
wprintw'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:39: undefined reference to wrefresh&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:41: undefined reference to
stdscr'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:41: undefined reference to wgetch&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:45: undefined reference to
endwin'
/usr/bin/ld: /tmp/cct3HGZR.o: in function create_newwin(int, int, int, int)&#39;:
/home/ubuntu/side-projects/slides/main.cpp:52: undefined reference to
newwin'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:53: undefined reference to box&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:55: undefined reference to
wrefresh'
/usr/bin/ld: /tmp/cct3HGZR.o: in function destroy_win(_win_st*)&#39;:
/home/ubuntu/side-projects/slides/main.cpp:60: undefined reference to
wborder'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:61: undefined reference to wrefresh&#39;
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:62: undefined reference to
delwin'
collect2: error: ld returned 1 exit status
make: *** [Makefile:9: all] Error 1

Here&#39;s my makefile:
```makefile
CC = g++
CFLAGS = -g -lncurses -Wall
SRC = main.cpp
OUT = ./main

.PHONY: all
all: ${SRC}
        @echo &quot;CC &quot; ${SRC} ${OUT}
        @${CC} ${CFLAGS} ${SRC} -o ${OUT}
        @echo &quot;Done&quot;

.PHONY: clean
clean:
        @echo &quot;Cleaning Directory&quot;
        @rm -rf ${OUT}
        @echo &quot;Done&quot;

And here's the C++ that I'm trying to compile:

#include &lt;ncurses.h&gt;
#include &lt;string&gt;

WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);

int main(int argc, char *argv[]) {
        WINDOW *window_one;
        WINDOW *window_two;
        int startx, starty, width, height;
        int ch;

        initscr();
        cbreak();

        keypad(stdscr, TRUE);

        start_color();
        init_pair(1, COLOR_BLUE, COLOR_RED);

        bkgd(COLOR_PAIR(1));

        height = LINES;
        width = COLS / 2;
        starty = 0;
        startx = 0;
        printw(&quot;Press F1 to exit&quot;);
        refresh();
        window_one = create_newwin(height, width, starty, startx);

        std::string text = &quot;test&quot;;

        wmove(window_one, height / 2, text.size() / 2);
        wprintw(window_one, text.c_str());
        wrefresh(window_one);

        window_two = create_newwin(LINES, COLS / 2, 0, COLS / 2);
        wprintw(window_two, text.c_str());
        wrefresh(window_two);

        while((ch = getch()) != KEY_F(1)) {
                switch(ch) {}
        }

        endwin();
        return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx) {
        WINDOW *local_win;

        local_win = newwin(height, width, starty, startx);
        box(local_win, 0, 0);

        wrefresh(local_win);
        return local_win;
}

void destroy_win(WINDOW *local_win) {
        wborder(local_win, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;);
        wrefresh(local_win);
        delwin(local_win);
}

答案1

得分: 1

库应该通常放在g++命令行的最后,否则它们可能不会被搜索。这是因为链接器只在它已经查找过的文件中存在未定义的名称时才会搜索库 - 在您的情况下,在它遇到库时,它尚未查看任何文件。

您可能希望创建一个名为LIBS或类似的单独的makefile变量,然后将其添加到g++命令行的末尾。

英文:

Libraries should normally come last on the g++ command line, otherwise they may not be searched. This is because the linker only searches libraries if there are undefined names in the files it has already looked at - in your case, it hasn't looked at any at the point it comes across the library.

You probably want a separate makefile variable called LIBS or similar which you tack on the end of the g++ command line.

huangapple
  • 本文由 发表于 2023年2月10日 04:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定