bash 如何使用 CMake 获取目标的相对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25433033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a relative path to a target with CMake?
提问by Alex
I have a project that uses CMake to generate build scripts, and each platform puts the executable that are generated in a different place. We have a non-programmer on our team who does not have build tools and I want to be able to bundle up all the files, scripts and executables necessary to run the project so that he can run the project.
我有一个使用CMake生成构建脚本的项目,每个平台将生成的可执行文件放在不同的地方。我们团队中有一位非程序员,他没有构建工具,我希望能够将运行项目所需的所有文件、脚本和可执行文件捆绑在一起,以便他可以运行项目。
To do that, I've added a custom target that takes all the necessary files and zips them up. It also generates a very simple script (which is included in the zip file) that he can just click on that should run a script then launch the executable, like so:
为此,我添加了一个自定义目标,用于获取所有必要的文件并将它们压缩。它还会生成一个非常简单的脚本(包含在 zip 文件中),他只需单击该脚本即可运行脚本,然后启动可执行文件,如下所示:
add_custom_target(runcommand
COMMAND echo '\#!/bin/bash -eu' > run.command &&
echo 'cd `dirname $file(RELATIVE_PATH variable directory file)
`' >> run.command &&
echo './scripts/prerun_script.sh && ${MY_EXECUTABLE}' >> run.command &&
chmod +x run.command)
The problem with this is that MY_EXECUTABLE is a hardcoded path to the executable on my system. I would like it to be a relative path so that I can just take this resultant zip file, unzip it anywhere and run it from there. What I would like is to get the path to MY_EXECUTABLE relative to the root directory of my project so that this script can be run from anywhere.
问题在于 MY_EXECUTABLE 是我系统上可执行文件的硬编码路径。我希望它是一个相对路径,这样我就可以把这个生成的 zip 文件解压到任何地方,然后从那里运行它。我想要的是获取相对于我的项目根目录的 MY_EXECUTABLE 路径,以便可以从任何地方运行该脚本。
回答by Lindydancer
You can use:
您可以使用:
file(RELATIVE_PATH rel ${MY_ROOT} ${MY_EXECUTABLE})
Concretely, assume that MY_ROOT
contains the root directory (there might be a suitable predefined CMake variable for this), this will set rel
to the relative path of MY_EXECUTABLE
:
具体来说,假设MY_ROOT
包含根目录(可能有一个合适的预定义 CMake 变量),这将设置rel
为的相对路径MY_EXECUTABLE
:
file(RELATIVE_PATH rel ${PROJECT_SOURCE_DIR} ${MY_EXECUTABLE})
回答by Innokentiy Alaytsev
In case where you want to get path relative to project root, you may use PROJECT_SOURCE_DIR
CMake variable:
如果您想获取相对于项目根目录的路径,您可以使用PROJECT_SOURCE_DIR
CMake 变量:
THISDIR=`pwd`
MY_EXECUTABLE=$THISDIR/test/someexec
REL_EXECUTABLE=${MY_EXECUTABLE##$THISDIR/}
echo $REL_EXECUTABLE
Remember, that MY_EXECUTABLE
must contain full path to file.
请记住,它MY_EXECUTABLE
必须包含文件的完整路径。
回答by zerodiff
Something like this would work:
像这样的事情会起作用:
${MY_EXECUTABLE##`pwd`/}
Basically, this chops off the base path from $MY_EXECUTABLE
. In this example, I just set up MY_EXECUTABLE
as a test to give it a full path. The one-line equivalent for what you want is:
基本上,这从$MY_EXECUTABLE
. 在这个例子中,我只是设置了MY_EXECUTABLE
一个测试来给它一个完整的路径。您想要的单行等效项是:
I guess this assumes pwd
is the root of your project. Otherwise, see the Cmake variabledoc.
我想这假设pwd
是您项目的根。否则,请参阅Cmake 变量文档。
(As an aside, this makes me nostalgic for the ksh
days.)
(顺便说一句,这让我怀念那些ksh
日子。)