I’ve not installed Code::Blocks on a Linux computer, but for a moment I’ll pretend that such an installation has the same issue I described in last week’s Lesson: You must manually link in the C language math library to create any program that uses a
More importantly, to expand the C language’s capabilities, you frequently need to mix in another library. The standard library is linked by default, but pretend that you’re using a library that creates graphics, sound, or provides other specific functions beyond the normal scope of C. You must specify that such a library is linked into the final program.
The library also comes with a header file that provides function definitions, constants, structures, and other whatnot. The header file, however, is not the same as the library. The library is what contains the code that makes the functions work.
On the command line, you use the
-lx switch to link in library x. In Code::Blocks, things work differently.
As an example, suppose you need to link in the math library in a Code::Blocks project. You don’t have to on a Mac or PC, but these are the steps you would use. And you can follow the steps to link in any library, not just the math library. Some huge projects require multiple libraries.
After creating the Code::Blocks project, follow these steps to add a library:
1. Choose Project, Build Options.
The Project Build Options dialog box appears.
2. Click the Linker Settings tab.
3. Click the Add button.
4. In Add Library dialog box, click the Ellipsis (…) button to browse to the location of the library file.
Use the Choose Library to Link dialog box to find the library’s location on your computer’s mass storage system:
- For the MinGW compiler, the location for standard libraries is
MinGW\lib, with the full path:
C:\Program Files (x86)\CodeBlocks\MingGW\lib\for a standard Code::Blocks installation. If the library was saved elsewhere, you must browse to that specific directory (“folder”).
- With XCode on the Mac, the library locations differ depending on the version of XCode. In the current version, C language libraries are kept in
/Applications/Xcode.app/Contents/Developer/ ... Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib. Ugh.
- In Linux, and other Unices, the traditional location for C libraries is
If you were linking in the math library — which isn’t necessary on a PC, but doing so doesn’t screw up anything — you would locate the file named
libm.a, which is the way MinGW names its libraries. On the Mac, the math library is named
5. Select the library file and click the Open button.
6. Click No to avoid using a relative path.
I’m a fan of direct or absolute pathnames.
7. Click OK to add the library to the list.
8. Click OK to close the Project Build Options dialog box.
One more thing:
9. Choose File, Save Project.
The library is linked after the code is compiled. That process allows the library’s functions to access their magic. Otherwise you’d get those ugly linker errors.
In next week’s Lesson, I cover how to locate and install bonus C language library files.