Linking error while compiling opencv with cuda on high sierra

I am getting a linking error while compiling opencv with cuda on Mac OS X high Sierra (10.13.6).
The error does not appear without cuda.

Error message:

[ 21%] Linking CXX shared library ../../lib/libopencv_cudev.dylib
ld: warning: directory not found for option '-L/Users/unknownn/opencv/-Wl,-rpath,/usr/local/cuda'
ld: warning: directory not found for option '-L-Wl,-rpath,/usr/local/cuda'
ld: library not found for -llib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_cudev.4.0.1.dylib] Error 1
make[1]: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/all] Error 2
make: *** [all] Error 2

I have already tried pretty much every cuda (9, 9.2, 10) and opencv (3.4, 4.0.1) version. Also I am aware that every cuda version requires a specific Xcode version.

I have also tried various cmake versions

I checked for the correct cuda paths in ccmake.

I dont know why the ld warnings appear since /usr/local/cuda exist.

What is -llib?

Can you compile a CUDA-only program?
Can you compile whatever targets in the makefile, one by one, manually calling the compiling line?
On line #2, does this path exist, /Users/unknownn/, with double n (unknownn)?

yes I can compile CUDA-only programs.

yes, the problem appears while compiling opencv_cudev

make -f CMakeFiles/Makefile2 opencv_cudev

[100%] Built target ippiw
[100%] Linking CXX shared library ../../lib/libopencv_cudev.dylib
ld: warning: directory not found for option '-L/Users/unknownn/opencv/-Wl,-rpath,/usr/local/cuda'
ld: warning: directory not found for option '-L-Wl,-rpath,/usr/local/cuda'
ld: library not found for -llib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_cudev.4.0.1.dylib] Error 1
make[1]: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/all] Error 2
make: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/rule] Error 2

Yes, the extra n is ok.

Then I have no idea. :)

Thank you anyways.

I took a closer look at the output of cmake and found the line with extra dependencies to be a little strange.

Extra dependencies:          cudart_static lib nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda/lib -L-Wl,-rpath,/usr/local/cuda

I am not sure how “lib”, " -L/usr/local/cuda/lib" and “-L-Wl,-rpath,/usr/local/cuda” are getting in there.

Do they belong there?

I can’t say because I don’t really know what your program depends on.
These are almost, if not all of the NPP libs plus others we regularly use with CUDA.

Can you try compiling each target by hand, one by one, and see if any of them complains about this “lib” thing?

I also encountered same problem make opencv on high sierra 10.13.6

$ make -f CMakeFiles/Makefile2 opencv_cudev
[100%] Built target ippiw
[100%] Linking CXX shared library ../../lib/libopencv_cudev.dylib
ld: warning: directory not found for option '-L/Users/default/opencv/-Wl,-rpath,/usr/local/cuda'
ld: warning: directory not found for option '-L-Wl,-rpath,/usr/local/cuda'
ld: warning: text-based stub file /System/Library/Frameworks//OpenGL.framework/OpenGL.tbd and library file /System/Library/Frameworks//OpenGL.framework/OpenGL are out of sync. Falling back to library file for linking.
ld: warning: text-based stub file /System/Library/Frameworks//OpenGL.framework/OpenGL.tbd and library file /System/Library/Frameworks//OpenGL.framework/OpenGL are out of sync. Falling back to library file for linking.
ld: library not found for -llib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_cudev.4.0.0.dylib] Error 1
make[1]: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/all] Error 2
make: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/rule] Error 2

Hey guys,
I found a work around to this problem, and tried writing up a description of it, but the NVidia forum wouldn’t let me post it.

If you are interested, maybe ask a question on StackOverflow or something and link the question here, and I’ll try and write up the answer again there.

At a high-level, the problem seems to be with “FindCUDA.cmake” (and corresponding directory) that comes with CMake. There seems to be a bug in it (resulting in the weird “Extra dependencies” that Stefan observed). I basically replaced it (you should be able to find the relevant files under /usr/local/share/cmake/Modules) with the versions of the file that comes with the OpenCV source package (which should be under the “cmake” directory).

After doing this, I just went through the build process, and everything compiled successfully.

Hey, I want to know what exactly leads to this issue. jackgene can you post your insights on macos - Linking error while compiling opencv with cuda on high sierra - Stack Overflow

Hey Jack,

Thanks for the reply and for looking into the problem. I tried to follow your instructions. However, I have’t been successful. I copied all the FindCUDA related files from the cmake/Modules to the opencv/cmake/ directory and recompiled from Zero again … Didn’t work … :-(

cp -r /usr/local/share/cmake/Modules/FindCUDA* /Users/[USERNAME]/opencv/cmake/.

Than I looked around and found a Modules_CUDA_fix/ directory, deep within the pytorch build directory (which builds without problems and with CUDA functionality ← confirmed). But in terms of openCV, Still no success … ;-(

Hey Stefan,
It’s the other way around, you need to copy the FindCUDA to /usr/local/share/cmake/…

This was the confusing part for me too. But if you try running your cmake command with the “–trace” flag, you’ll see that it never uses the opencv FindCUDA.

Let me know if it works for you when you do this.

I’ve posted my answer there, with more details. Hopefully you’ll find it helpful.

Hi Jack,

Thank you so much for the answer. It finally compiled :-)