3
$\begingroup$

I'm trying to generate a MathLink executable by CCompilerDriver package. The MathLink executable produced by Makefile works properly. However, when I'm trying to generate the same thing by CCompilerDriver, an executable can be generated and installed without any warnings. But it keeps crashing whenever I tried to use its functions. And the link closed automatically.

Any ideas on how to fix this issue?

Files cddmathlink.c, cddmathlink.tm, cddmlio.c, cddmlio.h, mathlink.h and the original Makefile can be found in
https://github.com/mcmtroffaes/cddlib/tree/master/src-mathlink2 .

One may also find the latest source file for cddlib through the link above.

I'm using OS X and I installed cddlib and GMP libraries via Homebrew. So I changed the Makefile according to my system and the installation of Mathematica, cddlib and GMP. The Makefile works and the resulting executable works properly too. But I don't know how to produce the executable via CCompilerDriver package.

The Makefile

MLINKDIR = /Applications/Mathematica.app/SystemFiles/Links/MathLink/DeveloperKit
SYS = MacOSX-x86-64
CADDSDIR = ${MLINKDIR}/${SYS}/CompilerAdditions
INCDIR = ${CADDSDIR}    
LIBDIR = ${CADDSDIR}
MPREP = ${CADDSDIR}/mprep
RM = rm
EXTRA_LIBS = -stdlib=libstdc++ -lstdc++ -framework Foundation
MATHLINK_LIB = -lMLi4
CC = /usr/bin/clang

BINARIES = cddmlgmp

all : $(BINARIES)

cddmlgmp : cddmathlinktm.o cddmathlink.o cddmlio.o
    ${CC} -I${INCDIR} cddmlio.o cddmathlinktm.o cddmathlink.o -L${LIBDIR} ${MATHLINK_LIB} ${EXTRA_LIBS} -lgmp /usr/local/lib/libcddgmp.a -o cddmlgmp

cddmathlinktm.o: cddmathlinktm.c
    $(CC) -c cddmathlinktm.c

cddmathlink.o: cddmathlink.c
    $(CC) -c cddmathlink.c -o cddmathlink.o

cddmlio.o: cddmlio.c
    $(CC) -DGMPRATIONAL -c cddmlio.c -o cddmlio.o

cddmathlinktm.c : cddmathlink.tm
    ${MPREP} $? > $@

clean:
    ${RM} -rf *.o *tm.c $(BINARIES)

First attempt (not working):

Needs["CCompilerDriver`"];
dir = FileNameJoin[{"~/", "OneDrive", "Documents", "Bitbucket", "cddlib", "src-mathlink"}];
src = FileNames[{"cddmathlink.*", "cddmlio.c"}, dir, Infinity];
CreateExecutable[src, "cddmlgmp", "Libraries" -> {"/usr/local/lib/libgmp.a", "/usr/local/lib/libcddgmp.a"}, "Defines" -> "GMPRATIONAL", "ShellOutputFunction" :> Print, "ShellCommandFunction" :> Print, "TargetDirectory" -> dir]

Another attempt (not working either):

Needs["CCompilerDriver`"];
dir = FileNameJoin[{"~/", "OneDrive", "Documents", "Bitbucket", "cddlib", "src-mathlink"}];
src = FileNames["cddmathlink.*", dir, Infinity];
srcext = FileNames["cddmlio.c",dir,Infinity];
obj = CreateObjectFile[srcext, "cddmlio", "Defines" -> "GMPRATIONAL", "ShellOutputFunction" :> Print, "ShellCommandFunction" :> Print, "TargetDirectory" -> dir];
CreateExecutable[src, "cddmlgmp", "Libraries" -> {"/usr/local/lib/libcddgmp.a", "/usr/local/lib/libgmp.a"}, "ExtraObjectFiles" -> obj, "ShellOutputFunction" :> Print, "ShellCommandFunction" :> Print, "TargetDirectory" -> dir]
$\endgroup$
  • $\begingroup$ Please post the code or a substitute that demonstrates the problem. Otherwise, it is very hard to say what might be going on here. $\endgroup$ – Oleksandr R. Apr 27 '15 at 13:38
  • $\begingroup$ @Oleksandr R.: May I ask what else are needed? Are those *.c, *.tm and *io.c files useful here? Or you mean the compilation output? $\endgroup$ – Han Xiao Apr 27 '15 at 14:34
  • $\begingroup$ Well, without the ability to generate or examine the executable, it is really not possible to guess what is wrong with it based on your description. It sounds like the executable is crashing, but who knows what the reason for that may be given what you say here. I personally cannot help you anyway as I don't have a Mac, but the question should contain sufficient information for someone else to be able to reproduce the problem. Otherwise you are depending on someone else having encountered this specific problem before in their own work to know how to answer you. $\endgroup$ – Oleksandr R. Apr 27 '15 at 17:42
  • $\begingroup$ @OleksandrR.: Thank you so much for your suggestions. I will edit my question to make it reproducible for someone who is interested. $\endgroup$ – Han Xiao Apr 28 '15 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.