Cross-platform build using nvml

I am trying to understand the interaction between the installed with the driver and the stub version. I added some nvml function calls to a c++ program. The linker failed to find these functions. I then added -lnvidia-ml to the build command. This produced an error saying that library could not be found. I then added -L/usr/local/cuda/lib64/stubs to the command. That linked successfully.

However, when I run ldd on the resulting binary, it lists => /usr/lib/nvidia-396/ (0x00007f93a8700000)

which is the library installed with the driver.

This all seems to work OK, but I am wondering how and where the driver version is being detected, I would like to write code to optionally load the so at runtime, using dlopen. I have noticed that the number in the driver library (396 above) varies depending on the installed driver version. So, I am wondering, was that library found and resolved at link time, or did ldd go find it? In either case, how can I decide at runtime where to look for the so?