# Is there a "$/", an easy "path-separator" variable?

**URL:** https://discourse.cmake.org/t/is-there-a-an-easy-path-separator-variable/4562
**Category:** Code
**Created:** [November 27, 2021, 11:32pm UTC](https://discourse.cmake.org/t/is-there-a-an-easy-path-separator-variable/4562 "2021-11-27T23:32:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kfsone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kfsone/32/1880_2.png) [@kfsone](https://discourse.cmake.org/u/kfsone)
#### Post date: [November 27, 2021, 11:32pm UTC](https://discourse.cmake.org/t/is-there-a-an-easy-path-separator-variable/4562/1 "2021-11-27T23:32:37Z")

</div>

Does CMake have some (easy) way to inject a platform-specific path separator? Something like “$/”?

```auto
SET(DESTINATION_DIR "${CMAKE_CURRENT_LIST_DIR}$/..$/bin")

```

Also … if there isn’t, it would be awesome if it could automatically swallow whitespace, so that:

```auto
SET(DESTINATION_DIR "${CMAKE_CURRENT_LIST_DIR} $/ .. $/ bin")

```

would expand to a well formed path without the spaces.

I guess a path join command would be an equally good way to do it:

```auto
get_joined_path(DESTINATION_DIR ${CMAKE_CURRENT_LIST_DIR} .. bin)

```

though this seems like it might trip up over “C:\Program Files (x86)” 🙂

---

<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 28, 2021, 12:32am UTC](https://discourse.cmake.org/t/is-there-a-an-easy-path-separator-variable/4562/2 "2021-11-28T00:32:29Z")

</div>

There is [`file(TO_NATIVE_PATH)`](https://cmake.org/cmake/help/latest/command/file.html#to-native-path) to convert where needed. But you should use `/` everywhere within CMake code.

---

<div class="post-metadata">

### Author: ![kfsone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kfsone/32/1880_2.png) [@kfsone](https://discourse.cmake.org/u/kfsone)
#### Post date: [November 28, 2021, 2:04am UTC](https://discourse.cmake.org/t/is-there-a-an-easy-path-separator-variable/4562/3 "2021-11-28T02:04:30Z")

</div>

> But you should use `/` everywhere within CMake code.

Can you expand on why?

It produces confusing paths like `C:\Program Files (x86)\Company\Product/1.3.1\include/headers`

Surely there are no places where CMake doesn’t understand `\`'?

And trying to teach a large base of developers with limited cross-platform experience and/or rare contact with CMakeLists “You should always use `/`” is largely received as “forget about paths” and makes trying to teach them they have to normalize paths in some weird edgecases much harder.

I’m actually considering doing

```auto
if (WIN32) set("/" "\\") else() set("/" "/") endif()
...
set(_library_path "${CMAKE_CURRENT_LIST_DIR}${/}${_version_folder}${/}library.lib")

```

the {}s are required since to stop `$` and `/` being recognized as individual tokens rather than a single token, but it’s probably easier to teach once it’s been instigated across all our current cmakelists.txt and \*.cmake files.

---

<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: [November 28, 2021, 9:53am UTC](https://discourse.cmake.org/t/is-there-a-an-easy-path-separator-variable/4562/4 "2021-11-28T09:53:08Z")

</div>

Definitively, to avoid any trouble about escaping, especially when you pass variable contents to commands, it is **strongly** recommended to use **exclusively** the slash as path separator.

Moreover, to be complete, [`file(TO_NATIVE_PATH)`](https://cmake.org/cmake/help/latest/command/file.html#to-native-path) is now superseded by [cmake\_path](https://cmake.org/cmake/help/latest/command/cmake_path.html) command.  
This command offers wide possibilities to deal with paths. For example, to compute a path from fragments:

```cmake
cmake_path(APPEND DESTINATION_DIR "${CMAKE_CURRENT_LIST_DIR}" ".." "bin")

```
