ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
andykmv
Posts: 58
Joined: Feb 12, 2015 9:50

ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by andykmv »

Hello! i am having issues getting started using postgresql. this is my first time attempting to use a database library and i am attempting to use the postgres library.
I have a portable version of postgres installed (and subsequently also installed the current enterprise version) and i have 2 small databases setup and working on the portable version. these dbs work fine using pgadmin and with excel using the postgresql odbc driver.

i did a bit of reading and found the simple example program referred to here viewtopic.php?t=5712
when compiling, the error message i am getting is: R:\Data_PApps\FreeBASIC\bin\win64\ld.exe: cannot find -lpq
(i also tried compiling examples for sqlite2/3 and i get the same type of error)

but i just cant seem to find the relevant library files for win32 or win64, and/or to put the file in the right spot to get this example working, and i need some remedial help to get me over the line. (it also would be fair to say i havent used mingw tools for compiling much at all so i am unfamiliar with it).

Freebasic is installed either here: R:\FreeBASIC (32 bit) or here R:\data_Papps\freebasic (64bit)
MinGW is installed here: R:\mingw
Postgres portable is installed here: C:\Data_PApps\PostgreSQLPortable
i also installed postgres enterprise and read this https://www.postgresql.org/docs/current ... libpq.html

my Path variable: C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\PuTTY\;C:\Program Files (x86)\Skype\Phone\;c:\data_Papps\pstools;R:\FreeBASIC;R:\freebasic\bin;R:\masm32;R:\masm32\bin;R:\mingw;R:\mingw\bin;R:\mingw\lib;R:\radasm;R:\FreePascal\bin\i386-Win32;C:\Data_PApps\PostgreSQLPortable\App\PgSQL\bin;C:\Program Files\PostgreSQL\9.6\bin;C:\Program Files\PostgreSQL\9.6\lib postgresql enterprise installed here: C:\Program Files\PostgreSQL\9.6

i have done further browsing on the forum and have found a few references to postgres library use, and it appears with further reading that the library files may need to be copied into the mingw lib folders but my knowledge is insufficient to join the dots :(

can someone please advise which library files i need, where to get them, and where they need to be put ? many thanks in advance!

and apologies if this is posted in the wrong place, please move as necessary!
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by St_W »

The error message says that a library ("cannot find -l") named "pq" is missing.
You'll find that library in the "lib" folder inside your Postgres installation, named "libpq.lib".
Copy it to FreeBasic's library folder (lib/win## inside your FreeBasic installation) and make sure that the architecture matches (64-bit library for 64-bit FreeBasic).
I don't know whether that lib file is a static version of the library or just an import library, but you'll see. In the latter case the compiled application will not run and complain about a missing DLL file. Copy libpq.dll into the application folder in that case. Maybe additional dlls are required - the error messages will tell you. You can also use "depends" (http://www.dependencywalker.com/) to find out.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by D.J.Peters »

@Si_W looks like "libpq.lib" are a VisualStudio compiler library a GCC compatible lib would be "libpq.a" ?

Joshy
andykmv
Posts: 58
Joined: Feb 12, 2015 9:50

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by andykmv »

many thanks guys, that was just the nudge in the right direction that i needed.
- i located the libpq.dll & lippq.lib files and copied them to the right spot.
- then i did the .lib file conversion to .a file viewtopic.php?f=14&t=14036
(I had to grab the missing reimp program first)
- after that i compiled with no error!

i double checked the results just to be sure and yes the example worked as expected.
so, now i know how to fix the missing sqlite lib file too! great teamwork :)
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by St_W »

D.J.Peters wrote:@St_W looks like "libpq.lib" are a VisualStudio compiler library a GCC compatible lib would be "libpq.a" ?
Yes, *.lib libraries are typically built with (and for) MSVC. However, as far as I remember, often *.lib files can be used with FreeBasic without any modification - even renaming them to *.dll.a or *.a is not necessary. I don't know whether this works in general for all libraries or just for some special ones. If you know any details about the object/library files generated by MSVC and GCC I'd be interested to learn more about them. e.g. what are the commonalities and differences? how to convert or transform them in either direction (MSVC <--> GCC)? or technical details/internals about libraries. Basically all that I know is that they are collections of object files.
And btw, is there any graphical application that allows to analyze static libraries?

@andykmv: great to hear that you got it working. Just out of interest: did you try to use the "libpq.lib" directly without transforming it with the "reimp" tool or just by renaming it to .a or .dll.a and what were the results?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by D.J.Peters »

@St_W I would self never rename a VS library only.
1) I don't know is a downloaded *.lib a import lib *.dll.a only a list of exported name entries to the DLL
or is it static lib *.a a collection of precompiled object code *.o

2) Often VS *.libs are a mix of C and CPP code or COM objects
You can use simple cpp via FreeBASIC also but it must be a GNU GCC/g++ lib.
In case of GNU C++ the hidden THIS pointer are on stack but VisualStudio compilers put the class pointer THIS in register EAX/RAX.
( I hacked THIS pointer stuff in the first Visual Studio Steinberg ASIO SDK for FreeBASIC)

3) I would "google" for "libPQ mingw" in this case you get the right lib and will known is it *.dll.a or *.a and is it 32 or 64-bit
if no mingw lib exist I would read about the type of the VS *.lib file (static or dynamic import)
in case of an import lib you can create from PQ.dll a *.def file and use dlltool and create libPQ.dll.a also.
In case of a static lib reimp are the right tool (so far I know).

Or simple rename it as you suggested and pray and hope you don't kill your data base :lol:

Joshy
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by marcov »

If you want to know details about a filetype, a good start is running the *nix/mingw command "file" on them.

My bet is that the msvc libs show "omf" as filetype, while the mingw derived (*.a) files are (pe)coff.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by D.J.Peters »

How to dlltool 32/64-bit
XXX is the PATH to your FreeBASIC folder

32-bit: "XXX\bin\win32\dlltool.exe" -d pq.def -l XXX\lib\win32\libpq.dll.a

64-bit: "XXX\bin\win64\dlltool.exe" -m i386:x86-64 --as-flags --64 -d pq.def -l XXX\lib\win64\libpq.dll.a

Joshy
andykmv
Posts: 58
Joined: Feb 12, 2015 9:50

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by andykmv »

marcov wrote:If you want to know details about a filetype, a good start is running the *nix/mingw command "file" on them.

My bet is that the msvc libs show "omf" as filetype, while the mingw derived (*.a) files are (pe)coff.
thanks for that! as mingw did not have a file.exe equivalent I tried the TrID program (written in FreeBasic apparently!) and it showed them as COFF. good tip. apparently cygwin does which can be copied out of the cygwin distro and used but it did not try that. yet.
anyway i used it to work out what target arch the AS and DLLTOOL etc i had were compiled for
Last edited by andykmv on Mar 16, 2018 6:40, edited 1 time in total.
andykmv
Posts: 58
Joined: Feb 12, 2015 9:50

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by andykmv »

D.J.Peters wrote:How to dlltool 32/64-bit
XXX is the PATH to your FreeBASIC folder

32-bit: "XXX\bin\win32\dlltool.exe" -d pq.def -l XXX\lib\win32\libpq.dll.a

64-bit: "XXX\bin\win64\dlltool.exe" -m i386:x86-64 --as-flags --64 -d pq.def -l XXX\lib\win64\libpq.dll.a

Joshy
@D.J.Peters, many thanks for this tip. i was having similar issues with the sqlite3 lib and after going down a few erroneous rabbit holes i noticed your message. used this technique on the sqlite3.def file from the sqlite site (3220000) and i got a working libsqlite3.dll.a - whoo hoo!

then i got an error - Error "(null).dll was not found" which sent me down a few more rabbit holes until i saw this post posting.php?mode=quote&f=2&p=241822 about "LIBRARY "sqlite3.dll" section was missing from sqlite3.def file"

I added the missing section, reapplied your technique and voila! finally i can query an sqlite3 database. problem solved @dkl!
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: ld.exe: cannot find -lpq (having trouble getting started using postgresql library)

Post by D.J.Peters »

The dlltool can create the *.def file from *.dll for you also :-)
see here: http://www.mingw.org/wiki/createimportlibraries

Joshy
Post Reply