Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-platform paths in CMake

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?

like image 534
user3534974 Avatar asked May 22 '14 07:05

user3534974


People also ask

Is CMake cross-platform?

CMake is a cross-platform build system generator. Projects specify their build process with platform-independent CMake listfiles included in each directory of a source tree with the name CMakeLists.

How do I specify CMake path?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .

What is CMake used for?

CMake is an open-source, cross-platform tool that uses compiler and platform independent configuration files to generate native build tool files specific to your compiler and platform. The CMake Tools extension integrates Visual Studio Code and CMake to make it easy to configure, build, and debug your C++ project.


1 Answers

You question affects different details:

  1. Just don't use backslashes. Windows will also process slashes.
  2. Don't use drive letters. Use relative paths everywhere.
  3. GET_FILENAME_COMPONENT(X "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH) can solve the whole path without writing any absolute paths.
  4. Add the tool binary paths into your PATH environment variable. So you do not need to search them by yourself (with absolute paths) or
  5. 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
  6. 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).
  7. If all those does not help, you can detect the platform by IF(WIN32) or IF(UNIX).

Hope, this helps...

like image 166
Stefan Weiser Avatar answered Sep 25 '22 14:09

Stefan Weiser