简体   繁体   English

CMake中的跨平台路径

[英]Cross-platform paths in CMake

I have a project I can build on both Linux and Windows using CMake. 我有一个项目,我可以使用CMake在Linux和Windows上构建。 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中的Unix风格路径无法在Windows上运行(它使用反斜杠而不是斜杠,也需要驱动器号)。

Is there any way I can write a cross-platform CMakeLists.txt? 有什么方法可以编写跨平台的CMakeLists.txt吗?

You question affects different details: 您的问题影响不同的细节:

  1. Just don't use backslashes. 只是不要使用反斜杠。 Windows will also process slashes. Windows也会处理斜杠。
  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. GET_FILENAME_COMPONENT(X "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH)可以在不写绝对路径的情况下解决整个路径。
  4. Add the tool binary paths into your PATH environment variable. 将工具二进制路径添加到PATH环境变量中。 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. 使用FIND_PROGRAM()来获取工具绝对路径而不需要猜测。 You could add hints in which registry entries and paths cmake will search for the tool or 您可以添加提示,其中注册表项和路径cmake将搜索该工具或
  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). 您可以从modules文件夹的任何模块中复制骨架(在FindJava.cmake上有一个锁;这是一个关于如何搜索程序的非常好的可移植示例)。
  7. If all those does not help, you can detect the platform by IF(WIN32) or IF(UNIX) . 如果所有这些都没有帮助,您可以通过IF(WIN32)IF(UNIX)检测平台。

Hope, this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM