dlibサンプルのコンパイルで  cuDNN V5.0 OR GREATER NOT FOUND と言われた時

環境: Max OS X El Capitan
dlib: v19.2

## dlib サンプルブログラム ビルド失敗

[コンパイル方法](http://dlib.net/compile.html)に従って、dlibのサンプルブログラムをビルドしたところ、以下のようなエラーが出ました。

```
% cmake ..
-- C++11 activated.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Looking for png_create_read_struct
-- Looking for png_create_read_struct - found
-- Looking for jpeg_read_header
-- Looking for jpeg_read_header - found
-- Searching for BLAS and LAPACK
-- Found PkgConfig: /usr/local/bin/pkg-config (found version "0.29.1")
-- Checking for module 'cblas'
--   No package 'cblas' found
-- Checking for module 'lapack'
--   No package 'lapack' found
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void*
-- Check size of void* - done
-- Found LAPACK library
-- Found CBLAS library
-- Looking for cblas_ddot
-- Looking for cblas_ddot - found
-- Found CUDA: /Developer/NVIDIA/CUDA-8.0 (found suitable version "8.0", minimum required is "7.5")
-- Looking for cuDNN install...
-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- Checking if you have the right version of cuDNN installed.
-- *** Found cuDNN, but it looks like the wrong version so dlib will not use it. ***
-- *** cuDNN V5.0 OR GREATER NOT FOUND.  DLIB WILL NOT USE CUDA. ***
-- *** If you have cuDNN then set CMAKE_PREFIX_PATH to include cuDNN's folder.
OpenCV not found, so we won't build the webcam_face_pose_ex example.
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mkisono/work/dlib/examples/build
```

CMAKE_PREFIX_PATHを付けても結果は同じでした。

```
% cmake -DCMAKE_PREFIX_PATH=/Developer/NVIDIA/CUDA-8.0  .. 
```

同じマシンで、Theanoなど他のツールは cuDNN が使えています。cuDNNのインストール自体は問題無いと思われたので、原因を調べてみました。

## cudnn_test をビルドして原因を探る

"cuDNN V5.0 OR GREATER NOT FOUND"と出てしまうのは、cuDNNを含む[テストプロジェクトのビルドに失敗しているから](https://github.com/davisking/dlib/issues/289)です。どんなエラーが出ているのでしょうか？

```
% mkdir build
% cd build 
% cmake ../dlib/cmake_utils/test_for_cudnn
% cmake --build .
Scanning dependencies of target cudnn_test
[ 50%] Building CXX object CMakeFiles/cudnn_test.dir/Users/mkisono/work/dlib/dlib/dnn/cudnn_dlibapi.cpp.o
/Users/mkisono/work/dlib/dlib/dnn/cudnn_dlibapi.cpp:117:13: error: thread-local storage is not supported for the current target
            thread_local cudnn_context c;
            ^
/Users/mkisono/work/dlib/dlib/dnn/cudnn_dlibapi.cpp:156:13: error: thread-local storage is not supported for the current target
            thread_local cudnn_activation_descriptor des(CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN,0);
            ^
/Users/mkisono/work/dlib/dlib/dnn/cudnn_dlibapi.cpp:162:13: error: thread-local storage is not supported for the current target
            thread_local cudnn_activation_descriptor des(CUDNN_ACTIVATION_SIGMOID, CUDNN_PROPAGATE_NAN,0);
            ^
/Users/mkisono/work/dlib/dlib/dnn/cudnn_dlibapi.cpp:168:13: error: thread-local storage is not supported for the current target
            thread_local cudnn_activation_descriptor des(CUDNN_ACTIVATION_TANH, CUDNN_PROPAGATE_NAN,0);
            ^
4 errors generated.
make[2]: *** [CMakeFiles/cudnn_test.dir/Users/mkisono/work/dlib/dlib/dnn/cudnn_dlibapi.cpp.o] Error 1
make[1]: *** [CMakeFiles/cudnn_test.dir/all] Error 2
make: *** [all] Error 2
```

## Xcodeをバージョンアップ

"error: thread-local storage is not supported for the current target" エラーの原因は、Xcodeがバージョン7.3だったせいでした。最新版にバージョンアップし、再度 test_for_cudnn をビルドしたところ、今度はうまく行きました。

![スクリーンショット 2017-01-17 9.39.45.png](https://qiita-image-store.s3.amazonaws.com/0/42252/e6f1368f-7c37-8a11-90ee-f584d981c207.png)

```
% cmake --build .
[ 50%] Building CXX object CMakeFiles/cudnn_test.dir/Users/mkisono/work/dlib/dlib/dnn/cudnn_dlibapi.cpp.o
[100%] Linking CXX static library libcudnn_test.a
[100%] Built target cudnn_test
```

## 今度は cuda_test がビルド出来ず

これで終わりじゃなかった。今度は CUDAのtest projectがビルド出来ない状態になっていた。何なんだ、この堂々巡りは・・

```
-- Found CUDA: /Developer/NVIDIA/CUDA-8.0 (found suitable version "8.0", minimum required is "7.5")
-- Looking for cuDNN install...
-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- *** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA. ***
-- *** cuDNN V5.0 OR GREATER NOT FOUND.  DLIB WILL NOT USE CUDA. ***
-- *** If you have cuDNN then set CMAKE_PREFIX_PATH to include cuDNN's folder.
```
CUDA toolkitが古いと、[Xcode 8がサポートされていない](https://github.com/arrayfire/arrayfire/issues/1384)らしい。仕方ないので今度はCUDA toolkitを新しくする。

エラーが出た時のバージョン。

```
% nvcc -V                                                                                                     [~/work/dlib/build]
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sun_Sep_18_22:16:08_CDT_2016
Cuda compilation tools, release 8.0, V8.0.46
```

アップデート後のバージョン。

```
% nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sun_Oct_30_22:18:43_CDT_2016
Cuda compilation tools, release 8.0, V8.0.54
```

## everything works fine, at last.

cuda_testプロジェクトのビルド成功

```
% cmake --build .                                                                                             [~/work/dlib/build]
[ 50%] Building NVCC (Device) object CMakeFiles/cuda_test.dir/cuda_test_generated_cuda_test.cu.o
Scanning dependencies of target cuda_test
[100%] Linking CXX static library libcuda_test.a
[100%] Built target cuda_test
```

そして、ようやくサンプルのcmakeが通った。

```
% cmake ..
-- C++11 activated.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Looking for png_create_read_struct
-- Looking for png_create_read_struct - found
-- Looking for jpeg_read_header
-- Looking for jpeg_read_header - found
-- Searching for BLAS and LAPACK
-- Found PkgConfig: /usr/local/bin/pkg-config (found version "0.29.1")
-- Checking for module 'cblas'
--   No package 'cblas' found
-- Checking for module 'lapack'
--   No package 'lapack' found
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void*
-- Check size of void* - done
-- Found LAPACK library
-- Found CBLAS library
-- Looking for cblas_ddot
-- Looking for cblas_ddot - found
-- Found CUDA: /Developer/NVIDIA/CUDA-8.0 (found suitable version "8.0", minimum required is "7.5")
-- Looking for cuDNN install...
-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- Checking if you have the right version of cuDNN installed.
-- Found cuDNN: /usr/local/cuda/lib/libcudnn.dylib
OpenCV not found, so we won't build the webcam_face_pose_ex example.
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mkisono/work/dlib/examples/build
```

## まとめ

開発ツールは、まめにアップデートした方が良さそうですね。