vmagnin / forcolormap

A Fortran library for colormaps
https://vmagnin.github.io/forcolormap/
MIT License
21 stars 4 forks source link

Using ForColormap as a static library fails #21

Closed vmagnin closed 8 months ago

vmagnin commented 8 months ago

@jchristopherson @gha3mi

System: Ubuntu 23.10, FreeBSD 14.0 Compiler: GFortran 13.2.0

I am now trying to use ForColormap as a static library (.a under Linux). I have written a toy.f90 minimalist program:

use forcolormap, only: Colormap, wp
type(Colormap) :: cmap

call cmap%set("cubehelix", 0.0_wp, 1.0_wp, 1024)
print *, "test"
end

ForColormap was configured with cmake -D BUILD_SHARED_LIBS=false .. before building and installing. The .mod files are all in /usr/local/include/ and the two .a are in /usr/local/lib/.

But I can not compile my toy program with the -static option:

$ gfortran -static toy.f90 $(pkg-config --cflags --libs forcolormap)
/usr/local/bin/ld: /usr/local/lib/libforcolormap.a(colormap_class.f90.o): in function `__forcolormap_MOD_write_ppm_colorbar':
colormap_class.f90:(.text+0xc10): undefined reference to `__pnm_MOD___vtab_pnm_Format_pnm'
/usr/local/bin/ld: colormap_class.f90:(.text+0xf38): undefined reference to `__pnm_MOD_set_format'
/usr/local/bin/ld: colormap_class.f90:(.text+0xf90): undefined reference to `__pnm_MOD_set_pnm'
/usr/local/bin/ld: colormap_class.f90:(.text+0xfbe): undefined reference to `__pnm_MOD_export_pnm'
/usr/local/bin/ld: colormap_class.f90:(.text+0x10a7): undefined reference to `__pnm_MOD_set_format'
collect2: error: ld returned 1 exit status
gha3mi commented 8 months ago
$ gfortran -static toy.f90 $(pkg-config --cflags --libs forcolormap)

I didn't test it yet, maybe need to add forimage as well? --libs forimage forcolormap

gha3mi commented 8 months ago

This works on my system:

cd forcolormap
mkdir build && cd build
cmake -D BUILD_SHARED_LIBS=false ..
make
sudo make install
cd mytest
gfortran -static toy.f90 -L/usr/local/lib -I/usr/local/include -lforcolormap -lforimage
vmagnin commented 8 months ago

Thanks a lot @gha3mi ,

I have advanced a lot thanks to your message. The following compilation commands works:

gfortran -static toy.f90 -L/usr/local/lib -I/usr/local/include -lforcolormap -lforimage
gfortran -static toy.f90 $(pkg-config --cflags --libs forcolormap forimage)

The following compilation commands fails:

gfortran -static toy.f90 $(pkg-config --cflags --libs forcolormap)
gfortran -static toy.f90 $(pkg-config --cflags --libs forimage forcolormap)
gfortran -static toy.f90 -L/usr/local/lib -I/usr/local/include -lforimage -lforcolormap

I had tried to use --libs forimage forcolormap with pkg-config. This order seemed intuitive (first the dependency, then the library itself), although I also thought it was not important. But it fails. And the reverse order works: --libs forcolormap forimage (and same remark with your detailed command).

gha3mi commented 8 months ago

fpm makes me forget all knowledge about that :)

I have added an uninstall target to CMake, providing the ability to use make uninstall. Perhaps you could also add it for forcolormap. See https://github.com/gha3mi/forimage/issues/17 and the corresponding changes: https://github.com/gha3mi/forimage/commit/f1b4056fa0fb62727844faaa6aa61e2891df1941. The same approach can be applied to forcolormap.

vmagnin commented 8 months ago

I have added an uninstall target to CMake, providing the ability to use make uninstall. Perhaps you could also add it for forcolormap. See gha3mi/forimage#17 and the corresponding changes: gha3mi/forimage@f1b4056. The same approach can be applied to forcolormap.

Good idea! I put it in my TODO list. There is also such a functionality in gtk-fortran: https://github.com/vmagnin/gtk-fortran/blob/gtk4/cmake/cmake_uninstall.cmake.in

vmagnin commented 8 months ago

https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking