1

How would I make a DLL file from a Makefile where the files are written in Fortran? I'm working with Windows but having it compiled under a Linux system isn't a problem.

I have two modules and one file that uses those modules. Normally, I would just write the following to to produce what I want:

gfortran module1.f90 module2.f90 main.f90 -o main.dll
Cleo
  • 67
  • 8

1 Answers1

0

If module1.f90 and module2.f90 are just modules and you want to create a dll, you may use the following command:

gfortran -o main.dll module1.f90 module2.f90 -shared -fPIC -lgfortran

It will generate a dll that can be loaded later by the main program.

The main flags here for generate a lib are -shared and -fPIC.

If main.f90 is the main program, the output should be an exe, and the command to compile maybe:

gfortran -o main.exe module1.f90 module2.f90 main.f90

It will generate an exe with the main file using the modules.


Edit:

Here, is a sample Makefile that builds the DLL (for the question):

FC=gfortran
FFLAGS=-g -shared -fPIC
LDFLAGS=-lgfortran

main.dll: main.f90 module1.o module2.o
    $(FC) $(LDFLAGS) -o $@ $?

module1.o: module1.f90
    $(FC) $(FFLAGS) -o $@ $? 

module2.o: module2.f90
    $(FC) $(FFLAGS) -o $@ $?

clean:
    rm -f *.o *.exe *.dll

After executing it:

gfortran -g -shared -fPIC -o module1.o module1.f90
gfortran -g -shared -fPIC -o module2.o module2.f90
gfortran  -o main.dll main.f90 module1.o module2.o

It generates the DLL:

20/12/2016  16:23            59.091 main.dll

Maybe, this Makefile can be improved with use of macros.

HTH

tested with gfortran version 6.2.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

Community
  • 1
  • 1
Gomiero
  • 2,240
  • 2
  • 22
  • 18
  • Where does this talk about a Makefile? Using the .f08 suffix is not a good idea. – Vladimir F Героям слава Dec 20 '16 at 08:57
  • thanks, I did this but my question was to have a makefile do it for me instead of writting it myself – Cleo Dec 20 '16 at 08:57
  • @Cleo I edited the answer with an example. I hope this can help :) – Gomiero Dec 20 '16 at 18:40
  • @VladimirF I'm relatively new to Fortran. Could you, please, point me some link that explains why is not a good idea to use .f08? I think (probably wrong) it's always a good idea to use the latest version of languages/extensions. Thanks!! – Gomiero Dec 20 '16 at 18:42
  • .f08 is not even recognized by some common compilers. http://stackoverflow.com/questions/20269076/correct-suffix-for-fortran-2003-source-file-intel-fortran-compiler .f90 is de-facto standard for free-form. You also don't use .c99 .c11 or .cpp14 for C and C++. – Vladimir F Героям слава Dec 20 '16 at 19:45
  • @VladimirF Many Thanks! :) – Gomiero Dec 20 '16 at 19:49
  • @Gomiero thanks for your example but am I suppose to make the .o files for the modules before hand? Also, I am getting an error that: it cannot read a module because it was created by a different version of GNU fortran., and this module isn't one that I have made – Cleo Dec 22 '16 at 14:42
  • Ideally, the dll should contain just modules, not the main program, and they should also be compiled with `-shared` flag. The Makefile on the answer, as I said, is just a simple sample and it can be improved. About the error your are getting, all the sources are compiled at same time with the same compiler? Maybe there is an incompatibility if you have some binaries compiled with anoher version/system together. – Gomiero Dec 22 '16 at 15:19
  • thanks, thats probally the problem then, I'm not sure how to fix it though since I only have the .mod file that is causing the problems – Cleo Dec 22 '16 at 15:41