# cmake does not find libraries when compiling for Android

**URL:** https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098
**Category:** Usage
**Tags:** os:linux, os:android
**Created:** [February 23, 2022, 2:10pm UTC](https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098 "2022-02-23T14:10:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![emmenlau](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@emmenlau](https://discourse.cmake.org/u/emmenlau)
#### Post date: [February 23, 2022, 2:10pm UTC](https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098/1 "2022-02-23T14:10:35Z")

</div>

I’ve just today discovered that its surprisingly simple to compile c++ projects with `cmake` for Android. I use Ubuntu Linux as a host, with the latest NDK. The build worked smoothly for a whole number of standard libraries (zlib, bzip2, Zstandard, and a few others)!

I’ve used the following parameters (I guess some of these are not even required, but I add them for completeness):

```auto
-GNinja
-DCMAKE_PREFIX_PATH=<TARGETDIR>/Debug;<TARGETDIR>/Debug/lib/cmake
-DCMAKE_INSTALL_PREFIX=<TARGETDIR>
-DCMAKE_BUILD_TYPE=Debug
-DCMAKE_CXX_STANDARD=20
-DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON
-DCMAKE_POLICY_DEFAULT_CMP0074=NEW
-DCMAKE_POLICY_DEFAULT_CMP0121=NEW
-DCMAKE_POLICY_DEFAULT_CMP0115=NEW
-DCMAKE_POLICY_DEFAULT_CMP0108=NEW
-DCMAKE_POLICY_DEFAULT_CMP0107=NEW
-DCMAKE_POLICY_DEFAULT_CMP0103=NEW
-DCMAKE_POLICY_DEFAULT_CMP0100=NEW
-DCMAKE_POLICY_DEFAULT_CMP0128=NEW
-DCMAKE_TOOLCHAIN_FILE=<NDKDIR>/22.1.7171670/build/cmake/android.toolchain.cmake
-DANDROID_NATIVE_API_LEVEL=30
-DANDROID_ABI=arm64-v8a

```

Now, while the build works, the find scripts from cmake seem not to work! Since the same build settings work on the host computer (just removing the Android specifics), I assume all paths etc should be correct.

For example, when building [pcre2](https://github.com/PhilipHazel/pcre2), it complains that bzip2 can not be found, even though its compiled and installed with cmake (including a package configuration). Similarly, when building Qt, it will not find bzip2, Zstandard and OpenSSL.

Is there something special needed when cross-compiling, or when building for Android?

I’m using cmake version 3.22.2-g45390d0 that I build from sources, and Ninja 1.10.2 also built from sources.

---

<div class="post-metadata">

### Author: ![emmenlau](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@emmenlau](https://discourse.cmake.org/u/emmenlau)
#### Post date: [February 23, 2022, 7:20pm UTC](https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098/2 "2022-02-23T19:20:24Z")

</div>

Ok, I’ve found the solution a few hours later. This should _definitely_ be better documented, or maybe cmake could even help isolate the problem. The helpful discussion is in [https://github.com/android/ndk/issues/912:](https://github.com/android/ndk/issues/912:) The Android NDK cmake toolchain overrides `CMAKE_FIND_ROOT_PATH`.

I did not consider that there is such a variable that makes cmake essentially ignore `CMAKE_PREFIX_PATH`? I think it would be _very_ helpful if cmake would warn when `CMAKE_PREFIX_PATH` points outside of `CMAKE_FIND_ROOT_PATH`.

---

<div class="post-metadata">

### Author: ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)
#### Post date: [June 8, 2022, 7:22am UTC](https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098/3 "2022-06-08T07:22:38Z")

</div>

Posting issue link for completeness [https://gitlab.kitware.com/cmake/cmake/-/issues/17272#note\_1159129](https://gitlab.kitware.com/cmake/cmake/-/issues/17272#note_1159129)

---

<div class="post-metadata">

### Author: ![Praveen\_CK](https://discourse.cmake.org/user_avatar/discourse.cmake.org/praveen_ck/32/2534_2.png) [@Praveen\_CK](https://discourse.cmake.org/u/Praveen_CK)
#### Post date: [June 24, 2022, 12:24pm UTC](https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098/4 "2022-06-24T12:24:04Z")

</div>

> [@emmenlau](#):
>

I am also facing same issue, how did you solve ?

---

<div class="post-metadata">

### Author: ![emmenlau](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@emmenlau](https://discourse.cmake.org/u/emmenlau)
#### Post date: [June 25, 2022, 12:07pm UTC](https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098/5 "2022-06-25T12:07:49Z")

</div>

@Praveen_CK , the solution for me was to put all paths from `CMAKE_PREFIX_PATH` also in `CMAKE_FIND_ROOT_PATH`, then it worked.

For me that boils down to something like:

```auto
cmake \
    "-DCMAKE_PREFIX_PATH=${TARGETDIR};${TOOLTARGETDIR};${EXTRASTARGETDIR}" \
    "-DCMAKE_FIND_ROOT_PATH=${TARGETDIR};${TOOLTARGETDIR};${EXTRASTARGETDIR}" \
    ...

```

---

<div class="post-metadata">

### Author: ![zjyhjqs](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zjyhjqs/32/4610_2.png) [@zjyhjqs](https://discourse.cmake.org/u/zjyhjqs)
#### Post date: [May 26, 2024, 10:44am UTC](https://discourse.cmake.org/t/cmake-does-not-find-libraries-when-compiling-for-android/5098/6 "2024-05-26T10:44:52Z")

</div>

set `CMAKE_FIND_ROOT_PATH_MODE_PACKAGE` to `BOTH`.

[https://cmake.org/cmake/help/latest/command/find\_package.html#id9](https://cmake.org/cmake/help/latest/command/find_package.html#id9)

> The default behavior can be adjusted by setting [`CMAKE_FIND_ROOT_PATH_MODE_PACKAGE`](https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_ROOT_PATH_MODE_PACKAGE.html#variable:CMAKE_FIND_ROOT_PATH_MODE_PACKAGE).
