I installed netcdf and netcdf-cxx libraries with Homebrew on my MacOS, and for a test, I tried to compile an example code found in the official website https://www.unidata.ucar.edu/software/netcdf/examples/programs/SfcPresTempWr.cpp.
From Linking and using netCDF with gcc, I added 2 linker options -lnetcdf_c++ -lnetcdf
in the compilation command:
g++ SfcPresTempWr.cpp -o SfcPresTempWr -lnetcdf_c++ -lnetcdf
But the linker produces an error
ld: library not found for -lnetcdf_c++
I installed netcdf-cxx with brew install netcdf-cxx
, but there is no netcdf_c++ or netcdf-cxx library in /usr/local/lib
or /usr/lib
.
Do you know how to fix this?
I could make it work with the following steps:
Search for the library location with find /usr -name netcdf-cxx
.
In my case, the location was /usr/local/Cellar/netcdf-cxx/4.3.1_1/lib/
.
Add the library location to the compilation command with -L
option and the library name with -l
option. In my case, the command became:
g++ SfcPresTempWr.cpp -o SfcPresTempWr -L/usr/local/Cellar/netcdf-cxx/4.3.1_1/lib/ -lnetcdf-cxx4.1.1.0
It turned out that I didn't need to add -lnetcdf
at the end of the command, and without -L
option it compiled successfully as well.