Driver does not build correctly with Clang

On Ubuntu 16.04, I set the clang-4.0 compiler has been set as default for cc and c++ (using the update-alternatives command which replaces the /usr/bin/cc and /usr/bin/c++ symlinks).

With the system set up that way, I installed one of the nvidia drivers using apt. (Tried with multiple versions, 369, 390, others) It seems that during the installation process it compiles kernel modules.

Then the modules don’t seem to work proberly. I cannot login with lightdm (it does not launch the session and returns to the login screen.) “/var/log/lightdm/seat0-greeter.log” reports a “Resource temporarily unavailable (11)” error from the X11 driver.

“modprobe nvidia” also fails, and “dmesg” reports “nvidia: Unknown symbol count (err 0)”.

If gcc is selected as compiler, it seems to work correctly.

You will of course have to build the kernel using clang, too.
Here another user posted a howto:
https://devtalk.nvidia.com/default/topic/1031934/?comment=5250082

Out-of-tree kernel modules need to be compiled with the same compiler that was used to compile the kernel itself. It also needs to be the same version of that compiler.
If the kernel was compiled with gcc 7.3, nvidia kernel module should not be compiled with gcc 4.9, or gcc 5.3 or clang or any other C compiler. Anything else risks breaking binary compatibility.