bash CMake - 从 CMakeLists.txt 更改当前目录(例如: project/build )?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20419737/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:54:42  来源:igfitidea点击:

CMake - change current directory ( for example: project/build ) from CMakeLists.txt?

linuxbashcmake

提问by vir2al

I'm building project with CMake. While configuration and building I'm in directory project/build . How can I change the directory in CMake and execute a bash script from another directory.

我正在用 CMake 构建项目。在配置和构建时,我在目录 project/build 中。如何更改 CMake 中的目录并从另一个目录执行 bash 脚本。

execute_process( COMMAND cd ../ ) - doesn't work. When I execute this CMake doesn't change its directory and I'm again in project/build.

execute_process( COMMAND cd ../ ) - 不起作用。当我执行这个 CMake 不会改变它的目录,我又在项目/构建中。

回答by Peter

The WORKING_DIRECTORYdirective of the execute_processcommand lets you directly specify the directory the script is to be run from.

WORKING_DIRECTORY命令的execute_process指令允许您直接指定要运行脚本的目录。

execute_process(COMMAND ${CMAKE_SOURCE_DIR}/script.sh args
                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

回答by usr1234567

With ${CMAKE_SOURCE_DIR}you get the source path. Executing a binary from the source directory would be

随着${CMAKE_SOURCE_DIR}你得到源路径。从源目录执行二进制文件将是

execute_process(${CMAKE_SOURCE_DIR}/bin/myscript.sh)

If you need to handle files from the build directory you have to add them like

如果您需要处理构建目录中的文件,则必须像这样添加它们

execute_process(${CMAKE_SOURCE_DIR}/bin/myscript.sh ${CMAKE_CURRENT_BINARY_DIR}/input.txt)

Overall the variables CMAKE_SOURCE_DIR, CMAKE_CURRENT_SOURCE_DIR, CMAKE_BINARY_DIR, and CMAKE_CURRENT_BINARY_DIRare very helpful. You can find a more complete list at http://www.cmake.org/Wiki/CMake_Useful_Variables

总的来说,变量CMAKE_SOURCE_DIRCMAKE_CURRENT_SOURCE_DIRCMAKE_BINARY_DIRCMAKE_CURRENT_BINARY_DIR非常有用。您可以在http://www.cmake.org/Wiki/CMake_Useful_Variables找到更完整的列表