Setting up and using third party libraries

Table of Contents


1. Quick checklists

1.1. Setting up a library in Visual Studio

  1. Download library from website.
  2. Extract library to your hard-drive.
  3. Create a new Empty project in Visual Studio.
  4. Create a new source file (main.cpp) in Visual Studio.
  5. Right-click on your project and go to Properties.
  6. Make sure your Configuration is set to Release and your Platform is set to x64.
  7. Under C/C++ > General, add SFML's include path to the Additional Include Directories property.
  8. Under Linker > General, add SFML's lib path to the Additional Library Directories property.
  9. Under Linker > Input, click the "v" dropdown on Additional Dependencies and click <Edit…>. Add your related .lib files (e.g., sfml-graphics.lib, sfml-window.lib, sfml-system.lib).
  10. Click OK.
  11. Move the .dll files (sfml-graphics-2.dll, sfml-window-2.dll, sfml-system-2.dll) from the SFML/bin folder to your project directory.
  12. Done with configuration.

1.2. Setting up a library in Code::Blocks (Windows)

  1. Download library from website.
  2. Extract library to your hard-drive.
  3. Create a new Empty project in Code::Blocks.
  4. Create a new empty file (main.cpp) in Code::Blocks.
  5. Right-click on your project and go to Build options…
  6. Make sure you've selected Release instead of Debug on the left-hand side.
  7. Under Search directories > Compiler, add SFML's include path.
  8. Under Search directories > Linker, add SFML's lib path.
  9. Under Linker settings and Link libraries:, add the libraries: sfml-system, sfml-graphics, sfml-window.
  10. Click OK.
  11. Move the .dll files (sfml-graphics-2.dll, sfml-window-2.dll, sfml-system-2.dll) from the SFML/bin folder to your project directory.
  12. Done with configuration.

2. Setting up third-party libraries in your IDE

2.1. Downloading and extracting the library

For these instructions we will be working with the SFML (Simple Fast Media Library) library, but setting up most libraries will work about the same.

2.1.1. Getting a library in Windows

Downloading: Go to the library webpage (https://www.sfml-dev.org/download.php) and select "SFML", latest stable version (as of writing this is 2.6.1).

From the downloads page, there are various options for a Windows download.

third_party_libraries_sfmlpage.png

Figure 1: An image of the SFML downloads page, showing different versions that can be downloaded for Windows.

If you're working with Visual Studio, then download the latest version, even if the year doesn't match. You'll probably want the 64-bit verison, since most computers these days are 64-bit.

If you're working with Code::Blocks, you will probably want the 64-bit SEH option ("GCC 13.1.0 MinGW (SEH) - 64-bit"), but you can double-check which version you need by going to your CodeBlocks install directory (such as C:\Program Files\CodeBlocks\MinGW\bin\) and looking for a file called "libgcc"… based on this version it will tell you which version of SFML you need to download:

third_party_libraries_sfmlpage_libgcc.png

In Windows I like to have a "LIBRARIES" folder on my root directory, such as "C:\LIBRARIES\". So, all extracted, my default path for SFML would be:

C:\LIBRARIES\SFML-2.6.1

2.1.2. Getting a library in Linux

You can download many libraries from your distro's package manager, such as synaptic for Debian-based systems. Do a search for "libsfml" and download the libsfml-dev package. (Most libraries are "libXYZ-dev" as well.)

third_party_libraries_synaptic.png

The library will be automatically installed on your machine, with the relevant paths for configuration being.

2.1.3. Keeping track of important paths

You'll need to know where your library files are being kept while we're configuring the project, so make sure to take note of the paths. Note that if you're in Windows, you might have unzipped your library to a different directory than me, and will need to replace "C:\LIBRARIES\" with wherever you put your SFML folder.

Type Windows Path Linux Path Description
.dll location C:\LIBRARIES\SFML-2.6.1\bin\ - When distributing your .exe for Windows, you'll want to include the relevant .dll files, held here.
Library files C:\LIBRARIES\SFML-2.6.1\lib\ /usr/lib/x86_64-linux-gnu/ SFML's C++ code is compiled into libraries, available here.
.hpp files C:\LIBRARIES\SFML-2.6.1\include\ /usr/include/ This shows the IDE what function and class declarations are available.

2.2. Setting up the project

You can also refer to the SFML Tutorials page on how to set up Visual Studio, Code::Blocks, and other platforms.

The following write-ups are step-by-step guides with screenshots to show you how to configure Visual Studio and Code::Blocks for SFML…


2.2.1. Visual Studio

1. Create a project: Create an Empty C++ Project in Visual Studio.

third_party_libraries_vs1_2023.png

2. Set your Project name and Location, then click Create.

third_party_libraries_vs2_2023.png

3. Create a source file: Create a new .cpp file for your project by going to your Solution Explorer, right-clicking on the project, going to Add, then selecting New Item….

third_party_libraries_vs3_2023.png

4. Name your new file "main.cpp" and click Add.

third_party_libraries_vs4_2023.png

In your main.cpp, it can be useful to add some starter code to make sure your project will build properly with the library linked within. Here is a very stripped-down SFML program:

#include <SFML/Graphics.hpp>

int main()
{
  sf::RenderWindow window(sf::VideoMode(200, 200), "Window");
  return 0;
}

At first this will generate build errors, but once we have the library set up properly, it should compile.

5. Go to the properties: Right-click on your project in the Solution Explorer and select Properties.

third_party_libraries_vs5_2023.png

Make sure at the top of this screen, to set the Configuration to Release and Platform to x64.

third_party_libraries_vs6_2023.png

6. Set up the include directory: On the left-hand side, open the C/C++ category and the General sub-category. In the Additional Include Directories property, paste the path to your SFML directory's include folder, such as:

C:\LIBRARIES\SFML-2.6.1\include

third_party_libraries_vs7_2023.png

7. Set up the lib directory: Now under the Linker category and the General sub-category, add the path for the SFML directory's lib folder to the Additional Library Directories property, such as:

C:\LIBRARIES\SFML-2.6.1\lib

third_party_libraries_vs8_2023.png

8. Set up dependencies: Under the Linker category and the Input sub-category, click the "v" button next to Additional Dependencies. Click the <Edit…> button.

third_party_libraries_vs9_2023.png

In the pop-up box, add the following in the top text box:

sfml-graphics.lib
sfml-window.lib
sfml-system.lib

third_party_libraries_vs10_2023.png

Click OK when done with setting up these properties.

9. Build the program: In the dropdown near the play button, select Release as our build type, and make sure x64 is selected as our platform.

third_party_libraries_vs11_2023.png

Now when you build there shouldn't be any errors:

third_party_libraries_vs12_2023.png

10. Adding the .dll files to the project:

If we try to run the program right now, it will complain about missing dll files:

third_party_libraries_vs13_2023.png

We need to copy the dll files from the SFML library to our project now.

Right-click on the project and select Open Folder in File Explorer.

third_party_libraries_vs14_2023.png

In the SFML directory's bin folder (e.g., C:\LIBRARIES\SFML-2.6.1\bin\), copy the relevant library files (sfml-graphics-2.dll, sfml-system-2.dll, sfml-window-2.dll).

third_party_libraries_vs15_2023.png

Paste these files into your project directory:

third_party_libraries_vs16_2023.png

Your program should now start, open, and close without any error messages. Currently the program doesn't really do anything because there's no game loop within. Reference the SFML setup page (https://www.sfml-dev.org/tutorials/2.6/start-vc.php) for some test code you can use to verify that everything works properly.

My Visual Studio setup video: https://www.youtube.com/watch?v=4BoB5H2Kvzo


2.2.2. Code::Blocks (Windows/Linux)

1. Create a project: Create a new empty project in Code::Blocks.

third_party_libraries_cb1_2023.png

2. Set the Project title and Folder to create project in, then click Next >. Use the default options, then click Finish to create the project.

third_party_libraries_cb2_2023.png

3. Create a source file: Go to File > New > Empty file and create a new file named "main.cpp".

third_party_libraries_cb3_2023.png

In your main.cpp, it can be useful to add some starter code to make sure your project will build properly with the library linked within. Here is a very stripped-down SFML program:

#include <SFML/Graphics.hpp>

int main()
{
  sf::RenderWindow window(sf::VideoMode(200, 200), "Window");
  return 0;
}

4. Go to the project options: Right-click on your project in the Projects view and select Build options….

third_party_libraries_cb4_2023.png

On the left-hand side, make sure to select the Release configuration, not the Debug.

5. Link to the include directory: Under the Search directories tab, select the Compiler sub-tab. From here, click the Add button and paste in the path to your SFML directory's include folder, such as:

C:\LIBRARIES\SFML-2.6.1\include

third_party_libraries_cb6_2023.png

6. Link to the lib directory: Under the Search directories tab, select the Linker sub-tab. From here, click the Add button and paste in the path to your SFML directory's lib folder, such as:

C:\LIBRARIES\SFML-2.6.1\lib

third_party_libraries_cb7_2023.png

7. Set up linked libraries: Under the Linker settings tab…

For WINDOWS, under the Link libraries: section, click the Add button. You'll do this three times, adding the three libraries: sfml-graphics, sfml-window, and sfml-system.

third_party_libraries_cb8_2023.png

For LINUX, under the Other linker options: section add these flags: -lsfml-graphics, -lsfml-window, -lsfml-system):

third_party_libraries_cb8linux_2023.png

Click OK once finished.

8. Build the program: In the dropdown near the build buttons, make sure to change it from building the "Debug" version to building the "Release" version.

third_party_libraries_cb9_2023.png

Now when you build there shouldn't be any errors:

third_party_libraries_cb10_2023.png

9. (WINDOWS) Adding the .dll files to the project:

If we try to run the program right now, it will complain about missing dll files:

third_party_libraries_cb11_2023.png

We need to copy the dll files from the SFML library to our project now.

In your file explorer, navigate one window to your SFML directory and one to your project directory.

In SFML's bin directory, highlight the dll files and copy them: sfml-graphics-2.dll, sfml-system-2.dll, and sfml-window-2.dll.

third_party_libraries_cb12_2023.png

Paste these files into your project directory:

third_party_libraries_cb13_2023.png

Your program should now start, open, and close without any error messages. Currently the program doesn't really do anything because there's no game loop within. Reference the SFML setup page (https://www.sfml-dev.org/tutorials/2.6/start-cb.php) for some test code you can use to verify that everything works properly.


If you use the example code from the SFML webpage guide, after you build and run you should see a green circle rendered to our window:

third_party_libraries_running_2023.png


Author: Rachel Wil Sha Singh

Created: 2023-11-14 Tue 16:32

Validate