semi-newbie | static compilation/linking gfortran

Evan Cooch evan.cooch@gmail.com
Wed Jan 20 14:27:00 GMT 2016


Greetings --

I have a large numerical (statistical) program (which I've collaed 
'solver') that I compile for distribution -- compilation is done on my 
RHEL 6.xx GNU/Linux machines, using gfortran v 4.8.2 20140102 (Red Hat 
4.8.2-15)

I've had zero issues doing dynamic compilations for the executable, but 
more and more users are complaining about the executable not running on 
their particular machines (which invariably are using a distro different 
than mine) because of a missing (or misplaced) library or other. So, I'm 
considering static linking (which I last did *years* ago) -- my 
assumption is to statically glob everything together into the executable 
-- larger executable size, but more portable, since all the libs come 
with the executable.

However, my initial attempts haven't met with much success -- I suspect 
I'm missing something obvious, so I'm hoping someone can point out my 
obvious mistakes.

For compiling the *dynamically* linked executable, I use the following 
makefile (edited from the full makefile, since the one I actually use is 
pretty long). Compilation runs fine, executable works perfectly.

COMPILER = gfortran
LINKER = gfortran
COPTIONS =  -c  -fimplicit-none -fbounds-check -m64 -funroll-loops 
-ftree-vectorize  -O2
OBJECTS = solver.o btest.o cvtcas.o error.o fillsg.o forset.o getcmd.o 
oindex.o \
ibclr.o ibset.o ivalue.o mindex.o glabrd.o dater.o copytp.o numerr.o 
prtcmd.o \
<about 30 more deleted from the list just to save some space in this 
post...>
dmfuncs.o hierparm.o ranuni.o
solver: solver.f90 $(OBJECTS)  status.mod hyperdist.mod multistrata.mod 
profileci.mod linkfuncs.mod datamodule.mod dmfuncs.mod hierparm.mod 
ranuni.mod
         $(LINKER) -fopenmp *.o Linpack.a -static-libgfortran -o solver.64
solver.o: solver.f90 status.mod datamodule.mod
         $(COMPILER) $(COPTIONS) -fopenmp solver.f90
status.mod: status.f90
         $(COMPILER) $(COPTIONS) status.f90
status.o: status.f90
         $(COMPILER) $(COPTIONS) status.f90
hyperdist.mod: hyperdist.f90
         $(COMPILER) $(COPTIONS) hyperdist.f90
hyperdist.o: hyperdist.f90
         $(COMPILER) $(COPTIONS) hyperdist.f90
<rest of the list skipped to save space>


Now,to compile a statically linked version, my attempts (so far) have 
basically involved tweaking the makefile, adding -static as compiler 
option, and changing all the references to *.o in the makefile to *.a. 
My attempt is copied at the bottom of this note.

Alas, said attempt fails during compilation -- executable is not 
created. The compilation terminates with the following error message 
that I'm not able to decipher:

gfortran -fopenmp *.a Linpack.a -static-libgfortran -o solver.64.static
/usr/lib/../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

Apologies in advance if the problem/solution is obvious. I spend more of 
my time on what the program does than on the mechanics of compiling it, 
and I suspect it shows. ;-)


----<attempt at static-linking makefile below>----

COMPILER = gfortran
LINKER = gfortran
COPTIONS =  -static -c  -fimplicit-none -fbounds-check -m64 
-funroll-loops -ftree-vectorize  -O2
OBJECTS = mark.a btest.a cvtcas.a error.a fillsg.a forset.a getcmd.a 
oindex.a \
ibclr.a ibset.a ivalue.a mindex.a glabrd.a dater.a copytp.a numerr.a 
prtcmd.a \
lmdex.a forst2.a inipar.a cdtr.a ndtr.a ascan.a title.a value.a warn.a 
warnum.a initpar.a \
xmatrx.a tmread.a rlabrd.a blabrd.a valnum.a matprt.a setlink.a \
<bunch of stuff deleted to save space>
dmfuncs.a hierparm.a ranuni.o
solver: solver.f90 $(OBJECTS)  status.mod hyperdist.mod multistrata.mod 
profileci.mod linkfuncs.mod datamodule.mod dmfuncs.mod hierparm.mod 
ranuni.mod
	$(LINKER) -fopenmp *.a Linpack.a -static-libgfortran -o solver.64.static
solver.a: mark.f90 status.mod datamodule.mod
	$(COMPILER) $(COPTIONS) -fopenmp solver.f90
status.mod: status.f90
	$(COMPILER) $(COPTIONS) status.f90
status.a: status.f90
	$(COMPILER) $(COPTIONS) status.f90
hyperdist.mod: hyperdist.f90
	$(COMPILER) $(COPTIONS) hyperdist.f90
hyperdist.a: hyperdist.f90
	$(COMPILER) $(COPTIONS) hyperdist.f90
ranuni.mod: ranuni.f90
	$(COMPILER) $(COPTIONS) ranuni.f90
ranuni.a: ranuni.f90
	$(COMPILER) $(COPTIONS) ranuni.f90
<rest deleted to save space>



More information about the Gcc-help mailing list