I don’t know if this fixes the issue, but you’re not using the LDFLAGS variable after you set it.
Also, the script that you show here is different from what it’s now on GitHub, for example there you don’t create the $prefix/lib directory (which leads to an error when you try to compile and put the output there).