【问题标题】:How can I link PDCurses using gcc on Windows?如何在 Windows 上使用 gcc 链接 PDCurses?
【发布时间】:2019-02-13 07:16:57
【问题描述】:

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

好吧,我开始学习如何使用curses模式here,并下载了他们的示例代码(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

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

先谢谢你!

【问题讨论】:

    标签: windows linker mingw pdcurses


    【解决方案1】:

    gcc-lname 链接被传递到链接器 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.libpdcurses.dll 存在于 C:\PDCurses\wincon 中。

    在该目录中将 pdcurses.a 重命名为 libpdcurses.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,则类似。

    要了解这一点,请参阅Your linkage consumes libraries before the object files that refer to them

    【讨论】:

    • 你也可以gcc -o myprogram myprogram.c C:\PDcurses\wincon\pdcurses.a .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2011-10-22
    • 2018-08-02
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多