# Why does TARGET\_FILE\_DIR create a dependency on the target

**URL:** <https://discourse.cmake.org/t/why-does-target-file-dir-create-a-dependency-on-the-target/1069>\
**Category:** Code\
**Created:** [April 21, 2020, 8:50pm UTC](https://discourse.cmake.org/t/why-does-target-file-dir-create-a-dependency-on-the-target/1069 "2020-04-21T20:50:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandercox](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sandercox/32/812_2.png) [@sandercox](https://discourse.cmake.org/u/sandercox)\
**Post date:** [April 21, 2020, 8:50pm UTC](https://discourse.cmake.org/t/why-does-target-file-dir-create-a-dependency-on-the-target/1069/1 "2020-04-21T20:50:57Z")

</div>

I’m trying to create some rules to copy files to a targets library directory. I’ve pretty much solved it but my current solution had some issues that it would only really work for sure if all targets would be build of the project. Now that doesn’t really need to happen so I was figuring out an alternative.

I came pretty close until I noticed that using generator expression of TARGET\_FILE\_DIR immediately adds a dependency on the target. Afterward I add the dependency the other way around and I get a inter-target dependency graph error.

Basically what I had was:  
add\_custom\_target(${CustomLibCopyCommandName}

```
                            COMMAND ${CMAKE_COMMAND} -E echo Running ${CustomLibCopyCommandName}

                            COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${MainTarget}>/${LINKLIBRARY_PATH}

                            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TargetBIN} $<TARGET_FILE_DIR:${MainTarget}>/${LINKLIBRARY_PATH}

                            )

```

and then adding  
add\_dependencies(${MainTarget} ${CustomLibCopyCommandName})

As soon as I change TARGET\_FILE\_DIR to some other like TARGET\_FILE\_BASE\_NAME it works. In the docs it also mentions that TARGET\_FILE\_BASE\_NAME doesn’t add the dependency on tgt but I don’t understand why the depencency needs to be there for the other cases and if there is a good alternative that works with Ninja, Visual Studio and Xcode to get the correct paths for a target.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [April 22, 2020, 7:18am UTC](https://discourse.cmake.org/t/why-does-target-file-dir-create-a-dependency-on-the-target/1069/2 "2020-04-22T07:18:00Z")

</div>

This behavior is a side effect of the current implementation. This genex shares the implementation with `$<TARGET_FILE:...>` which, with reason, add a dependency on the target.

The dependency is also added for `$<TARGET_FILE_NAME:...>`. For these two genex, I agree with you, It seems a bit curious. But adding a dependency ensure, for your example, that the directory exist when your command is executed.

---

<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:** [April 27, 2020, 1:34pm UTC](https://discourse.cmake.org/t/why-does-target-file-dir-create-a-dependency-on-the-target/1069/3 "2020-04-27T13:34:11Z")

</div>

> [@marc.chevrier](#):
>
> But adding a dependency ensure, for your example, that the directory exist when your command is executed.

Yes. Nothing in CMake guarantees that directories for targets exist before the target itself is done.

---

<div class="post-metadata">

**Author:** ![esnosy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/esnosy/32/4450_2.png) [@esnosy](https://discourse.cmake.org/u/esnosy)\
**Post date:** [March 29, 2024, 3:27pm UTC](https://discourse.cmake.org/t/why-does-target-file-dir-create-a-dependency-on-the-target/1069/4 "2024-03-29T15:27:50Z")

</div>

Same problem here 😕  
I really wish it was possible to copy resources next to binary automatically  
Edit: I just found this [https://stackoverflow.com/a/45535867/8094047](https://stackoverflow.com/a/45535867/8094047), but didn’t try it yet, might help us

this is my code that uses `TARGET_FILE_DIR`, it has the dependency issue

> <https://github.com/esnosy/geobox/blob/5ccba11dc810f31e66dacec47ed27f7ce2e8c57c/src/CMakeLists.txt#L67>
