"make" 命令使用了错误的 clang 编译器(编译 CUDA 示例)- 如何解决这个问题?

"make" command is using wrong clang compiler (to compile CUDA samples) - how to fix this?

我正在使用 MacOS Sierra 10.12.4 并尝试让 CUDA 8.0 在我的 MacBook Pro(2013 年末)上使用 CUDA 兼容的 NVIDIA GeForce GT 750M。

我按照 NVIDIA installation guide 安装了 Xcode 8.2 和相应的命令行工具,但是当我尝试编译示例时出现以下错误:

$ make -C 1_Utilities/deviceQuery

/Developer/NVIDIA/CUDA-8.0/bin/nvcc -ccbin clang++ -I../../common/inc -m64 -Xcompiler -arch -Xcompiler x86_64 -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_60,code=compute_60 -o deviceQuery.o -c deviceQuery.cpp
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
nvcc fatal : The version ('30900') of the host compiler ('clang') is not supported
make: *** [deviceQuery.o] Error 1

这是我认为的问题所在:
"make" 命令使用了错误的 clang,但我不知道如何 change/fix 这个。

在这里你可以看到我机器上的两个版本的 clang:

$ clang --version

clang version 3.9.0 (tags/RELEASE_390/final)
Target: x86_64-apple-darwin16.5.0 Thread model: posix
InstalledDir: /opt/local/libexec/llvm-3.9/bin

$ /usr/bin/clang --version

Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.5.0
Thread model: posix InstalledDir: /Applications/Xcode_8.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

如何让 "make" 命令默认使用文件夹 /usr/bin/clang 中的正确 clang 版本?

或者有没有办法通过添加一些 parameters/tags 来告诉 "make" 命令使用文件夹 /usr/bin/clang 中的 clang 版本?

这也是我的 ~/.bash_profile 的样子,如果有帮助的话:

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

# MacPorts Installer addition on 2016-09-26_at_12:06:30: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

# colorful terminal
export PS1="\[3[36m\]\u\[3[m\]@\[3[32m\]\h:\[3[33;1m\]\w\[3[m\]$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'

# CUDA
export PATH=/Developer/NVIDIA/CUDA-8.0.61/bin${PATH:+:${PATH}}
export DYLD_LIBRARY_PATH=/Developer/NVIDIA/CUDA-8.0.61/lib\
                         ${DYLD_LIBRARY_PATH:+:${DYLD_LIBRARY_PATH}}

通过make -n作为干货运行,可以看到make背后真正的命令。由于我没有基于 nvidia 的 mac,我将向您展示 linux 版本作为示例:

"/usr/local/cuda-8.0"/bin/nvcc -ccbin g++ -I../../common/inc  -m64    -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_60,code=compute_60 -o deviceQuery.o -c deviceQuery.cpp
"/usr/local/cuda-8.0"/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_60,code=compute_60 -o deviceQuery deviceQuery.o
mkdir -p ../../bin/x86_64/linux/release
cp deviceQuery ../../bin/x86_64/linux/release

你可以看到-ccbin(或--compiler-bindir)是设置主机编译器的实际参数。另外nvcc --help会告诉你这个论点。

然后您可以 运行 nvcc 手动或尝试在 Makefile.

中进行一些更改

如果更改 Makefile,您可以很容易地发现有一个要设置的 HOST_COMPILER 变量。而darwin版本部分是这样的:

ifeq ($(TARGET_OS),darwin)
    ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $}' | cut -d'.' -f1` \>= 5),1)
        HOST_COMPILER ?= clang++
    endif

我想你可以把它改成你自己的 clang 路径。