weak-linking "libcuda.dylib" and "libcudart.dylib"

Hello,

I’ve been working on a library (currently a program) for image manipulation with codepaths for CPU/OpenGL/CUDA. So far, everything works nicely. The program must run even if CUDA is not installed (hence the CPU/OpenCL codepaths). So, I weak-link both libcuda.dylib and libcudart.dylib dynamic libraries… I’m using my own Makefile, and my link command is as follow:

ld -o my_program -arch i386 -flat_namespace -undefined suppress -macosx_version 10.6 -L/usr/local/cuda/lib -weak-lcuda -weak-lcudart

At runtime, I check if the libraries are present using dlopen(), and use the CUDA codepath whenever possible. If “libcuda.dylib” is not present (i.e. renamed to “libcuda.dy”), the program launches as expected, and (of course) dlopen(“libcuda.dylib”) fails. However, if “libcudart.dylib” is not present, dyld fails to launch the program, displaying the following error:

dyld: lazy symbol binding failed: Symbol not found: ___cudaRegisterFatBinary
Referenced from: /Users/raphael/Documents/Sources/aliomix/./bin/aliomix
Expected in: flat namespace

dyld: Symbol not found: ___cudaRegisterFatBinary
Referenced from: /Users/raphael/Documents/Sources/aliomix/./bin/aliomix
Expected in: flat namespace

I’ve tried everything I could think of (including adding a attribute((weak_import)) to __cudaRegisterFatBinary() in crt/host_runtime.h) without luck.

I also found a “USECUDADYNLIB := 1” commented line in the CUDA SDK deviceQueryDrv sample’s Makefile, which is supposed to enable weak linking (see in common.mk). Setting it makes the link fail…

As of today, I simply cannot provide a program with a CUDA codepath if the program it doesn’t launch when CUDA is not installed!!!

Has somebody succeeded in performing weak-linking with CUDA? Or, are static libs available somewhere?

I asked on Apple’s mailing lists a couple of days ago, but got no reply…

Thanks in advance for your replies,

Raphaël