# Call find\_file from a cmake function?

**URL:** https://discourse.cmake.org/t/call-find-file-from-a-cmake-function/4588
**Category:** Code
**Created:** [December 3, 2021, 11:07am UTC](https://discourse.cmake.org/t/call-find-file-from-a-cmake-function/4588 "2021-12-03T11:07:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![amzh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@amzh](https://discourse.cmake.org/u/amzh)
#### Post date: [December 3, 2021, 11:07am UTC](https://discourse.cmake.org/t/call-find-file-from-a-cmake-function/4588/1 "2021-12-03T11:07:41Z")

</div>

Hello,

I’m having trouble making my function FIND\_THIRD\_PARTY\_LIB work.

Inside it I call find\_file but the parameter holding the result doesn’t renew apparently because

> If the full path to a file is found the result is stored in the variable and the search will not be repeated unless the variable is cleared.

**function(LS\_FIND\_THIRD\_PARTY\_LIB name return\_path )**

* * *

\*\* execute\_process( \*\*  
\*\* COMMAND ls -r -1 …/…/…/3rdParty/${name} \*\*  
\*\* OUTPUT\_VARIABLE out\_dirs\*\*  
\*\* OUTPUT\_STRIP\_TRAILING\_WHITESPACE\*\*  
\*\* )\*\*  
\*\* string(REPLACE “\n” “;” l1 ${out\_dirs})\*\*  
\*\* list (GET l1 0 HEAD)\*\*

* * *

\*\* find\_file(var\_return NAMES “lib${return\_path}.a” PATHS “…/…/…/…/…/3rdParty/${name}/${HEAD}/macOS” NO\_CACHE NO\_DEFAULT\_PATH)\*\*

* * *

\*\* set(${return\_path} ${var\_return} PARENT\_SCOPE)\*\*

* * *

**endfunction()**

So basically after each call of that function var\_return is the same (the very first result)

Please help, perhaps I’m doing something wrong with the variable settings? Or perhaps it’s impossible to use find\_file like that?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [December 3, 2021, 11:45am UTC](https://discourse.cmake.org/t/call-find-file-from-a-cmake-function/4588/2 "2021-12-03T11:45:45Z")

</div>

You just have to vary the variable name. An easy way to do that would be something like:

```auto
 find_file(var_return_${name}
           NAMES “lib${return_path}.a” #BTW: is this right?
           PATHS “…/…/…/…/…/3rdParty/${name}/${HEAD}/macOS”
           NO_CACHE NO_DEFAULT_PATH)

set(${return_path} ${var_return_${name}} PARENT_SCOPE)

```

---

<div class="post-metadata">

### Author: ![amzh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@amzh](https://discourse.cmake.org/u/amzh)
#### Post date: [December 3, 2021, 11:50am UTC](https://discourse.cmake.org/t/call-find-file-from-a-cmake-function/4588/3 "2021-12-03T11:50:22Z")

</div>

Thanks. I need to have a different name for var\_return\_ each call?

(yes, that’s right in the code, I reuse that parameter, set that to lib name first and then return as a new path)

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [December 3, 2021, 12:03pm UTC](https://discourse.cmake.org/t/call-find-file-from-a-cmake-function/4588/4 "2021-12-03T12:03:12Z")

</div>

> [@amzh](#):
>
> Thanks. I need to have a different name for var\_return\_ each call?

Yep. That’s used for caching, so if you look for the same `name` more than once it’ll just return what’s in the cache. You can have other way to differentiate the name but it probably be somehow connected to the thig you’re looking for to take advantage of caching.  
In the odd case (very unlikely) you don’t want caching behaviour at all you could also just reset the cache variable.
