windowslinkermingwpdcurses

How can I link PDCurses using gcc on Windows?


Recently I installed PDCurses 3.6 (latest version) on my HP Pavilion laptop with Windows 7 Home Premium. Also I have installed MinGW-w64 (latest version too).

Well, I started to learn how to use the curses mode here, and downloaded their example codes (ncurses_programs.tar.gz); at this point all was OK. After unzipping the programs, I wanted to make use of the Makefile to have all the .exes already made. Here is the problem.

I ran cmd.exe, moved to the folder where the programs are, and then typed mingw32-make -f Makefile. This is the following process:

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

Well, you surely are thinking "man, it's trying to link ncurses and you have pdcurses because you are on Windows". Yes, I know it. That's why I edited the Makefile, typing LIBS=-lpdcurses instead LIBS=-lncurses, but it doesn't find it neither.

I know where pdcurses.a is, so I tried to compile a simple program (print 'Hello World!') by console like this:

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

I still getting:

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

I don't know what else can I do...

Thank you beforehand!


Solution

  • The -lname linkage of gcc is passed through to the linker, ld. It instructs the linker to search for either of the files libname.so (a shared library) or libname.a (a static library), first in the specified linker search directories (-Ldir), in their specified order, and then its default search directories, in their configured order. When either of those files is found in one of the search directories the linker stops searching and inputs the library to the linkage. If it finds both of them in the same directory then by default it chooses libname.so.

    On Windows ports of GCC, name.lib (static library) and name.dll (dynamic library) will probably also satisfy the -lname option.

    Given that you have the PDCurses static library pdcurses.a installed in -LC:\PDCurses\wincon, the linkage:

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

    fails with:

    cannot find -lpdcurses
    

    because no file called libpdcurses.a, libpdcurses.so, pdcurses.lib, or pdcurses.dll exists in C:\PDCurses\wincon.

    Renaming pdcurses.a to libpdcurses.a in that directory will remedy this failure. If you don't want to rename it, then you can replace the linkage option -lpdcurses with -l:pdcurses.a. The option -l:name directs the the linker to search for a file called precisely name.

    However, you will still fail link your test program with either:

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

    or:

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

    The linkage will fail with undefined reference errors to any pdcurses symbols (functions or variables) that you refer to in myprogram.c. (If you don't actually refer to any such symbols in myprogram.c then it will not fail, but only because the library is redundant).

    To correct this error (which may not effect your makefile, which we can't see), instead run:

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

    or similary if you choose -l:pdcurses.a.

    To understand this, see Your linkage consumes libraries before the object files that refer to them