首页 > 解决方案 > 如何在 Windows 上使用 gcc 链接 PDCurses?

问题描述

最近,我在装有 Windows 7 Home Premium 的 HP Pavilion 笔记本电脑上安装了 PDCurses 3.6(最新版本)。我还安装了 MinGW-w64(也是最新版本)。

好吧,我在这里开始学习如何使用curses模式,并下载了他们的示例代码(ncurses_programs.tar.gz);此时一切正常。解压缩程序后,我想利用 Makefile 来制作所有的 .exe。这是问题所在。

我运行 cmd.exe,移动到程序所在的文件夹,然后键入mingw32-make -f Makefile. 这是以下过程:

mingw32-make[1]: Entering directory 'C:/.../ncurses_programs/JustForFun'
gcc -o hanoi.o -c hanoi.c

/* throws some warnings */

gcc -o ../demo/exe/hanoi hanoi.o -lncurses
C:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64w64-mingw32/bin/ld.exe: cannot find -lncurses
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile:20: ../demo/exe/hanoi] Error 1
rm hanoi.o
mingw32-make[1]: Leaving directory 'C:/.../ncurses_programs/JustForFun'
mingw32-make: *** [Makefile:4: all] Error 2

好吧,您肯定在想“伙计,它正在尝试链接 ncurses 并且您拥有 pdcurses,因为您在 Windows 上”。是的,我知道。这就是为什么我编辑了 Makefile,LIBS=-lpdcurses而是键入LIBS=-lncurses,但它也没有找到它。

我知道在哪里pdcurses.a,所以我尝试通过控制台编译一个简单的程序(打印“Hello World!”),如下所示:

gcc -LC:\PDCurses\wincon -lpdcurses -o myprogram myprogram.c

我仍然得到:

C:/MinGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lpdcurses
collect2.exe: error: ld returned 1 exit status

我不知道我还能做什么...

预先感谢您!

标签: windowslinkermingwpdcurses

解决方案


-lname链接gcc被传递给链接器,ld。它指示链接器搜索文件libname.so(共享库)或libname.a(静态库),首先在指定的链接器搜索目录 ( -Ldir) 中,按照指定的顺序,然后是默认搜索目录,按照配置的顺序。当在其中一个搜索目录中找到这些文件中的任何一个时,链接器将停止搜索并将库输入到链接中。如果它在同一个目录中找到它们,那么默认情况下它会选择libname.so.

在 GCC 的 Windows 端口上,name.lib(静态库)和name.dll(动态库)也可能满足该-lname选项。

鉴于您安装了 PDCurses 静态库pdcurses.a-LC:\PDCurses\wincon链接:

gcc -LC:\PDCurses\wincon -lpdcurses -o myprogram myprogram.c

失败:

cannot find -lpdcurses

因为没有名为libpdcurses.alibpdcurses.sopdcurses.lib或 的文件pdcurses.dll存在于C:\PDCurses\wincon.

在该目录中重命名pdcurses.alibpdcurses.a解决此故障。如果您不想重命名它,则可以将链接选项替换-lpdcurses-l:pdcurses.a. 该选项-l:name指示链接器搜索名为精确的文件name

但是,您仍然无法将您的测试程序与以下任一链接:

gcc -LC:\PDCurses\wincon -lpdcurses -o myprogram myprogram.c

或者:

gcc -LC:\PDCurses\wincon -l:pdcurses.a -o myprogram myprogram.c

链接将失败,对pdcurses您在 中引用的任何符号(函数或变量)的未定义引用错误myprogram.c。(如果您实际上没有引用任何此类符号,myprogram.c那么它不会失败,而只是因为库是多余的)。

要更正此错误(这可能不会影响您的 makefile,我们看不到),请运行:

gcc -o myprogram myprogram.c -LC:\PDCurses\wincon -lpdcurses

或类似的,如果你选择-l:pdcurses.a

要理解这一点,请参阅您的链接在引用它们的目标文件之前使用库


推荐阅读