# Using third-party libraries

**URL:** https://discourse.cmake.org/t/using-third-party-libraries/8852
**Category:** Code
**Tags:** tool:cmake
**Created:** [August 31, 2023, 10:29pm UTC](https://discourse.cmake.org/t/using-third-party-libraries/8852 "2023-08-31T22:29:46Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)
#### Post date: [August 31, 2023, 10:29pm UTC](https://discourse.cmake.org/t/using-third-party-libraries/8852/1 "2023-08-31T22:29:47Z")

</div>

I’d like to begin by acknowledging that there are many other posts discussing this topic. Nevertheless, after a thorough review, I’ve observed that none of them explicitly explain the specifics of my questions issues that I am encountering. I have several questions that I would greatly appreciate if someone could take the time to explaining how these elements function and their significance in more detail.

  

My first question involves the use of the 4 following commands:

```auto
find_library()
find_package()
target_include_directories()
target_link_libraries()

```

  

Whenever I research the topic of **“Adding third-party libraries”** , I consistently come across one of the commands mentioned above. Each response typically addresses how to resolve issues by altering these commands or solving common small developer mistakes. However, none of them dive deeper into the actual functionality of these commands. While I did consult CMake’s documentation on them, I must admit that the documentation isn’t the most user-friendly resource I’ve encountered.

  

Here’s what I’ve managed to find about these commands. I start off with the ones I understand better (Or at least I hope I understand better):

  

```auto
target_include_directories()

# Specifies the directories that have header files in them. It will add those directories with their header files to the target.

```

  

```auto
target_link_libraries()

# Links the library files to the final executable, so it can work after being exported.

```

  

```auto
find_package()

# Find a package and load its package-specific details

```

  

```auto
find_library()

# This command is used to find a library

```

  

**Here are the questions that I have about the above:**

1. If **“target\_include\_directories”** locates the header files (_.h files) and **“target\_link\_libraries”** identifies and links the necessary libraries to the final executable (_.cpp and \*.lib files), then the question arises: what purpose does **“find\_package”** serve? (Aren’t header files and their corresponding implementation libraries all that’s required to utilize a library?)

2. What sets apart **“find\_package”** from **“find\_library”**? (Both appear to serve the purpose of locating a library, yet one is labeled as a “package” and the other as a “library.” What advantages and disadvantages do they each bring to the table?)

  
  

In addition to the above commands, I’ve been wondering whether there exists a more direct and hands-on approach. Is there a way to **explicitly specify the header and library files** required for connection to one’s executable and project by giving the full path to each file, rather than depending on these commands? (I’ve noticed substantial variation between libraries – some, like the Boost library, seamlessly integrate with these commands, while others, like SFML, prove to be challenging to work with due to a lack of standardization and frequent architectural changes)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 1, 2023, 11:33am UTC](https://discourse.cmake.org/t/using-third-party-libraries/8852/2 "2023-09-01T11:33:19Z")

</div>

> [@Saviz](#):
>
> ```auto
> target_include_directories()
> 
> # Specifies the directories that have header files in them. It will add those directories with their header files to the target.
> 
> ```

This looks fine.

> [@Saviz](#):
>
> ```auto
> target_link_libraries()
> 
> # Links the library files to the final executable, so it can work after being exported.
> 
> ```

This is correct, but not a complete picture. `tll` works by applying usage requirements of targets (the “right hand side”) to other targets (the “left hand side”). So `target_link_libraries(tgt PRIVATE foo)` says “give `tgt` the usage requirements of `foo`, but not those using `tgt`”. Usage requirements can be things like compile definitions, include directories, compile options, link options, libraries to link, and a few more.

> [@Saviz](#):
>
> ```auto
> find_package()
> 
> # Find a package and load its package-specific details
> 
> ```

Pretty much correct. Note that what it means to “find” a specific package can vary greatly.

> [@Saviz](#):
>
> ```auto
> find_library()
> 
> # This command is used to find a library
> 
> ```

If by “find” you mean “find where a library lives on the system”, yes. It has nothing to do with the linker finding said library though.

> [@Saviz](#):
>
> If **“target\_include\_directories”** locates the header files (_.h files) and **“target\_link\_libraries”** identifies and links the necessary libraries to the final executable (_.cpp and \*.lib files), then the question arises: what purpose does **“find\_package”** serve? (Aren’t header files and their corresponding implementation libraries all that’s required to utilize a library?)

Here I see some confusion. `target_include_directories(tgt)` does not “locate” anything. It just says “tell the compiler when to add these directories to the include search path when compiling `tgt`”. This is basically a set of `-I` flags pretty much everywhere.

There are two parts:

- finding where things are on the system
- using them for specific things

So the `find_*` commands all handle the first part. But they do nothing about actually _using_ this information. That belongs to the `target_*` family of commands.

> [@Saviz](#):
>
> What sets apart **“find\_package”** from **“find\_library”**? (Both appear to serve the purpose of locating a library, yet one is labeled as a “package” and the other as a “library.” What advantages and disadvantages do they each bring to the table?)

A package is a logical collection of artifacts (headers, libraries, resources, executables, whatever). `find_library` is about finding a specific library.
