简体   繁体   English

我需要一个 CMakeLists.txt 等效于定义环境变量的此 Makefile 行

[英]I need a CMakeLists.txt equivalent to this Makefile line that defines an environment variable

I have a Makefile containing the following lines, which fish around for the root of the local CUDA tools folder and stuff an environment variable for later consumption.我有一个 Makefile 包含以下几行,它在本地 CUDA 工具文件夹的根目录中四处寻找,并填充一个环境变量供以后使用。 On my system it now yields '/usr/local/cuda-11.4', which is used to find headers and other things.在我的系统上,它现在生成“/usr/local/cuda-11.4”,用于查找标题和其他内容。

# Location of the CUDA Toolkit
CUDA_PATH = $(subst /bin/,,$(dir $(shell which nvcc)))

My heart's desire is to replicate this in a CMakeLists.txt file that I am presently building.我的心愿是在我目前正在构建的 CMakeLists.txt 文件中复制它。 I want to get the same information in a variable (or something,), and later be able to stuff it into path names.我想在变量(或其他东西)中获得相同的信息,然后能够将其填充到路径名中。 etc.等等

How can I do this?我怎样才能做到这一点? I have used add_definitions() in simpler ways.我以更简单的方式使用了 add_definitions() 。

A direct translation of your code would be to use find_program instead of which ,您的代码的直接翻译是使用find_program而不是which

find_program(NVCC_EXECUTABLE nvcc HINT ENV PATH NO_DEFAULT_PATH)

cmake_path with PARENT_PATH instead of dir使用cmake_path而不是dirPARENT_PATH

cmake_path(GET ${NVCC_EXECUTABLE} PARENT_PATH CUDA_BIN_PATH)

and string with REPLACE instead of subst .stringREPLACE而不是subst

string(REPLACE /bin/ "" CUDA_PATH ${CUDA_BIN_PATH})

However you could instead use CMake's built-in CUDA support:但是,您可以改为使用 CMake 的内置 CUDA 支持:

find_package(CUDAToolkit REQUIRED)
set(CUDA_PATH ${CUDAToolkit_LIBRARY_ROOT})

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

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