From the course: Exploring C Libraries

Understanding libraries - C Tutorial

From the course: Exploring C Libraries

Start my 1-month free trial

Understanding libraries

- Writing a program in C involves three steps: creating the source code compiling that source code file into object code and then linking the object code with a library to create a program. Now modern compilers combine steps two and three but these are really discrete steps. Other steps, of course, involve testing the program, fixing bugs and the whole programming cycle. The Linker mixes object code with a C library. The library itself contains object code for functions that carry out specific tasks just like functions in your code. The program is built from these instructions designed to run on a target platform which could be your computer, gaming console, microcontroller, and so on. The default library is the standard C library. It contains instructions for common C language functions such as print-f, f-open, malloc and the rest. Some of the math functions may be contained in a second library called the math library, but often these are included in the standard C library. To extend a program's capabilities other libraries can be brought in. For example, a graphics library may include routines to access, manipulate and create image files. Library may provide specific routines to sift through formatted data, access the web, control terminal screens and so on. These libraries come with their own set of functions and hopefully some lucid documentation. Library files are archive files with a specific naming convention. The file name starts with "lib" followed by the library name. So "lib-c" is the name of the standard C library file. The extension is dot-a for a static library with dot-dylib for a dynamic library, though other extensions can be used. When you link in a library, however, only the name is used not the first "lib" part. Libraries dwell in a library folder or directory. In Unix, the birthplace of the C-language, the directory is user-lib. Custom libraries can be added in the user local lib directory. And by default, the Linker looks in these directories for libraries though specific paths can also be specified when you compile. For C compilers such as Microsoft's Visual Studio, XCode on the Mac, and MinGW, the library's location is local to the application. Specific libraries are added to your code in the linking process. Use your compiler to specify the libraries in their location if it's not in the default directories. The Linker combines your code with a code in the specific library, giving your program whatever super capabilities the library provides.

Contents