Building NCO against shared libraries

  • I was building NCO and linking to HDF and netCDF libraries compiled with -fPIC, and found that I had to manually modify one Makefile in the NCO tree to get it to work when building with the Gnu or PGI compilers (gcc 4.4.7, pgi 14.7, on RedHat 6.3).

    My setup for the Gnu build including some system-specific module loads was

    module load jdk
    module load hdf5-1.8.13/gcc-4.4.7
    module load netcdf-4.3.2/gcc-4.4.7
    module load udunits-2.2.16/gcc-4.4.7
    module load gsl
    setenv CC gcc
    setenv CXX g++
    setenv FC gfortran
    setenv F77 gfortran
    setenv CFLAGS "-O3 -fPIC"
    setenv FFLAGS "-O3 -fPIC"
    setenv CXXFLAGS "-O3 -fPIC"
    setenv FCFLAGS "-O3 -fPIC"
    setenv NETCDF_INC /usr/local/apps/netcdf-4.3.2/gcc-4.4.7/include
    setenv NETCDF4_ROOT /usr/local/apps/netcdf-4.3.2/gcc-4.4.7
    setenv NETCDF_LIB /usr/local/apps/netcdf-4.3.2/gcc-4.4.7/lib
    setenv UDUNITS2_PATH /usr/local/apps/udunits-2.2.16/gcc-4.4.7
    ./configure --prefix=/usr/local/apps/nco-4.4.4/gcc-4.4.7 --with-pic --enable-netcdf4

    make was fine up through compiling ncap2, then I saw

    libtool: link: g++ -O3 -fPIC -o .libs/ncap2 Invoke.o ncap2.o ncap2_utl.o sdo_utl.o sym_cls.o fmc_cls.o fmc_all_cls.o fmc_gsl_cls.o prs_cls.o NcapVar.o NcapVarVector.o
    ncoLexer.o ncoParser.o ncoTree.o nco_gsl.o -L../nco -L/usr/local/apps/netcdf-4.3.2/gcc-4.4.7/lib -L/usr/local/apps/udunits-2.2.16/gcc-4.4.7/lib /work/STAFF/dle/nco-
    4.4.4/src/nco/.libs/ -lantlr -lexpat -lm /usr/local/apps/netcdf-4.3.2/gcc-4.4.7/lib/ /usr/local/apps/udunits-2.2.16/gcc-4.4.7/lib/
    -Wl,-rpath -Wl,/usr/local/apps/nco-4.4.4/gcc-4.4.7/lib -Wl,-rpath -Wl,/usr/local/apps/netcdf-4.3.2/gcc-4.4.7/lib -Wl,-rpath -Wl,/usr/local/apps/udunits-2.2.16/gcc-4.4
    /usr/bin/ld: .libs/ncap2: hidden symbol `__dso_handle' in /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtbegin.o is referenced by DSO
    /usr/bin/ld: final link failed: Nonrepresentable section on output
    collect2: ld returned 1 exit status

    The fix was to edit nco-4.4.4/src/nco++/Makefile and add "-Xcompiler -shared" to ncap2_LINK:

    ncap2_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CXXLD) -Xcompiler -shared $(AM_CXXFLAGS) \ $(CXXFLAGS) $(ncap2_LDFLAGS) $(LDFLAGS) -o $@

    The same fix was required when building with the PGI compiler, although PGI spit out lots of additional errors such as undefined reference to `antlr::ASTRef::__dt( (void))'

    The -shared option was found on a forum while searching on the error messages, although where to put it wasn't clear from those posts.

  • Charlie Zender
    Charlie Zender

    Hello Ed,
    Thank you for posting the problem you encountered and its fix.
    This seems like something automake should figure out on its own,
    yet apparently it does not. I am reluctant to change the default automake
    flags unless someone else confirms this problem. If they do I will try adding
    ncap2_LDFLAGS = -Xcompiler -shared
    to nco/src/nco++/