[fpm] can fpm be used to build a dynamic link library?

Hi, I was just wondering if fpm could be used to build a dll/so as a library output? I have seen how to generate a static lib but haven’t seen in the manuals any info regarding dynamic libs.

Today I manage to do it with CMake. Would like to see if fpm could help doing it in a more “clean” manner?

1 Like

Last time I checked (which was a while back) it couldn’t. Probably a worthwhile feature to add if not present.

Note there should already be a GitHub issue for this.

You are right: Producing shared libraries · Issue #681 · fortran-lang/fpm · GitHub the issue is still open.

Not that fpm shouldn’t at some point be able to do it for you, but would this work for your needs?

I did it by hand with a simple file and it works, I also saw here how to do it with ifort https://community.intel.com/t5/Intel-Fortran-Compiler/Creating-dll-from-fortran-file/m-p/1182026 from a single source file. I guess that is more or less what CMake is currently doing for me? the goal was to see if it could be easily doable within a fpm build. Just out of curiosity :slight_smile:

I would like to share a very simple Makefile if you need to create a shared library from the static one generated by fpm.
fpm is used to build the static library and the latter is copied to the root of the build folder. Then it is used to create the shared library with gfortran according to the defined PLATFORM. You can add additional target according to your needs.

LIBNAME=mylib
PLATFORM=linux
BUILD_DIR=./build

all: $(LIBNAME)

$(LIBNAME): build copy_static shared copy_shared

build:
	fpm build --profile=release

copy_static:
	cp $(shell find ./build -type f -name lib$(LIBNAME).a) $(BUILD_DIR)

shared: shared_$(PLATFORM)

shared_linux:
	gfortran -shared -o $(BUILD_DIR)/lib$(LIBNAME).so -Wl,--whole-archive $(BUILD_DIR)/lib$(LIBNAME).a -Wl,--no-whole-archive

shared_darwin: 
	gfortran -dynamiclib -install_name @rpath/lib$(LIBNAME).dylib -static-libgfortran -static-libquadmath -static-libgcc -o $(BUILD_DIR)/lib$(LIBNAME).dylib -Wl,-all_load $(BUILD_DIR)/lib$(LIBNAME).a -Wl,-noall_load

shared_windows: 
	gfortran -shared -static -o $(BUILD_DIR)/lib$(LIBNAME).dll -Wl,--out-implib=$(BUILD_DIR)/lib$(LIBNAME).dll.a,--export-all-symbols,--enable-auto-import,--whole-archive $(BUILD_DIR)/lib$(LIBNAME).a -Wl,--no-whole-archive
2 Likes

Welcome @mskocic and thanks for the Makefile :+1: I haven’t check the inners of how fpm does its magic but would be interesting if something like this could be added :thinking:

I didn’t look in details at the fpm internals but I assume it might be challenging to deal with rpath according to the platforms and the whole set of options for the different available compilers. Windows is, in my point of view, always a special case where there is a ton of specific options ( :woozy_face:).