Fortran program; linking in a static library of C code.

Hello All, & thanks in advance for your advice.

I have a FORTRAN program that needs to link to a static library compiled from C/C++ code.

portland group compiler

CC = pgcc
C++FLAGS= -fPIC
.C.o: $(SRC)
$(CC) $(C++FLAGS) -c $<
lib: $(OBJ)
ar r libNAME.a $(OBJ)

The Makefile compiles the code & creates the *.a w/o complaint


The link step, for lack of actually knowing how to do it, looks like this…
f90 = pgf90

getlLIB = -L/path -lNAME

pName : $(OBJ)
$(f90) -o pName $(OBJ) $(getlLIB)

\


I am using 'extern “C” ’ around the C function referenced by FORTRAN, & do not appear too have name-mangling issues. However the compile step (new Makefile) seems to complain about a lack of C/C++ libraries. Error messages from the compile include (but are not limited to):

path/NAME.a(getl.o): In function getl_': path/./getl.C:73: undefined reference to std::cout(void)’
path/./getl.C:80: undefined reference to operator new(unsigned long)' path/./getl.C:82: undefined reference to std::cout(void)’
path/./getl.C:97: undefined reference to std::cout(void)' path/NAME.a(getl.o): In function __sti___6_getl_C_b0e82dec’:
path/./getl.C:103: undefined reference to std::ios_base::_Loc_init::__ct( (void))' path/./getl.C:103: undefined reference to __record_needed_destruction’
path/./getl.C:103: undefined reference to std::ios_base::Init::__ct( (void))' path/./getl.C:103: undefined reference to __record_needed_destruction’
path/NAME.a(getl.o):(.data+0x0): undefined reference to `__T_Q2_3std8ios_base’

etc.

\


I am thinking the problem is in the compile step, but I have been unable to find documentation for this particular case. All help is appreciated!

Andy

Hi Andy,

It appears to me that you just need to add the C++ run time libraries to the link. Are you using pgCC (or pgcpp) as you’re C++ compiler? If so, we have convience flag you can use, “-pgcpplibs”, which will add the appropriate libraries.

  • Mat

Thanks! That took care of that.

I was using pgcc. The documentation says it is a C compiler but I am using a few things that I understood were unique to C++ (such as fstream), so that is a bit confusing.

I assume “-pgcpplibs” is the library to use when I am using C exclusively (no C++) , or in other words that it covers both C & C++?

Thank again!

Andy

For pure C, then you don’t need to add any additional flags. The Fortran run time calls some C run time routines so the C libraries always get added.

  • Mat