[英]Single file program with PDCurses fails to compile with "undefined reference to"
I have some problems compiling my test program using PDCurses library.我在使用 PDCurses 库编译我的测试程序时遇到了一些问题。 I have compiled PDCurses 3.4 with MinGW compiler 8.2.0.
我用 MinGW 编译器 8.2.0 编译了 PDCurses 3.4。 In output I have 2 files panel.a and pdcurses.a.
在输出中我有 2 个文件 panel.a 和 pdcurses.a。 I copied them to C:\MinGW\lib plus the header file curses.h in C:\MinGW\include.
我把它们复制到C:\MinGW\lib加上C:\MinGW\include中的头文件curses.h。 But when I try to compile test program I have errors.
但是当我尝试编译测试程序时出现错误。 It seems I have done everything correctly according to instructions and how-to`s.
看来我已经按照说明和操作方法正确地完成了所有操作。
Some testing C code:一些测试 C 代码:
#include <C:\MinGW\include\curses.h>
int main () {
initscr();
mvprintw( 5, 5, "Hello, World!" );
getch();
endwin();
return 0;
}
ERRORS:错误:
c:/Users/username/Dropbox/rakshasa/main.c:4: undefined reference to `initscr'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/Users/username/Dropbox/rakshasa/main.c:5: undefined reference to `mvprintw'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/Users/username/Dropbox/rakshasa/main.c:6: undefined reference to `stdscr'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/Users/username/Dropbox/rakshasa/main.c:6: undefined reference to `wgetch'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/Users/username/Dropbox/rakshasa/main.c:7: undefined reference to `endwin'
I have no idea why it is not compiling properly.我不知道为什么它没有正确编译。 It looks like compiler cant find libraries in linking stage but I do not know why.
看起来编译器在链接阶段找不到库,但我不知道为什么。
I was try to compile like gcc -g main.c -o main.exe我试着像 gcc -g main.c -o main.exe 那样编译
Thanks in advance.提前致谢。
Never use absolute paths in #include
directives.永远不要在
#include
指令中使用绝对路径。 Instead of #include <C:\MinGW\include\curses.h>
you should put #include <curses.h>
and compile with -IC:\MinGW\include
flag if needed.而不是
#include <C:\MinGW\include\curses.h>
您应该放置#include <curses.h>
并在需要时使用-IC:\MinGW\include
标志进行编译。
Next you need to link with the relevant library using -lpdcurses
.接下来,您需要使用
-lpdcurses
链接到相关库。 If needed you can add the location of the library using -LC:\MinGW\lib
.如果需要,您可以使用
-LC:\MinGW\lib
添加库的位置。
So if we split compilation and linking steps you need the following commands:因此,如果我们拆分编译和链接步骤,您需要以下命令:
gcc -g main.c -c -o main.o -IC:\MinGW\include
gcc main.o -o main.exe -lpdcurses -LC:\MinGW\lib
though I think you can leave out the -I
and -L
flags as they point to the location of your compiler's include
and lib
folders.尽管我认为您可以省略
-I
和-L
标志,因为它们指向编译器的include
和lib
文件夹的位置。
Finally: MinGW is a bit outdated and so is GCC 8.2.0.最后:MinGW 有点过时,GCC 8.2.0 也是如此。 I recommend you switch to MinGW-w64 (which supports both 32-bit and 64-bit Windows).
我建议您切换到 MinGW-w64(它同时支持 32 位和 64 位 Windows)。 To get a more recent MinGW-w64 GCC build, you can try the standalone builds from https://winlibs.com/ .
要获得更新的 MinGW-w64 GCC 构建,您可以尝试来自https://winlibs.com/的独立构建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.