Cannot link MathLink libraries

Hi,

I’m trying to link some C and Fortran files that I need to use via MathLink.
As I try to link the libraries to these file I got errors.
Here I write down the command line I use in my makefile and the errors I got

MAKEFILE:

CXX = c++

MLINKDIR = /usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions

LINK_FLAGS = -lML64i3 -lgfortran

OBJS = fpdf.o CT12Pdf.o

pdf : $(OBJS) pdf.c
	$(CXX) -o $@ -I$(MLINKDIR) -L$(MLINKDIR) $(LINK_FLAGS) $(OBJS) pdf.c

This are some examples of the errors I got:

CT12Pdf.o: In function `readpds0_':
CT12Pdf.f:(.text+0x21b): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x232): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x23f): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x28f): undefined reference to `_gfortran_st_read'
.
.
.
/tmp/ccib2uwm.o: In function `_tr0(MLink*)':
pdf.c:(.text+0x84): undefined reference to `MLGetInteger'
pdf.c:(.text+0xa5): undefined reference to `MLGetReal'
pdf.c:(.text+0xc6): undefined reference to `MLGetReal'
pdf.c:(.text+0xe0): undefined reference to `MLNewPacket'
pdf.c:(.text+0x13c): undefined reference to `MLPutFunction'
pdf.c:(.text+0x157): undefined reference to `MLPutReal'
/tmp/ccib2uwm.o: In function `MLInstall':
pdf.c:(.text+0x181): undefined reference to `MLConnect'
pdf.c:(.text+0x1bf): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x1d4): undefined reference to `MLFlush'
/tmp/ccib2uwm.o: In function `_definepattern(MLink*, char*, char*, int)':
pdf.c:(.text+0x22d): undefined reference to `MLPutFunction'
pdf.c:(.text+0x240): undefined reference to `MLPutString'
.
.
.
.
collect2: error: ld returned 1 exit status
make: *** [pdf] Error 1

I think the issue is about properly linking the libraries of gfortran and MathLink, but I can’t find how to do it in the right way.

I can’t quite tell from the information you have posted what might be going exactly wrong. Could you post all of your Makefile. For example, by the _tr0(MLINK) function reference I see that you are using mprep to generate code from MathLink template files. It would help me answer your question if I could get a more complete picture of what you are trying to build.

Also, on Linux you need to add the following libraries to your LINK_FLAGS variable: -lrt -lpthread -lm.

Steve

Thank you Steve for your help,
I tried adding the LINK_FLAGS you suggested but I got the same errors.

Here I attach the whole Makefile:

F77 =  gfortran
FLAGS = -c -O3

CXX = c++

MLINKDIR = /usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions

LINK_FLAGS = -lML64i3 -lgfortran -lrt -lpthread -lm

OBJS = fpdf.o CT12Pdf.o


pdf : $(OBJS) pdf.c
	$(CXX) -o $@ -I$(MLINKDIR) -L$(MLINKDIR) $(LINK_FLAGS) $(OBJS) pdf.c

pdf.c : pdf.tm
	./mprep $? -o $@

.f.o: ; $(F77) $(FLAGS)  $*.f


clean :
	$(RM) $(OBJS) pdf.c

I think the other lines works properly because I can get the output files “fpdf.o”, “CT12Pdf.o” and “pdf.c”
Here I attach the full list of errors I get when launching the “make” command

gfortran -c -O3  fpdf.f
gfortran -c -O3  CT12Pdf.f
./mprep pdf.tm -o pdf.c
c++ -o pdf -I/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -L/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -lML64i3 -lrt -lpthread -lm -lgfortran fpdf.o CT12Pdf.o pdf.c
CT12Pdf.o: In function `readpds0_':
CT12Pdf.f:(.text+0x21b): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x232): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x23f): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x28f): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x2a6): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x2b3): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x2cc): undefined reference to `_gfortran_compare_string'
CT12Pdf.f:(.text+0x31c): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x333): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x34a): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x361): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x378): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x38d): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x3b1): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x412): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x429): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x436): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x44f): undefined reference to `_gfortran_compare_string'
CT12Pdf.f:(.text+0x49f): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x4b6): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x4cd): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x4e2): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x4f7): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x50c): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x523): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x53a): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x547): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x5a4): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x5bb): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x5d2): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x5e9): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x5fe): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x627): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x699): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x6b0): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x6bd): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x6f6): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x70d): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x724): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x73b): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0x752): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x769): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x77e): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x78b): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x7db): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x7f2): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x7ff): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x838): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x84f): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x866): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x87b): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x892): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x8a7): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0x8b4): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x91b): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x943): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x95a): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0x9aa): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0x9c1): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0x9ce): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0xa14): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0xa2b): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xa42): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xa83): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xa9c): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xab5): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xad1): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0xbb5): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0xbcc): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0xbd9): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0xc12): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0xc29): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xc40): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xc7a): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xc91): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0xd16): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0xd2d): undefined reference to `_gfortran_transfer_character'
CT12Pdf.f:(.text+0xd3a): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0xd88): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0xdc2): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xdd9): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0xe19): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0xe30): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xe47): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xe8f): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xea8): undefined reference to `_gfortran_transfer_real'
CT12Pdf.f:(.text+0xefa): undefined reference to `_gfortran_st_read'
CT12Pdf.f:(.text+0xf0f): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0xf24): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0xf39): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0xf50): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0xf67): undefined reference to `_gfortran_transfer_integer'
CT12Pdf.f:(.text+0xf74): undefined reference to `_gfortran_st_read_done'
CT12Pdf.f:(.text+0xfb3): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0xfca): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0xfd7): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x1011): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x1028): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x103f): undefined reference to `_gfortran_transfer_real_write'
CT12Pdf.f:(.text+0x1056): undefined reference to `_gfortran_transfer_real_write'
CT12Pdf.f:(.text+0x1063): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x106c): undefined reference to `_gfortran_stop_string'
CT12Pdf.o: In function `nextun_':
CT12Pdf.f:(.text+0x10b3): undefined reference to `_gfortran_st_inquire'
CT12Pdf.f:(.text+0x10d4): undefined reference to `_gfortran_stop_string'
CT12Pdf.o: In function `partonx12_':
CT12Pdf.f:(.text+0x1b87): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x1b9b): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1baf): undefined reference to `_gfortran_transfer_real_write'
CT12Pdf.f:(.text+0x1bb9): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x1bc2): undefined reference to `_gfortran_stop_string'
CT12Pdf.f:(.text+0x1c01): undefined reference to `_gfortran_st_write'
CT12Pdf.o: In function `ct12pdf_':
CT12Pdf.f:(.text+0x1c7a): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x1c8e): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1ca0): undefined reference to `_gfortran_transfer_real_write'
CT12Pdf.f:(.text+0x1caa): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x1d34): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x1d48): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1d52): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x1d7d): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x1d91): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1da3): undefined reference to `_gfortran_transfer_integer_write'
CT12Pdf.f:(.text+0x1db7): undefined reference to `_gfortran_transfer_integer_write'
CT12Pdf.f:(.text+0x1dc1): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x1df4): undefined reference to `_gfortran_stop_string'
CT12Pdf.f:(.text+0x1e1f): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x1e33): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1e45): undefined reference to `_gfortran_transfer_real_write'
CT12Pdf.f:(.text+0x1e4f): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x1e58): undefined reference to `_gfortran_stop_string'
CT12Pdf.o: In function `setct12_':
CT12Pdf.f:(.text+0x1eaf): undefined reference to `_gfortran_st_inquire'
CT12Pdf.f:(.text+0x1f01): undefined reference to `_gfortran_st_open'
CT12Pdf.f:(.text+0x1f44): undefined reference to `_gfortran_st_close'
CT12Pdf.f:(.text+0x1f71): undefined reference to `_gfortran_stop_string'
CT12Pdf.f:(.text+0x1f9c): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x1fb0): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1fc2): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1fd6): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x1fe0): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x1fe9): undefined reference to `_gfortran_stop_string'
CT12Pdf.o: In function `ct12alphas_':
CT12Pdf.f:(.text+0x215a): undefined reference to `_gfortran_stop_string'
CT12Pdf.f:(.text+0x2182): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x218a): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x21b2): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x21c4): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x21cc): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x21f4): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x2206): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x220e): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x2236): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x2248): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x2250): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x2278): undefined reference to `_gfortran_st_write'
CT12Pdf.f:(.text+0x228a): undefined reference to `_gfortran_transfer_character_write'
CT12Pdf.f:(.text+0x2292): undefined reference to `_gfortran_st_write_done'
CT12Pdf.f:(.text+0x229b): undefined reference to `_gfortran_stop_string'
CT12Pdf.o: In function `ct12mass_':
CT12Pdf.f:(.text+0x22c2): undefined reference to `_gfortran_stop_string'
/tmp/ccaI54W2.o: In function `_tr0(MLink*)':
pdf.c:(.text+0x84): undefined reference to `MLGetInteger'
pdf.c:(.text+0xa5): undefined reference to `MLGetReal'
pdf.c:(.text+0xc6): undefined reference to `MLGetReal'
pdf.c:(.text+0xe0): undefined reference to `MLNewPacket'
pdf.c:(.text+0x13c): undefined reference to `MLPutFunction'
pdf.c:(.text+0x157): undefined reference to `MLPutReal'
/tmp/ccaI54W2.o: In function `MLInstall':
pdf.c:(.text+0x181): undefined reference to `MLConnect'
pdf.c:(.text+0x1bf): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x1d4): undefined reference to `MLFlush'
/tmp/ccaI54W2.o: In function `_definepattern(MLink*, char*, char*, int)':
pdf.c:(.text+0x22d): undefined reference to `MLPutFunction'
pdf.c:(.text+0x240): undefined reference to `MLPutString'
pdf.c:(.text+0x253): undefined reference to `MLPutString'
pdf.c:(.text+0x264): undefined reference to `MLPutInteger'
pdf.c:(.text+0x270): undefined reference to `MLError'
/tmp/ccaI54W2.o: In function `_MLDoCallPacket(MLink*, func*, int)':
pdf.c:(.text+0x2a7): undefined reference to `MLGetInteger'
pdf.c:(.text+0x30f): undefined reference to `MLCheckFunction'
pdf.c:(.text+0x390): undefined reference to `MLClearError'
pdf.c:(.text+0x3a5): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x3cd): undefined reference to `MLEndPacket'
pdf.c:(.text+0x3dd): undefined reference to `MLNewPacket'
/tmp/ccaI54W2.o: In function `MLAnswer':
pdf.c:(.text+0x44d): undefined reference to `MLError'
pdf.c:(.text+0x45d): undefined reference to `MLNextPacket'
/tmp/ccaI54W2.o: In function `refuse_to_be_a_frontend(MLink*)':
pdf.c:(.text+0x4ad): undefined reference to `MLPutFunction'
pdf.c:(.text+0x4c3): undefined reference to `MLPutFunction'
pdf.c:(.text+0x4d9): undefined reference to `MLPutFunction'
pdf.c:(.text+0x4ef): undefined reference to `MLPutFunction'
pdf.c:(.text+0x500): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x511): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x527): undefined reference to `MLPutFunction'
pdf.c:(.text+0x53d): undefined reference to `MLPutFunction'
pdf.c:(.text+0x54e): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x561): undefined reference to `MLTransferExpression'
pdf.c:(.text+0x577): undefined reference to `MLPutFunction'
pdf.c:(.text+0x58d): undefined reference to `MLPutFunction'
pdf.c:(.text+0x59e): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x5af): undefined reference to `MLPutString'
pdf.c:(.text+0x5c0): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x5d1): undefined reference to `MLPutSymbol'
pdf.c:(.text+0x5dd): undefined reference to `MLEndPacket'
pdf.c:(.text+0x5eb): undefined reference to `MLNewPacket'
pdf.c:(.text+0x5f7): undefined reference to `MLNextPacket'
pdf.c:(.text+0x622): undefined reference to `MLNewPacket'
pdf.c:(.text+0x62e): undefined reference to `MLError'
/tmp/ccaI54W2.o: In function `MLEvaluate':
pdf.c:(.text+0x66f): undefined reference to `MLPutFunction'
pdf.c:(.text+0x689): undefined reference to `MLPutFunction'
pdf.c:(.text+0x6a0): undefined reference to `MLPutString'
pdf.c:(.text+0x6b0): undefined reference to `MLEndPacket'
/tmp/ccaI54W2.o: In function `MLEvaluateString':
pdf.c:(.text+0x710): undefined reference to `MLNewPacket'
pdf.c:(.text+0x747): undefined reference to `MLNewPacket'
pdf.c:(.text+0x753): undefined reference to `MLError'
/tmp/ccaI54W2.o: In function `_MLMain(char**, char**, char*)':
pdf.c:(.text+0x7c1): undefined reference to `MLInitialize'
pdf.c:(.text+0x811): undefined reference to `MLOpenString'
pdf.c:(.text+0x83e): undefined reference to `MLOpenArgcArgv'
pdf.c:(.text+0x861): undefined reference to `MLErrorString'
pdf.c:(.text+0x876): undefined reference to `MLAlert'
pdf.c:(.text+0x89d): undefined reference to `MLSetYieldFunction'
pdf.c:(.text+0x8bf): undefined reference to `MLSetMessageHandler'
pdf.c:(.text+0x910): undefined reference to `MLClose'
pdf.c:(.text+0x91f): undefined reference to `MLDeinitialize'
collect2: error: ld returned 1 exit status
make: *** [pdf] Error 1

I would try the following, change:

pdf : (OBJS) pdf.c (CXX) -o @ -I(MLINKDIR) -L$(MLINKDIR) (LINK\_FLAGS) (OBJS) pdf.c

to

pdf : (OBJS) pdf.c (CXX) -o @ -I(MLINKDIR) -L$(MLINKDIR) pdf.c (OBJS) (LINK_FLAGS)

The linker on Linux (depending on your distribution/version) can be a little picky about the order in which it searches for symbols in libraries and object files.

If that doesn’t work, you can try this:

pdf : (OBJS) pdf.c (CXX) -o @ -I(MLINKDIR) -L$(MLINKDIR) pdf.c -Wl,--start-group (OBJS) (LINK_FLAGS) -Wl,--end-group

The -Wl,--start-group/-Wl,--end-group flags get passed to the linker and instruct the linker to continue searching through the libraries and object files until it can fully resolve missing symbols. By default the linker only makes one pass through the libraries and object files to find symbols and because of the timing of when it searches for a symbol it may miss a symbol that is actually defined in the object file or library.

Hi Steve,
I tried with the changes you suggested:
now libraries from gfortran are properly linked I guess, but I still have some troubles with the “libML64i3.so”
Here it is what I get now:

gfortran -c -O3  fpdf.f
gfortran -c -O3  CT12Pdf.f
./mprep pdf.tm -o pdf.c
c++ -o pdf -I/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -L/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions pdf.c fpdf.o CT12Pdf.o -lML64i3 -lgfortran -lrt -lpthread -lm
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_unlink'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `pthread_create'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_open'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_wait'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_init'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_destroy'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_close'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_post'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_trywait'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `pthread_sigmask'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `shm_open'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `pthread_join'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `shm_unlink'
collect2: error: ld returned 1 exit status
make: *** [pdf] Error 1

The same happens when using the commands “-Wl,--start-group … -Wl,--end-group”

Those symbols that are listed as undefined are in librt and libpthread.

Try switching the order of the libraries as follows:

-lML64i3 -lrt -lpthread -lm

With this change I get the same list of errors:

gfortran -c -O3  fpdf.f
gfortran -c -O3  CT12Pdf.f
./mprep pdf.tm -o pdf.c
c++ -o pdf -I/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -L/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions pdf.c fpdf.o CT12Pdf.o -lML64i3 -lrt -lpthread -lm -lgfortran
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_unlink'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `pthread_create'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_open'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_wait'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_init'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_destroy'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_close'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_post'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `sem_trywait'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `pthread_sigmask'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `shm_open'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `pthread_join'
/usr/local/Wolfram/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to `shm_unlink'
collect2: error: ld returned 1 exit status
make: *** [pdf] Error 1

What distribution of Linux are you using and what version of that distribution?

I have UBUNTU 13.10
64-bit

My apologies for the delay. I had to get a 13.10 machine up and running and Mathematica 9 installed. Add the linker flag -Wl,--no-as-needed to your link flags and it should resolve the problem.

Steve

Thank you very much Steve for your hard work,

finally it works properly, and I can compile my files.

Your help have been fundamental!

Thanks again!

Hi, I have a similar problem with fortran libraries and MathLink, even using your advice to include
-Wl,–start-group … -Wl,–end-group -Wl,–no-as-needed.

The makefile (in the context of FormCalc-8.5) contains

mcc $(MCFLAGS) -o $@ $*.tm $*.o $(LIBS) $(STDLIBS) $(LDFLAGS) $(MCLIBS)

As a modification I tried

  mcc $(MCFLAGS) -o $@ $*.tm $*.o -Wl,--start-group $(LIBS) $(STDLIBS) $(LDFLAGS) $(MCLIBS) -Wl,--end-group -Wl,--no-as-needed

This generates

mcc -b64 -o run run.tm run.o -Wl,--start-group  renconst.a squaredme.a util.a ../../../../FeynHiggs-2.10.2/x86_64-Linux/lib64/libFH.a ../../../LoopTools/x86_64-Linux/lib64/libooptools.a  -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. -lgfortran -lm -lgcc_s -lgcc -lquadmath -lm -lgcc_s -lgcc -lgcc_s -lgcc -lpthread -Wl,--end-group -Wl,--no-as-needed

However, the output of make contains many undefined references to _gfortran:

run.tm.c: + exec gcc -O3 -g -march=native -std=gnu99 -fomit-frame-pointer -ffast-math -m64 -Iutil//cuba/common -I. -DHAVE_CONFIG_H -I. -I./C -I. -I. -I../../../LoopTools/x86_64-Linux/lib64/../include -I../../../LoopTools/x86_64-Linux/lib64 -I../../../../FeynHiggs-2.10.2/x86_64-Linux/lib64/../include -I../../../../FeynHiggs-2.10.2/x86_64-Linux/lib64 -Irenconst/ -I/usr/local/Wolfram/Mathematica/10.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -m64 -o run.tm.o run.tm.c -c
+ exec g++ -m64 -I/usr/local/Wolfram/Mathematica/10.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions -m64 -o run run.tm.o run.o renconst.a squaredme.a util.a ../../../../FeynHiggs-2.10.2/x86_64-Linux/lib64/libFH.a ../../../LoopTools/x86_64-Linux/lib64/libooptools.a /usr/local/Wolfram/Mathematica/10.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i4.so -L/usr/local/Wolfram/Mathematica/10.0/SystemFiles/Libraries/Linux-x86-64/ -lm -lpthread -lrt -ldl -luuid
run.o: In function `cuba_':
/./F/xsection.F:681: undefined reference to `_gfortran_st_write'
/./F/xsection.F:681: undefined reference to `_gfortran_transfer_character_write'
/./F/xsection.F:681: undefined reference to `_gfortran_st_write_done'
/./F/xsection.F:696: undefined reference to `_gfortran_st_write'
/./F/xsection.F:696: undefined reference to `_gfortran_transfer_character_write'
/./F/xsection.F:696: undefined reference to `_gfortran_transfer_integer_write'
/./F/xsection.F:696: undefined reference to `_gfortran_st_write_done'
    ...
   /LoopTools-2.12/build/ffdcxs.F:502: undefined reference to `_gfortran_st_write_done'
   /LoopTools-2.12/build/ffdcxs.F:503: undefined reference to `_gfortran_stop_string'
    collect2: error: ld returned 1 exit status

I use Kubuntu-14.04.2, Mathematica 10.0 and gfortran.
Do you have any suggestion how to fix this? Thanks a lot in advance!