CMake:如何在 CMakeLists.txt 中使用 bash 命令

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

CMake : how to use bash command in CMakeLists.txt

bashcmake

提问by claf

I'm wondering how I can use bash command in CMakeLists.txt. What I want is to get the number of processor retrieved using :

我想知道如何在 CMakeLists.txt 中使用 bash 命令。我想要的是使用以下方法获取检索到的处理器数量:

export variable=`getconf _NPROCESSORS_ONLN`

and set NB_PROCESSOR to variable using something like :

并使用以下内容将 NB_PROCESSOR 设置为变量:

SET (NB_PROCESSOR variable)

So my question is how can I use the getconf command in CMakeLists.txt and how can I use the result (stored in variable) in CMake SET command?

所以我的问题是如何在 CMakeLists.txt 中使用 getconf 命令以及如何在 CMake SET 命令中使用结果(存储在变量中)?

采纳答案by richq

This seems to do the trick, and saves the "set" too.

这似乎可以解决问题,并且也可以保存“设置”。

execute_process(COMMAND getconf  _NPROCESSORS_ONLN
                OUTPUT_VARIABLE NB_PROCESSOR)

回答by Jiang Bian

Use the EXEC_PROGRAM command and then use the CACHE option of the SET command to save the output to a variable like GTK_PKG_FLAGS. Then use the SET command to add the value. Something like this:

使用 EXEC_PROGRAM 命令,然后使用 SET 命令的 CACHE 选项将输出保存到像 GTK_PKG_FLAGS 这样的变量。然后使用 SET 命令添加值。像这样的东西:

IF(NOT GTK_PKG_FLAGS)
   EXEC_PROGRAM(pkg-config ARGS --cflags --libs gtkmm
                OUTPUT_VARIABLE GTK_PKG_FLAGS)
   SET(GTK_PKG_FLAGS "${GTK_PKG_FLAGS}" CACHE STRING "GTK Flags")
ENDIF(NOT GTK_PKG_FLAGS)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GTK_PKG_FLAGS}")

Links: http://www.cmake.org/pipermail/cmake/2005-January/006051.html

链接:http: //www.cmake.org/pipermail/cmake/2005-January/006051.html