PDA

View Full Version : How do I install a library/tell gcc where it is?



Yes
July 30th, 2008, 02:50 AM
I downloaded a library I need for a program I'm working on, but I'm not sure how I make gcc know where it is. I know you add '-l[libname]' to the arguments, but if it's not "installed", what do I do? Do I need to install it? If I do, would that be distro specific, because I'm not using Ubuntu.

Thanks!

LaRoza
July 30th, 2008, 03:27 AM
I downloaded a library I need for a program I'm working on, but I'm not sure how I make gcc know where it is. I know you add '-l[libname]' to the arguments, but if it's not "installed", what do I do? Do I need to install it? If I do, would that be distro specific, because I'm not using Ubuntu.

Thanks!

First tell what library it is (and in what state. A tarball? An RPM? etc) and what distro you are using.

Yes
July 30th, 2008, 03:54 AM
It's libmpdclient, I've got the tarball, and I'm using Arch.

dwhitney67
July 30th, 2008, 04:04 AM
$ gcc MyProg.c -L<pathToLibrary> -l<libname> -o MyExec
The <pathToLibrary> is the full path where the library file exists. If the file is in /usr/lib or /lib, then you do not have to specify the -L option. The last sentence is not meant to imply that you should place your library file in those restricted areas, but merely to enlighten you to where gcc normally looks to find system libraries.

The <libname> is the short-hand version of the library name. So if you want to link in libsomething.so, then you would specify "-lsomething". Note that the leading "lib" and the trailing ".so" (or ".a") are not required.

Yes
July 30th, 2008, 05:06 AM
I don't have anything called ".so" or ".a", just libmpdclient.c and libmpdclient.h. Is that a problem? I tried putting them in the same directory as the source file and running ' gcc client.c -L./ -llibmpdclient -o client', but I got the error '/usr/bin/ld: cannot find -llibmpdclient'.

LaRoza
July 30th, 2008, 09:32 AM
I don't have anything called ".so" or ".a", just libmpdclient.c and libmpdclient.h. Is that a problem? I tried putting them in the same directory as the source file and running ' gcc client.c -L./ -llibmpdclient -o client', but I got the error '/usr/bin/ld: cannot find -llibmpdclient'.

In that case, you can either create a library, or just compile it yourself.

Say your program is in a file called "Main.c":



gcc Main.c libmpdclient.c -o <program-name>


Include the header in Main.c.

nvteighen
July 30th, 2008, 09:53 AM
If interested on how to build libraries (static or shared), look here: http://crasseux.com/books/ctutorial/Building-a-library.html

Yes
July 30th, 2008, 01:44 PM
Great, thanks.