Alex Rivera | Logout

How do you actually use a C library?

Asked 2009-11-17T13:58:11.717
23

I'm sure this question has been asked many times, but I can't figure this out. Bear with me.

So when you download a library, you get a bunch of .c and .h files, plus a lot of other stuff. Now say you want to write a program using this library.

I copy all the .h files into my project directory. It just doesn't compile.

Great, so then I get the library as a bunch of .dll's, and i copy the dlls into my project directory. Still doesn't compile.

How does this work?

What do you do, like right after creating the folder for your project? What parts of the library package do you copy/paste into the folder? How do you make it so that it can compile? Go through the steps with me please.

  1. Where to put the .h files?
  2. Where to put the .dll files?
  3. How to compile?

Thanks.

(the library I'm trying to get working is libpng, I'm in windows with MinGW, and i'm looking to compile from command-line like usual.)

(from what i gather, you put the .h files in directory A and the .dll files in directory B and you can use -l and -L compiler options to tell the compiler where to find them, is this correct?)

Edit
Report

2 Answers

14

Doing it under windows (supposing you user Visual Studio)

  • After unpacking add the library include directories to your projects' settings (Project -> Properties -> C/C++ -> Additional Include Directories)

  • Do the same thing for the Libraries Directory (Project -> Properties -> Linker -> Additional Library Directories)

  • Specify the name of the library in your Linker Input: Project -> Properties -> Linker -> Input -> Additional Dependencies

After this hopefully should compile.

I don't recommend adding the directories above to the Global settings in Visual Studio (Tools -> Options -> Project and Solutions) since it will create and environment where something compiles on your computer and does NOT compile on another one.

Now, the hard way, doing it for a Makefile based build system:

  • Unpack your stuff
  • Specify the include directory under the -I g++ flag
  • Specify the Library directory under the -L g++ flag
  • Specify the libraries to use like: -llibrary name (for example: -lxml2 for libxml2.so)
  • Specify the static libraries like: library name.a

at the end you should have a command which is ugly and looks like:

g++ -I/work/my_library/include -L/work/my_library/lib -lmylib my_static.a -o appname_exe MYFILE.CPP

(the line above is not really tested just a general idea)

I recommend go, grab a template makefile from somewhere and add in all your stuff.

answered 2009-11-17T14:09:31.580
2

It depends on the library. For examples, some libraries contain precompiled binaries (e.g. dlls) and others you need to compile them yourself. You'd better see the library's documentation.

Basically, to compile you should:

(1) have the library's include (.h) file location in the compiler's include path,

(2) have the library stubs (.lib) location in the linker's library path, and have the linker reference the relevant library file.

In order to run the program you need to have the shared libraries (dlls) where the loader can see them, for example in your system32 directory.

answered 2009-11-17T14:13:49.230

Your Answer