CMake中的跨平台路径 [英] Cross-platform paths in CMake

查看:144
本文介绍了CMake中的跨平台路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以使用CMake在Linux和Windows上构建的项目.唯一的问题是,CMakeLists.txt中的Unix风格的路径无法在Windows上运行(Windows使用反斜杠而不是斜杠,这也需要驱动器号).

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).

有什么办法可以编写跨平台的CMakeLists.txt?

Is there any way I can write a cross-platform CMakeLists.txt?

推荐答案

您的问题会影响不同的细节:

You question affects different details:

  1. 请不要使用反斜杠. Windows还将处理斜杠.
  2. 请勿使用驱动器号.随处使用相对路径.
  3. GET_FILENAME_COMPONENT(X "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH)可以解决整个路径,而无需编写任何绝对路径.
  4. 将工具二进制路径添加到您的PATH环境变量中.因此,您无需自己搜索它们(使用绝对路径)或
  5. 使用FIND_PROGRAM()无需猜测即可获得工具的绝对路径.您可以添加提示,其中cmake的注册表项和路径将在其中搜索该工具,或者
  6. 考虑为每个工具编写自己的模块.您可以从modules文件夹的任何模块中复制框架(在FindJava.cmake上有一个锁;这是一个很好的可移植示例,它关于如何搜索程序).
  7. 如果所有这些都无济于事,则可以通过IF(WIN32)IF(UNIX)来检测平台.
  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...

这篇关于CMake中的跨平台路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆