CMake中的跨平台路径 [英] Cross-platform paths in CMake
本文介绍了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:
- 请不要使用反斜杠. Windows还将处理斜杠.
- 请勿使用驱动器号.随处使用相对路径.
-
GET_FILENAME_COMPONENT(X "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH)
可以解决整个路径,而无需编写任何绝对路径. - 将工具二进制路径添加到您的
PATH
环境变量中.因此,您无需自己搜索它们(使用绝对路径)或 - 使用
FIND_PROGRAM()
无需猜测即可获得工具的绝对路径.您可以添加提示,其中cmake的注册表项和路径将在其中搜索该工具,或者 - 考虑为每个工具编写自己的模块.您可以从modules文件夹的任何模块中复制框架(在FindJava.cmake上有一个锁;这是一个很好的可移植示例,它关于如何搜索程序).
- 如果所有这些都无济于事,则可以通过
IF(WIN32)
或IF(UNIX)
来检测平台.
- 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)
orIF(UNIX)
.
希望,这对您有帮助...
Hope, this helps...
这篇关于CMake中的跨平台路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文