My application needs libpq, paho-mqtt libraries and mqtt broker. I have installed and build all of them in a separate folder with my application.While I was trying to build my application using "make" command, I faced with /usr/bin/ld: cannot find -lpq error. How can I link my application directory to libpq library ?
1 Answers
I encountered this error, since i also compiled postgresql and the postgresql library called libpq. So when you write a C program and compile it with -lpq option it probably won't work, unless you updated the path GCC looks for libraries.
The solution that i came with isn't the best but i sym-linked the files in the directory where postgresql libraries was installed, usually "usr/local/pgsql/lib" to "usr/include" using
ln -s /usr/local/pgsql/lib/* /usr/lib/
*You might need root privileges for above command to execute, try with sudo if it fails. This is just one of the ways, a better way would be to update the linker path for GCC to include the postgresql lib path using the variable "LIBRARY_PATH". You could also use the option -L within GCC and specify the directory you want GCC to look for to link libraries.
This will only work if you installed the entire postgresql package, or you have compiled postgresql from source by using "make world" and "make install-world"which makes libraries plus docs. if you haven't done so then you could install package called postgresql-libs and this method would depend on the OS. For arch there is a package called postgresql-libs which can be installed through Pacman. For Ubuntu i think it's called libpq-dev. Installing it again through package manager, will be unnecessary if you already have compiled the whole package, since the libraries can be made from the source, also you should be aware the libraries can cause issues if there's version mismatch between the libraries you install from package manager and the database binaries which you compiled from source, so take a look before installing. However this may not always happen.
sudo apt-get install -y libpq-dev
– Philippe