gotm-model / code

Source code for the General Ocean Turbulence Model
https://gotm.net
GNU General Public License v2.0
53 stars 44 forks source link

compile error related to netcdf #51

Open gaelforget opened 3 weeks ago

gaelforget commented 3 weeks ago

I just tried to build gotm on my laptop (Mac, M3), but get the following error message, despite the fact that netcdf is installed and the library path seems correctly detected.

What am I missing? How do I fix this?

Error message:

[ 97%] Building Fortran object gotmlib/CMakeFiles/gotm.dir/gotm.F90.o
[ 98%] Building Fortran object gotmlib/CMakeFiles/gotm.dir/cmdline.F90.o
[100%] Building Fortran object gotmlib/CMakeFiles/gotm.dir/print_version.F90.o
[100%] Linking Fortran static library libgotm.a
[100%] Built target gotm
[100%] Building Fortran object CMakeFiles/gotm_exe.dir/src/gotm/main.F90.o
[100%] Linking Fortran executable gotm
ld: warning: ignoring duplicate libraries: '-lnetcdf'
ld: library 'netcdf' not found
collect2: error: ld returned 1 exit status
make[2]: *** [gotm] Error 1
make[1]: *** [CMakeFiles/gotm_exe.dir/all] Error 2
make: *** [all] Error 2

?2 gotmbuild_20240614b % grep netcdf *
CMakeCache.txt:NetCDF_INCLUDE_DIRS:PATH=/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include
CMakeCache.txt:NetCDF_LIBRARIES:STRING=-L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib -lnetcdff -lnetcdf -lnetcdf
CMakeCache.txt:FIND_PACKAGE_MESSAGE_DETAILS_NetCDF:INTERNAL=[-L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib -lnetcdff -lnetcdf -lnetcdf][/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include][v()]

Details on netcdf as installed :

nf-config

This  4.6.1 has been built with the following features:

  --cc        -> clang
  --cflags    -> -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include  -g -Wall -Wno-unused-variable -Wno-unused-parameter -O2

  --fc        -> /opt/homebrew/bin/gfortran
  --fflags    -> -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include
  --flibs     -> -L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib -lnetcdff -lnetcdf -lnetcdf
  --has-f90   -> TRUE
  --has-f03   -> yes

  --has-nc2   -> yes
  --has-nc4   -> yes

  --prefix    -> /opt/homebrew/Cellar/netcdf-fortran/4.6.1
  --includedir-> /opt/homebrew/Cellar/netcdf-fortran/4.6.1/include
  --version   ->  4.6.1
knutaros commented 2 weeks ago

Hi Gael,

sorry for the late reply. I have no experience with Mac, homebrew and separate netcdf, so Karsten and Jorn probably know better how to handle in a sustainable and non-intrusive way...

For now you could try to add "-DCMAKE_EXE_LINKER_FLAGS=-L" to you cmake call.

Hope this helps? Cheers, Knut

On 6/13/24 18:02, Gael Forget wrote:

I just tried to build |gotm| on my laptop (Mac, M3), but get the following error message, despite the fact that |netcdf| is installed and the library path seems correctly detected.

What am I missing? How do I fix this?

Error message:

|[ 97%] Building Fortran object gotmlib/CMakeFiles/gotm.dir/gotm.F90.o [ 98%] Building Fortran object gotmlib/CMakeFiles/gotm.dir/cmdline.F90.o [100%] Building Fortran object gotmlib/CMakeFiles/gotm.dir/print_version.F90.o [100%] Linking Fortran static library libgotm.a [100%] Built target gotm [100%] Building Fortran object CMakeFiles/gotm_exe.dir/src/gotm/main.F90.o [100%] Linking Fortran executable gotm ld: warning: ignoring duplicate libraries: '-lnetcdf' ld: library 'netcdf' not found collect2: error: ld returned 1 exit status make[2]: [gotm] Error 1 make[1]: [CMakeFiles/gotm_exe.dir/all] Error 2 make: ** [all] Error 2 ?2 gotmbuild_20240614b % grep netcdf CMakeCache.txt:NetCDF_INCLUDE_DIRS:PATH=/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include CMakeCache.txt:NetCDF_LIBRARIES:STRING=-L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib -lnetcdff -lnetcdf -lnetcdf CMakeCache.txt:FIND_PACKAGE_MESSAGE_DETAILS_NetCDF:INTERNAL=[-L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib -lnetcdff -lnetcdf -lnetcdf][/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include][v()] |

Details on netcdf as installed :

|nf-config This 4.6.1 has been built with the following features: --cc -> clang --cflags -> -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include -g -Wall -Wno-unused-variable -Wno-unused-parameter -O2 --fc -> /opt/homebrew/bin/gfortran --fflags -> -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include --flibs -> -L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib -lnetcdff -lnetcdf -lnetcdf --has-f90 -> TRUE --has-f03 -> yes --has-nc2 -> yes --has-nc4 -> yes --prefix -> /opt/homebrew/Cellar/netcdf-fortran/4.6.1 --includedir-> /opt/homebrew/Cellar/netcdf-fortran/4.6.1/include --version -> 4.6.1 |

— Reply to this email directly, view it on GitHub https://github.com/gotm-model/code/issues/51, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC2RV6X7F5AUHXFKALYMOJLZHG7ADAVCNFSM6AAAAABJIWQIVOVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM2TCNJSGA3TQOI. You are receiving this because you are subscribed to this thread.Message ID: @.***>

gaelforget commented 2 weeks ago

Hi Knut. Thanks for getting back to me.

I tried the following but no luck -- same error message occurs.

cmake $GOTM_BASE -DGOTM_USE_FABM=off -DCMAKE_EXE_LINKER_FLAGS=-L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib

However, a method that seems to work is to manually edit the CMakeCache.txt file to remove the two instances of -lnetcdf -lnetcdf. Not ideal, but I was able to run some of the examples which generated netcdf files and no errors, so the executable looks ok.

May be a good idea to put a note about this on the webpage that gives the compile direction until issue is fixed -- in case other users have the same.

knutaros commented 2 weeks ago

Hi Gael,

On 6/17/24 14:26, Gael Forget wrote:

Hi Knut. Thanks for getting back to me.

I tried the following but no luck -- same error message occurs.

|cmake $GOTM_BASE -DGOTM_USE_FABM=off -DCMAKE_EXE_LINKER_FLAGS=-L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib|

|you have to provide the path to the folder where libnetcdf (not libnetcdff) is located (netcdf-c?).|

Cheers, Knut

However, a method that seems to work is to manually edit the |CMakeCache.txt| file to remove the two instances of |-lnetcdf -lnetcdf|. Not ideal, but I was able to run some of the examples which generated netcdf files and no errors, so the executable looks ok.

May be a good idea to put a note about this on the webpage https://gotm.net/software/linux/ that gives the compile direction until issue is fixed -- in case other users have the same.

— Reply to this email directly, view it on GitHub https://github.com/gotm-model/code/issues/51#issuecomment-2173264114, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC2RV6WWPZJG3W4EQMOUPTDZH3IXVAVCNFSM6AAAAABJIWQIVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNZTGI3DIMJRGQ. You are receiving this because you commented.Message ID: @.***>

gaelforget commented 2 weeks ago

|you have to provide the path to the folder where libnetcdf (not libnetcdff) is located (netcdf-c?).|

I tried this but no luck, same error :

cmake $GOTM_BASE -DGOTM_USE_FABM=off -DCMAKE_EXE_LINKER_FLAGS=-L/opt/homebrew/Cellar/netcdf/4.9.2_1/lib

The folder above contains :

cmake           libnetcdf.a     libnetcdf.settings
libnetcdf.19.dylib  libnetcdf.dylib     pkgconfig

I think this is what you meant Knut. Is there supposed to be a file called netcdf-c there, or something else I am missing?

bolding commented 1 week ago

Hello

It seems you have libnetcdf.a and libnetcdff.a in two different folders - /opt/homebrew/Cellar/netcdf/4.9.2_1/lib and /opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib.

That might be the reason why libnetcdf.a is not found initially.

Somehow it seems nf-config is not working properly as it does not report where to find libnetcdf.a. What Knut suggest is to manually add this folder - via the -L argument to the linker - https://cmake.org/cmake/help/latest/variable/CMAKE_EXE_LINKER_FLAGS.html.

Karsten

gaelforget commented 1 week ago

Thanks Kirsten, and Knutt

This work-around worked :

cmake $GOTM_BASE -DGOTM_USE_FABM=off -DCMAKE_EXE_LINKER_FLAGS=-L/opt/homebrew/lib

We have a similar issue with MITgcm it seems on arm64 Macs -- See https://github.com/MITgcm/MITgcm/issues/846