Cross-platform paths in CMake

cmake, cross-platform, linux, path, windows

Solution

You question affects different details:

- Just don't use backslashes. Windows will also process slashes.

- Don't use drive letters. Use relative paths everywhere.

- `GET_FILENAME_COMPONENT(X "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH)` can solve the whole path without writing any absolute paths.

- Add the tool binary paths into your `PATH` environment variable. So you do not need to search them by yourself (with absolute paths) or

- Use `FIND_PROGRAM()` to get the tools absolute path without guessing around. You could add hints in which registry entries and paths cmake will search for the tool or

- Consider to write your own module for every tool. You can copy the skeleton from any module of the modules folder (have a lock at FindJava.cmake; a very good and portable example on how to search a program).

- If all those does not help, you can detect the platform by `IF(WIN32)` or `IF(UNIX)`.

Hope, this helps...

Problem

I have a project I can build on both Linux and Windows using CMake. The only issue is that Unix-style paths, in CMakeLists.txt, can't work on Windows (which uses backslashes instead of slashes, also requiring the drive letter). Is there any way I can write a cross-platform CMakeLists.txt?

Original source