Importing third party libraries in a Juce project

Hi, I want to import a third party library into my JUCE project, specifically this one:

(I have noticed that Juce has built-in functions for Json, but I want to use this other one, mostly because I want to understand how to add packages in general to my Juce project, so just picked this one as practice, later I will want to add also the aws sdk for c++ , etc).

But I could not find information on how to do this in Juce - so I tried adding the file just like another Juce package, through the Projucer - “Add existing files”, selecting to compile it and then saving the project. After that, in my juce component cpp file, I tried this:

#include "ThirdParty/json.hpp"
using json = nlohmann::json;

While this works, is this the correct way to handle third party libraries in Juce? Because on their github, it says it should be added like this:

#include <nlohmann/json.hpp>

But that gives me this error:

fatal error: nlohmann/json.hpp: No such file or directory #include <nlohmann/json.hpp>

So, it feels like I am importing it wrong but somehow it works?
Thanks a lot in advance.

First of all you have to distinguish between three concepts:

  • Header Only Libraries: Those libraries are included in your compilation unit (aka .cpp file) and get compiled along with the compilation unit they are included in
  • Libraries to compile along with your project: This is the concept of the JUCE module format (just have a look in the Modules tab on the left of your Projucer window – there you see the Modules your project uses). This kind of libraries have one or more own compilation units which get compiled along with the other compilation units your project consists of. This is much like just adding a .cpp file directly to your project.
  • Precompiled libraries: Those libraries are either really complex to build or closed source. You only get a precompiled version of them and a header file describing their interface. You need to tell the linker that it should include the precompiled library in the linking step – therefore you’ll find the External Libraries To Link field in every Exporter for your Projucer project.

For both, header only libraries and precompiled libraries the compiler needs to know the directories where it should look for the header file. If this directory is relative to the current source file, you can chose the #include "foo/bar.h" syntax, this includes the header bar.h in the directory foo that is expected to be placed next to your current source file. To become independent of this relation you can pass one or more folders where the compiler should look for header files. If you click the little gear icon on the top left of your Projucer window, you’ll find the field Header Search Paths where you can put in directories where the compiler should look for headers. To include a file from the header search path you use the #include <foo/bar.h> syntax.

So TLDR: As the library you want to use is a header only library just add the directory of it to your project’s header search path :wink: Because this is the easiest way to include a library, many people like header only libraries.

2 Likes

words don’t do justice for how awesome you are.
thank you so much!!

Hi, what if library split between .cpp and .h? How to add it to project correctly? Should i use module concept?