I am trying to use third-party libraries in LibraryLink, but the kernel crashes when I call their functions.
In particular, I want to make available some functions of SuiteSparse to manipulate SparseArrays in MMA. The library gets built, and the function loaded, but calling it crashes the kernel.
Here a minimal example:
Needs["CCompilerDriver`"]
CreateLibrary[
{NotebookDirectory[] <> "/libTest.c"}, "libTest",
"TargetDirectory" -> NotebookDirectory[],
"ShellCommandFunction" -> Function[Print[Style[#, Blue]]],
"ShellOutputFunction" -> Function[Print[Style[#, Red]]],
"CompileOptions" -> "-lcholmod -lspqr -lsuitesparseconfig"
]
fun = LibraryFunctionLoad[lib, "function", {}, Integer];
fun[] (* <-CRASHES KERNEL*)
And the library code (libTest.c)
#include "WolframLibrary.h"
#include "WolframSparseLibrary.h"
#include <stdint.h>
#include <stdio.h>
#include "SuiteSparseQR_C.h"
#include "SuiteSparse_config.h"
DLLEXPORT mint WolframLibrary_getVersion( ) {
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData) {
return LIBRARY_NO_ERROR;
}
DLLEXPORT void WolframLibrary_uninitialize(WolframLibraryData libData) {
return;
}
DLLEXPORT int function(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)
{
cholmod_common Common, *cc;
// start CHOLMOD
cc = &Common;
cholmod_l_start (cc); // CRASHES KERNEL
MArgument_setInteger(Res, 0);
return 0;
}
I am out of ideas. The kernel just crashes without any output. Any comment would be extremely useful just to have something to look at.
Important to say that those lines run if compiled and executed outside LibraryLink
standalone.c:
#include "SuiteSparseQR_C.h"
#include "SuiteSparse_config.h"
int main()
{
cholmod_common Common, *cc;
// start CHOLMOD
cc = &Common;
cholmod_l_start (cc); // CRASHES KERNEL
return 0;
}
gcc standalone.c -lcholmod -lspqr -lsuitesparseconfig -o standalone
./standalone