英文:
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'm trying to compile an ncurses file using g++. It gives this output which would suggest that it isn't linking correctly. Interestingly it compiles fine under clang++. I have ncurses installed from source and I know that it's working because it compiles under clang. Ignore the warnings at the start of the file, I'm going to work on those next. Also I know that main.cpp is godawful, I'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':
initscr'
/home/ubuntu/side-projects/slides/main.cpp:13: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:14: undefined reference to cbreak'
stdscr'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:16: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:16: undefined reference to keypad'
start_color'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:18: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:19: undefined reference to init_pair'
stdscr'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:21: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:21: undefined reference to wbkgd'
LINES'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:23: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:24: undefined reference to COLS'
printw'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:27: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:28: undefined reference to refresh'
wmove'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:33: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:34: undefined reference to wprintw'
wrefresh'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:35: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:37: undefined reference to COLS'
COLS'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:37: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:37: undefined reference to LINES'
wprintw'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:38: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:39: undefined reference to wrefresh'
stdscr'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:41: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:41: undefined reference to wgetch'
endwin'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:45: undefined reference to
/usr/bin/ld: /tmp/cct3HGZR.o: in function create_newwin(int, int, int, int)':
newwin'
/home/ubuntu/side-projects/slides/main.cpp:52: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:53: undefined reference to box'
wrefresh'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:55: undefined reference to
/usr/bin/ld: /tmp/cct3HGZR.o: in function destroy_win(_win_st*)':
wborder'
/home/ubuntu/side-projects/slides/main.cpp:60: undefined reference to
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:61: undefined reference to wrefresh'
delwin'
/usr/bin/ld: /home/ubuntu/side-projects/slides/main.cpp:62: undefined reference to
collect2: error: ld returned 1 exit status
make: *** [Makefile:9: all] Error 1
Here's my makefile:
```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"
And here's the C++ that I'm trying to compile:
#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, 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("Press F1 to exit");
refresh();
window_one = create_newwin(height, width, starty, startx);
std::string text = "test";
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, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论