# FetchContent ignores CMAKE\_MODULE\_PATH set in subproject

**URL:** https://discourse.cmake.org/t/fetchcontent-ignores-cmake-module-path-set-in-subproject/5486
**Category:** Code
**Created:** [April 19, 2022, 10:33am UTC](https://discourse.cmake.org/t/fetchcontent-ignores-cmake-module-path-set-in-subproject/5486 "2022-04-19T10:33:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mschmidtesd](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mschmidtesd/32/2355_2.png) [@mschmidtesd](https://discourse.cmake.org/u/mschmidtesd)
#### Post date: [April 19, 2022, 10:33am UTC](https://discourse.cmake.org/t/fetchcontent-ignores-cmake-module-path-set-in-subproject/5486/1 "2022-04-19T10:33:30Z")

</div>

I have several self-contained CMake-based library projects. They all build fine when built on their own, but problems arise, when I try to use them in a main project in connection with FetchContent.

One of the problems is, that I have an additional file included file with macros and variables, that is not found, when the libs are built as subproject.

The path is cmake/my\_macros.cmake and I tried  
`set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)`  
and  
`set(CMAKE_MODULE_PATH ${CMAKE_CURRENt_SOURCE_DIR}/cmake`  
before the  
`include(my_macros)`.

How do I have to modify the subprojects CMakeLists.txt to correctly work stand-alone and as subproject?

A hint to an example project or a howto for this kind of nested projects would also be helpful. Most what I have found is too simple and blows up in my face when extended.

Edit: I know, I can have a  
`list(APPEND CMAKE_MODULE_PATH ${subproject_SOURCE_DIR}/cmake)`  
in the main project after the call to FetchContent\_Declare. But all the libraries use the same name for the included file but might have different contents in this file, so fetching multiple libraries create a naming collision.

---

<div class="post-metadata">

### Author: ![mschmidtesd](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mschmidtesd/32/2355_2.png) [@mschmidtesd](https://discourse.cmake.org/u/mschmidtesd)
#### Post date: [April 20, 2022, 8:29am UTC](https://discourse.cmake.org/t/fetchcontent-ignores-cmake-module-path-set-in-subproject/5486/2 "2022-04-20T08:29:38Z")

</div>

Okay, my own fault or rather a dirty build directory. Somehow changing `${CMAKE_SOURCE_DIR}` to `${CMAKE_CURRENT_SOURCE_DIR}` wasn’t effective when reconfiguring. After cleaning the the build dir and restarting the configuration process worked fine, so  
`set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})`  
`include(my_macros)`  
is the way to go.

---

<div class="post-metadata">

### Author: ![aitzkora](https://discourse.cmake.org/user_avatar/discourse.cmake.org/aitzkora/32/4857_2.png) [@aitzkora](https://discourse.cmake.org/u/aitzkora)
#### Post date: [August 15, 2024, 1:57pm UTC](https://discourse.cmake.org/t/fetchcontent-ignores-cmake-module-path-set-in-subproject/5486/3 "2024-08-15T13:57:14Z")

</div>

Thanks to you, I solved the same probleme. I was convinced that FetchContent did not user my top level CMakelists.txt, but in fact, it was exactly this probleme!
