# How to force the find\_package searching inside a specific directory?

**URL:** https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614
**Category:** Code
**Tags:** os:linux
**Created:** [February 11, 2020, 3:47pm UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614 "2020-02-11T15:47:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Tianfang\_Sun](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tianfang_sun/32/346_2.png) [@Tianfang\_Sun](https://discourse.cmake.org/u/Tianfang_Sun)
#### Post date: [February 11, 2020, 3:47pm UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614/1 "2020-02-11T15:47:41Z")

</div>

Hi all.  
I’m trying to cross compile a Qt program using cmake. What I’m trying to do is to force the find\_package searching for a directory. From the document I read as follows:

> The default search order is designed to be most-specific to least-specific for common use cases. Projects may override the order by simply calling the command multiple times and using the `NO_*` options:
> 
> find\_package ( PATHS paths… NO\_DEFAULT\_PATH) find\_package ()

But when I trying to do things like:  
find\_package(Qt5Gui ${QT\_VERSION} PATHS my/directory/to/qt5guiconfig/file/Qt5Gui NO\_DEFAULT\_PATH)  
cmake cannot find the Qt5GuiConfig.cmake file, which is locate at the path I passed to find\_package.

Can anyone pls give me some hints? Thank you so much.

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [February 11, 2020, 9:24pm UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614/2 "2020-02-11T21:24:05Z")

</div>

When CMake fails to find a package, it usually prints a message which advises you to use the `CMAKE_MODULE_PATH` variable.  
Have you tried calling cmake with

```auto
-DCMAKE_MODULE_PATH=my/directory/to/qt5guiconfig/file/Qt5Gui

```

?

---

<div class="post-metadata">

### Author: ![Tianfang\_Sun](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tianfang_sun/32/346_2.png) [@Tianfang\_Sun](https://discourse.cmake.org/u/Tianfang_Sun)
#### Post date: [February 12, 2020, 9:16am UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614/3 "2020-02-12T09:16:55Z")

</div>

Thanks a lot for your advise Martin. But I still do not understand one thing. The CMAKE\_MODULE\_PATH is used as a global variable? Can I set different path for different modules?

---

<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: [February 16, 2020, 1:03am UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614/4 "2020-02-16T01:03:08Z")

</div>

In this case, `Qt5Gui_DIR` would probably be better. But `CMAKE_MODULE_PATH` is a list of paths, so additional directories can be added.

---

<div class="post-metadata">

### Author: ![Tianfang\_Sun](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tianfang_sun/32/346_2.png) [@Tianfang\_Sun](https://discourse.cmake.org/u/Tianfang_Sun)
#### Post date: [February 16, 2020, 11:14am UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614/5 "2020-02-16T11:14:20Z")

</div>

Thank you Ben, I do not know that the CMAKE\_MODULE\_PATH is a list. And about the Qt5Gui\_DIR, does that mean the variable like \_DIR will be automatically added to the search list for find\_package?

Ben Boeckel via CMake Discourse [noreply@discourse.cmake.org](mailto:noreply@discourse.cmake.org) 于2020年2月16日周日 上午2:03写道：

---

<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: [February 17, 2020, 11:14am UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614/6 "2020-02-17T11:14:49Z")

</div>

Yes, `<pkgname>_DIR` is special for `find_package(<pkgname>)`.

---

<div class="post-metadata">

### Author: ![Tianfang\_Sun](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tianfang_sun/32/346_2.png) [@Tianfang\_Sun](https://discourse.cmake.org/u/Tianfang_Sun)
#### Post date: [February 17, 2020, 11:20am UTC](https://discourse.cmake.org/t/how-to-force-the-find-package-searching-inside-a-specific-directory/614/7 "2020-02-17T11:20:06Z")

</div>

Thx a lot Ben, that saves my life.
