# cmake \_\_FILE\_NAME\_\_ in a portable way?

**URL:** <https://discourse.cmake.org/t/cmake-file-name-in-a-portable-way/6918>\
**Category:** Code\
**Created:** [November 21, 2022, 10:51am UTC](https://discourse.cmake.org/t/cmake-file-name-in-a-portable-way/6918 "2022-11-21T10:51:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)\
**Post date:** [November 21, 2022, 10:51am UTC](https://discourse.cmake.org/t/cmake-file-name-in-a-portable-way/6918/1 "2022-11-21T10:51:43Z")

</div>

gcc 12 has ` __FILE__ NAME __` defined which is a great improvement to `__ FILE__` for reducing the string contamination in the binaries. In the lack of gcc 12, is there a way to have cmake generate a base file name to the pre-processed file in a portable way? If often see this makefile hack in cmake, but this does not work on e.g. ninja

```cmake
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D __FILENAME__ ='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")

```

I was trying to make use of ` -fmacro-prefix-map`, so at least I can eliminate the path to the source tree, but it does not give me the base name of the file.

---

<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 21, 2022, 1:41pm UTC](https://discourse.cmake.org/t/cmake-file-name-in-a-portable-way/6918/2 "2022-11-21T13:41:10Z")

</div>

AFAIK, there’s no portable way to do this without per-source properties setting some common define.

---

<div class="post-metadata">

**Author:** ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)\
**Post date:** [November 21, 2022, 2:04pm UTC](https://discourse.cmake.org/t/cmake-file-name-in-a-portable-way/6918/3 "2022-11-21T14:04:29Z")

</div>

I see. If I decide to backport this to an older gcc, can I then with cmake check if the builtin macro exists? Similar way that `check_symbol_exists` works but for gcc macros…

---

<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 21, 2022, 2:09pm UTC](https://discourse.cmake.org/t/cmake-file-name-in-a-portable-way/6918/4 "2022-11-21T14:09:03Z")

</div>

`check_symbol_exists` should work for macros too.
