Unit 19: Third party libraries

  1. Introduction
  2. Third party libraries
  3. Compiler and linker options
  4. Linker inputs
  5. Using the library in the code
  6. The Linking Third Party Libraries assignment is optional

  7. Example code and additional resources

This week's stuff:

Linking third party libraries

Introduction

A library is a set of pre-written code - functions, classes, etc. - packaged in a way that can be used by many other projects. C++ comes with its own standard library, which includes things like strings, vectors, and even cin and cout - this functionality isn't directly a part of the core C++ language, so we include other libraries to use this functionality.

We can also find other third party libraries online to help us do stuff like advanced math, user interface display, playing media, connecting to databases, and more.

To use these libraries, we have to download them, and link them to our projects. We will step through this process here.

Third party libraries

When a library is downloaded and extracted to your computer filesystem there will usually be header files (for SFML, they use .hpp instead of .h) and library files, which may end in .a (archive), .lib, or .so extension.

The header files contain class and function declarations, which help you know what is included in the library, and the library files are source files compiled to a more "portable" library format (instead of a program that needs an int main()).

When we're setting up our IDE, we can point it to the header files so it can give us the "intellisense" / auto-fill function names and parameter list, and we link to the library files so our program can actually use that functionality.

Compiler and linker options

When you right-click on your project and select properties, there are a lot of options you can manage for your project. In particular, you have access to the C++ (compiler) options and Linker options.

In Visual Studio, these locations look like:

(Image from the SFML and Visual Studio page)

In Code::Blocks, we have:

(Image from the SFML and Code::Blocks (MinGW) page)

Linker inputs

We also have to update the Linker inputs, where we also set the linker to import specific libraries. Here, the list of library names are given.

In Visual Studio:

(Image from the SFML and Visual Studio page)

In Code::Blocks:

(Image from the SFML and Code::Blocks (MinGW) page)

Using the library in the code

Once everything is hooked up properly, you can then include header files from the library that specify what classes/functions are available and utilize them in your code. For example, the SFML library setup guide gives this example code:

#include <SFML/Graphics.hpp>

int main()
{
  sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
  sf::CircleShape shape(100.f);
  shape.setFillColor(sf::Color::Green);

  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
      window.close();
    }

    window.clear();
    window.draw(shape);
    window.display();
  }

  return 0;
}

Running the program:

(Image from the SFML page)

The Linking Third Party Libraries assignment is optional

Since everybody is on a different computer, it can be hard to get everyone's IDE configurations working with this. Especially on Mac, I don't know how to set up SFML with XCode or other tools because I don't have access to a Mac computer.

I can help set up the library in Linux and in Windows, and even in Windows sometimes I can't always figure out issues with setting up IDEs.

Therefore, this assignment is optional. You can try it out but if you can't get it working it won't affect your grade.

Example code and additional resources

Example code: (repository)

Archived videos: