[CMake] Please support FindLAPACK/FindBLAS for C/C++-only projects

Arjen Markus arjen.markus at deltares.nl
Fri Jan 22 04:05:19 EST 2010


On 2010-01-21 18:27, Mark Moll wrote:
> On Jan 21, 2010, at 10:22 AM, Jed Brown wrote:
> 
>> On Thu, 21 Jan 2010 09:17:22 -0600, Mark Moll <mmoll at cs.rice.edu> wrote:
>>> You *can* use the Fortran BLAS/LAPACK libraries with C/C++ code.
>> Yes, this is actually very common, but check_fortran_function_exists
>> works by trying to link an actual Fortran program.  When calling from C,
>> you have to know how Fortran names are mangled which means testing a
>> number of common choices.  Nobody put this logic into these Find
>> modules, but it would be nice because many (most?) users of BLAS/Lapack
>> need to call it from both Fortran and C so they need to know how the
>> symbols are mangled.  CBLAS/CLapack are entirely different beasts, but I
>> know of far more C projects that call the Fortran versions directly than
>> use the C bindings.
> 
> 
> Wouldn’t it be possible to use “nm” to detect the Fortran name mangling? For example:
> 
> On OS X, default LAPACK library:
>> nm /usr/lib/liblapack.dylib | grep cheev$ | awk '{print $3}’
> _cheev
> 
> On Linux, using MKL:
>> nm /opt/intel/Compiler/11.1/056/mkl/lib/em64t/libmkl_intel_lp64.a | grep cheev$ | awk '{print $3}'
> cheev
> 
> (Of course the “grep” and “awk” commands can be replaced with some cmake string commands, if you want to make it a bit more portable.)
> 

This would indeed not work on systems that do not provide an nm-like
utility, but there is an system-independent way to do this:

Use a set of small C programs that will call a LAPACK routine with any
of the naming conventions:

int main() {
     _cheev( ... );
}

void stdcall__ CHEEV( ... );
int main() {
     CHEEV( ... );
}

etc.

Whichever of the programs can be successively linked (no unresolved
symbols) is the indication of the used name mangling scheme.

Caveats:
- Some Fortran compilers add two underscores when the name of the
   routine contains at least one underscore. So that variation must
   be tested too.
- String arguments pose an extra complication - the string length is
   usually added as a separate argument, but it may come at the end
   OR right after the string argument. This is not detectable by
   the above method.

Regards,

Arjen


More information about the CMake mailing list