makefile for building a library

makefile for building a library

Post by TideMa » Mon, 23 Feb 2009 17:16:14

I'm porting some routines from CVF 6.6 on Win XP to gfortran on Linux.
CVF is wonderful because I don't have to worry about makefiles. It
does it all for me automatically. Using Linux takes me back to the
incantations we called JCL on the IBM 360 ............

I understand that I must replace my .dll containing a bunch of
subroutines from CVF with a library prepared using:
ar -crs sublib.a sub1.o sub2.o sub3.o etc
then build my executable using a makefile:
objects=MyProgMod.o MyProg.o sublib.a

MyProg: $(objects)
<tab>$(f90) -o MyProg $(objects)


and this works fine.

But now I'd like to build the library within this makefile, rather
than a bash script.
I've looked all over the web, but cannot find how to do it.
Can anyone help, or point me to a webpage/tutorial, please?

makefile for building a library

Post by Louis Krup » Tue, 24 Feb 2009 04:27:46

Try this:

Note that you can put your subroutines in a shared object (.so) file,
which is similar to a DLL; you won't have to relink your executable
every time a subroutine changes (unless you change the interface).

(You probably knew this, but if you have include files, you'll have to
add them to the dependencies in the makefile.)

Also, CVF comes with what's known as an Integrated Development
Environment, or IDE (in the case of CVF, I think it's called Visual
Studio), and that's what builds the makefiles for you so you don't have
to write them manually. If you Google for "linux IDE" or "linux Fortran
IDE," you might find something you can download that will give you
something like what you're accustomed to.



makefile for building a library

Post by Arja » Tue, 24 Feb 2009 06:36:38

Try a Perl script that makes your Makefiles...

Ps. If you can't find it, I'll send you a copy!

makefile for building a library

Post by TideMa » Tue, 24 Feb 2009 07:28:45

Thanks Arjan

I found it OK.
It looks to be just what I need.
I'm off to learn how to use Perl...........

makefile for building a library

Post by Arja » Wed, 25 Feb 2009 02:08:36

> It looks to be just what I need.


One of the platforms under which I develop is Win-XP/MinGW/MSYS
using the g95 compiler. There is an extension to MSYS, I believe
(Development ToolKit). That one contains Perl.

Now create a configuration file for your specific compiler, e.g. g95,
like this:

CC = gcc
FC = g95
LD = g95
# The following switches are necessary for the de *** !
FFLAGS = -g -ftrace=full
# add your libraries after LIBS= , e.g. -lblas
# LIBS = c:/mingw/lib

Assume the above is stored in a file named g95_args.
If you use a different compiler, you'll have to use names and switches
that apply to that compiler.
Once you have Perl, and once you have copied the script and
the compiler settings file to your source directory, you open a shell
in your source directory and call:

perl ./ -t ./g95_args -p myprogram.exe

If all is right, you now have a new file named Makefile.
That's all I know about Perl.
Maybe this is enough Perl for you too? ;-))


makefile for building a library

Post by OldSchoo » Wed, 25 Feb 2009 02:14:19

?? the basic concept is the same as the statement used for "MyProg"

you need to have a target for sublib.a that has the objects it
contains, something like:

subobjs = (sub1.o sub2.o sub3.o)

sublib.a: $subobjs
ar -crs $subobjs

and then build rules for each of the objects

sub1.o: sub1.c sub.h .....
cc .........

make will sort out the dependencies and rebuild the library if the
source changes, however the syntax above may be "off" a little.

see if you can find a book called "Managing Projects with Make". Out
of print, but likely available on Amazon. It covered a lot of make
variants and limitions. It's later, GNU specific version is:

makefile for building a library

Post by TideMa » Wed, 25 Feb 2009 03:57:53

I've got mkmf working OK and have built a number of Makefiles with it.
As always, there are some unresolved difficulties.
1. Most of my routines are .f90, but one is .for (a relic Fortran IV
(!!) subroutine). mkmf did not pick this one up, so I had to insert it
manually in the Makefile.
2. I couldn't figure out how to append the libraries I want to link,
so I put them in this way in my configuration file:
LDFLAGS=../DateHour/DateHour.a ../GetTide/GetTide.a
It works, but I'm not sure it's the correct way to do it.

Once again, thanks for the help.

makefile for building a library

Post by Arja » Wed, 25 Feb 2009 04:41:37

> 1. Most of my routines are .f90, but one is .for (a relic Fortran IV

Did you code all the zeros and ones of the source-file with a morse-
on the side-panel of a PDP-11? :-)

Generally, you're as far as I am now.
You may have found a pragmatic hack, but then: pragmatic it is!

You're welcome!


makefile for building a library

Post by Arja » Wed, 25 Feb 2009 04:43:47

Problem is that "make" does not take care of the order of the
With f90+ that is an issue. Utility knows how to deal with


makefile for building a library

Post by Louis Krup » Wed, 25 Feb 2009 04:52:06

It's normally done like this:

1. Name the libraries libDateHour.a and libGetTide.a

2. LD_FLAGS = -L../DateHour -lDateHour -L../GetTide -lGetTide


makefile for building a library

Post by Craig Powe » Wed, 25 Feb 2009 06:04:10

Try exporting your CVF project to a makefile. You'll likely need to
modify it, but it should start out fairly close to complete. I also
wouldn't guess that nmake (AFAIK the target "make" for CVF makefiles)
accepts anything that gnu make doesn't.

makefile for building a library

Post by TideMa » Wed, 25 Feb 2009 07:19:46

On Feb 24, 10:04m, Craig Powers < XXXX@XXXXX.COM >

You're right, Craig, I've just discovered I can generate a makefile in
CVF by Project/Export Makefile. However, the .mak file so generated
doesn't look compatible with a Linux Makefile because it uses !, not
#, for comments and \, not /, for directories, etc.
I think I'm better off using mkmf.

makefile for building a library

Post by Craig Powe » Wed, 25 Feb 2009 08:07:47

Oops, yeah, although those would be simple textual substitutions.

Regardless, if you already have mkmf working, there's little point to
changing horses midstream. I hadn't read the rest of the thread at the
point where I posted the suggestion to export a Makefile.

makefile for building a library

Post by John » Fri, 27 Feb 2009 01:33:49

n Feb 22, 3:16m, TideMan < XXXX@XXXXX.COM > wrote:

There is a uuencoded gzipped tar file at

The purpose of the site is to let someone access my version of the
VOGLE graphics library for low-level
graphics from Fortran and C (It's main difference from the original is
that I added many output devices -- VML, SVG, HTML5 CANVAS, Adobe
PDF, ...)

What you might find interesting is the way each library is just layed
out in a directory with as
many subdirectories as you want to keep related routines grouped.
There is a file at the top of
each library directory that lists all the files used to build that
library; and another very small
one that holds the name of the library to generated.

There is a scripts directory in which you might have
change the script to add your own library.
I cut this way down to just make a vogle release and
didn't go out of my way to describe how it works but
it has a good number of advantages and I have used it with dozens of
libraries simultaneously, building
several million of lines. It already has make files
for dozens of configurations; and I have config files for many systems
including Linux with Intel,
NAG, g95, gfortran, g77, PathScale, ...

1) If the files are in an NFS-mounted file system you can build many
platforms simultaneously.

2) It works with GNU, Posix, BSD, and SysV versions of make.

3) The source files can be HTML files or scripts as well as Fortran or
C or whatever.

4) As the libraries are built a text archive is build with the
expanded source in it for Fortran
that uses cpp(1) directives

5) Used with the publish script, all the source and
supporting documentation can be combined into a
single PDF file for each library (depends on what
other tools you have on your machine; like html2ps and

6) Each platform has a directory made for the libraries generated on

7) The ccall command can then be used on any platform to build most
programs (whether C, Fortran,

8) You get a log file for each build

It's not for the timid; you have to know Unix or GNU
shell scripting to add case statements to,
system, publish, and ccall to add a new compiler or

On the other hand, I haven't made a make(1) file in a long time; and I
build dozens of libraries on many
many platforms.

It is NOT integrated into something like CVS. This scheme has worked
for me for decades on platforms ranging from UNICOS, Tru64, AIX,
NextStep, OpenBSD,
AIX, Linux, CygWin, ...

I could probably make this into a mini-example with
a few example libraries in a few days if there is
interest; but I've felt this was overkill for most