# Best practices on finding third party libraries for a project?

**URL:** https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170
**Category:** Usage
**Created:** [November 8, 2019, 1:06pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170 "2019-11-08T13:06:14Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![thomthom](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thomthom/32/141_2.png) [@thomthom](https://discourse.cmake.org/u/thomthom)
#### Post date: [November 8, 2019, 1:06pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170/1 "2019-11-08T13:06:15Z")

</div>

I’m new to CMake. Trying to read up on best practices and common concepts.

One thing which eludes me is how to be able to provide CMake with paths for external libraries such that `find_library` finds them. The documentation refer to a number of variables used by CMake, but I’m struggling to work out which ones to use when.

Example, I have a project where I need external libraries. These libs cannot be bundled with my project so it must be supplied by the system/user somehow. (I’m targeting Windows and Mac)

To avoid hard coding paths that relate to my own machine I’m trying to figure out how to pass the appropriate directories to the project and using `find_library` and `find_path`.

One thing I tried, (using VSCode along with the CMake Tools extension) was to set the user config to:

```auto
    "cmake.configureSettings": {
        "CMAKE_INCLUDE_PATH": "<path>",
        "CMAKE_LIBRARY_PATH": "<path>"
    }
}

```

And this works, but for both variables CMake documentation says:

> By default it is empty, it is intended to be set by the project.

So it sounds like I’m not using the variables appropriately by setting them in a user setting.

What’s common practice? Add all these paths to the system environment PATH variable? (Is there a CMAKE environment variable that I can avoid using the PATH variable?)

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [November 8, 2019, 3:10pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170/2 "2019-11-08T15:10:31Z")

</div>

Take a look at [`CMAKE_FIND_ROOT_PATH`](https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_ROOT_PATH.html).

---

<div class="post-metadata">

### Author: ![thomthom](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thomthom/32/141_2.png) [@thomthom](https://discourse.cmake.org/u/thomthom)
#### Post date: [November 8, 2019, 3:28pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170/3 "2019-11-08T15:28:32Z")

</div>

> [@kyle.edwards](#):
>
> Take a look at [`CMAKE_FIND_ROOT_PATH`](https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_ROOT_PATH.html).

I don’t see that listed under CMake env variables. So presumably this is only a configuration variable? `cmake -DCMAKE_FIND_ROOT_PATH=<paths>`?

Any canonical way to set this as a machine config for CMake? Or do people usually always pass in these types of paths when configuring CMake?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [November 8, 2019, 3:33pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170/4 "2019-11-08T15:33:07Z")

</div>

> [@thomthom](#):
>
> I don’t see that listed under CMake env variables. So presumably this is only a configuration variable? `cmake -DCMAKE_FIND_ROOT_PATH=<paths>` ?

Yes.

> [@thomthom](#):
>
> Or do people usually always pass in these types of paths when configuring CMake?

Yes, these variables are meant to be passed on the command line. They explain to the project what your system looks like.

> [@thomthom](#):
>
> Any canonical way to set this as a machine config for CMake?

I’m not sure what you mean by this.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [November 8, 2019, 3:37pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170/5 "2019-11-08T15:37:13Z")

</div>

> [@kyle.edwards](#):
>
> I’m not sure what you mean by this.

Never mind, the below quote gave me insight into what you’re asking:

> [@thomthom](#):
>
> To avoid hard coding paths that relate to my own machine I’m trying to figure out how to pass the appropriate directories to the project and using `find_library` and `find_path` .

Indeed, using `find_*()` is the best approach to make your project cross-platform. And as I said above, machine-specific settings should be passed on the command line rather than hard-coded in your project.

That said, you can use the `-C` argument to pass a script file that automatically sets your system-specific variables, or write a shell script that passes them to CMake automatically, to avoid having to repeat them every time you configure your project.

---

<div class="post-metadata">

### Author: ![thomthom](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thomthom/32/141_2.png) [@thomthom](https://discourse.cmake.org/u/thomthom)
#### Post date: [November 8, 2019, 4:12pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170/6 "2019-11-08T16:12:56Z")

</div>

> [@kyle.edwards](#):
>
> That said, you can use the `-C` argument to pass a script file that automatically sets your system-specific variables, or write a shell script that passes them to CMake automatically, to avoid having to repeat them every time you configure your project.

Gotcha - thank you for the insight.

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [November 8, 2019, 7:45pm UTC](https://discourse.cmake.org/t/best-practices-on-finding-third-party-libraries-for-a-project/170/7 "2019-11-08T19:45:43Z")

</div>

I prefer [CMAKE\_PREFIX\_PATH](https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html) . It can be set via the command line or as an environment variable ( 3.6+ ).
