# CMAKE\_PREFIX\_PATH does not work as expected

**URL:** https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677
**Category:** Usage
**Created:** [August 6, 2020, 3:22pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677 "2020-08-06T15:22:11Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![fifothekid](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fifothekid/32/783_2.png) [@fifothekid](https://discourse.cmake.org/u/fifothekid)
#### Post date: [August 6, 2020, 3:22pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/1 "2020-08-06T15:22:11Z")

</div>

I’m compiling the same Qt/QML application on Windows, Linux, MacOS, WebAssembly, iOS, and Android. So far, I found that detection of Qt’s components only works on Windows, regardless whether I’m using it to compile a Windows version or cross-compiling to Android. It doesn’t work neither on MacOS nor on Linux.

I tried pointing CMAKE\_PREFIX\_PATH to Qt’s installation prefix (~/Qt/5.15.0/wasm\_32/), but it keeps on complaining about QtConfig.cmake. The only alternative I found to work was passing -DQt5\_DIR for QtConfig.cmake to be found, but then it would keep on asking for at least 10 more .cmake files which I need to pass their paths one by one using \_DIR variables. This way I would always end with a huge command and it’s a major source of annoyance.

Note that I do not want to install Qt using apt or any other package manager. I only want to use Qt’s installer since it gives me the flexibility of having multiple versions targeting multiple frameworks.

Is it a problem in Qt? CMake? Ubuntu?

Qt: 5.15.0

CMake: 3.18.0

Ubuntu: 20.04

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [August 6, 2020, 4:49pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/2 "2020-08-06T16:49:40Z")

</div>

this is likely not a problem with Qt, CMake or Ubuntu. Prefix path is working just fine for me on Ubuntu.

> [@fifothekid](#):
>
> I tried pointing CMAKE\_PREFIX\_PATH to Qt’s installation prefix (~/Qt/5.15.0/wasm\_32/), but it keeps on complaining about QtConfig.cmake.

Maybe you are not providing the correct path. Can you post the folder structure for the \*.cmake file you want to be found in your build?

---

<div class="post-metadata">

### Author: ![fifothekid](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fifothekid/32/783_2.png) [@fifothekid](https://discourse.cmake.org/u/fifothekid)
#### Post date: [August 6, 2020, 6:05pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/3 "2020-08-06T18:05:21Z")

</div>

Yes, I’m pretty sure that I’m pointing to the correct path. Here is the folder structure:  
tree -d ~/Qt/5.15.0/wasm\_32/  
/home/[user]/Qt/5.15.0/wasm\_32/  
├── bin  
├── doc  
│ .  
│ .  
│ .  
├── include  
│ ├── QtAccessibilitySupport  
│ │ └── 5.15.0  
│ │ └── QtAccessibilitySupport  
│ │ └── private  
│ ├── QtBodymovin  
│ │ └── 5.15.0  
│ │ └── QtBodymovin  
│ │ └── private  
│ .  
│ .  
│ .  
├── lib  
│ ├── cmake  
│ ├── Qt5  
│ │ ├── Qt5Config.cmake  
│ │ ├── Qt5ConfigVersion.cmake  
│ │ └── Qt5ModuleLocation.cmake  
│ ├── Qt5AccessibilitySupport  
│ │ ├── Qt5AccessibilitySupportConfig.cmake  
│ │ └── Qt5AccessibilitySupportConfigVersion.cmake  
│ │ .  
│ │ .  
│ │ .  
├── mkspecs  
│ .  
│ .  
│ .  
├── qml  
│ .  
│ .  
│ .  
└── translations

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [August 6, 2020, 9:42pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/4 "2020-08-06T21:42:35Z")

</div>

Please post the command line you are using to set `CMAKE_PREFIX_PATH` and show the `find_package()` calls you are using to find Qt. Also show the exact output that demonstrates that Qt is not being found. Probably it will be easiest to diagnose this for Linux or MacOS, so please show for one of those two platforms.

---

<div class="post-metadata">

### Author: ![fifothekid](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fifothekid/32/783_2.png) [@fifothekid](https://discourse.cmake.org/u/fifothekid)
#### Post date: [August 6, 2020, 11:22pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/5 "2020-08-06T23:22:09Z")

</div>

I could solve the problem by adding this to the beginning of my CMake script:

list(APPEND CMAKE\_PREFIX\_PATH “~/Qt/5.15.0/wasm\_32”)

Neither passing it as a command-line parameter nor as an env variable worked.  
Is this expected?

EDIT: I was wrong

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [August 6, 2020, 11:24pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/6 "2020-08-06T23:24:52Z")

</div>

There seems to be more going on here than we can tell from the description so far. Please provide a _complete_ example with steps that someone else can use to reproduce. Leaving out details will only slow down anyone’s chance to help.

---

<div class="post-metadata">

### Author: ![fifothekid](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fifothekid/32/783_2.png) [@fifothekid](https://discourse.cmake.org/u/fifothekid)
#### Post date: [August 6, 2020, 11:26pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/7 "2020-08-06T23:26:09Z")

</div>

I was wrong. I made the change then tried to run the configuration in a build folder that was already configured by passing all the library paths manually.

---

<div class="post-metadata">

### Author: ![fifothekid](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fifothekid/32/783_2.png) [@fifothekid](https://discourse.cmake.org/u/fifothekid)
#### Post date: [August 6, 2020, 11:42pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/8 "2020-08-06T23:42:05Z")

</div>

Now I’m sure that I found it:  
The toolchains had this:  
set(CMAKE\_FIND\_ROOT\_PATH\_MODE\_PACKAGE ONLY)

It doesn’t look like setting CMAKE\_PREFIX\_PATH was enough. I had to do this also:  
list(APPEND CMAKE\_FIND\_ROOT\_PATH “~/Qt/5.15.0/wasm\_32”)

---

<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: [November 11, 2020, 9:11pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/9 "2020-11-11T21:11:53Z")

</div>

> [@fifothekid](#):
>
> list(APPEND CMAKE\_FIND\_ROOT\_PATH “~/Qt/5.15.0/wasm\_32”)

Using `~` in a CMake path isn’t going to help much. You should use `$HOME` (on the shell) or `$ENV{HOME}` (in CMake code) instead.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [November 11, 2020, 10:08pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/10 "2020-11-11T22:08:54Z")

</div>

I believe that using `~` does actually work in some cases. There is undocumented behavior of the `find_...()` commands where they will not re-root the set of search locations where a search location starts with a `~` character (sorry if that is a really cryptic description, it comes from a longer discussion about cross-compilation in my book). This implies that `~` does get interpreted by find operations in at least some cases. You’d have to dig into the source code to ascertain exactly what those cases are, but `cmFindCommon::RerootPaths()` looks like the code related to the specific case I’m mentioning here.

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [July 18, 2023, 12:23pm UTC](https://discourse.cmake.org/t/cmake-prefix-path-does-not-work-as-expected/1677/11 "2023-07-18T12:23:41Z")

</div>

> [@fifothekid](#):
>
> Now I’m sure that I found it:  
> The toolchains had this:  
> set(CMAKE\_FIND\_ROOT\_PATH\_MODE\_PACKAGE ONLY)

I got the same problem with Emscripten toolchain, here’s also [related discussion](https://github.com/emscripten-core/emscripten/issues/19243) in Emscripten repository.

So the solution/workaround is to:

- either set `CMAKE_FIND_ROOT_PATH` instead of `CMAKE_PREFIX_PATH`;
- or keep `CMAKE_PREFIX_PATH` but set `CMAKE_FIND_ROOT_PATH_MODE_PACKAGE` to `BOTH`.

I am not sure if any of these variants is “correct” (_because, I imagine, Emscripten toolchain disables this discovery for a reason_), but that’s what got things working for me.
