Problems linking Fortran/C++ code on Mac OS X

I am trying to compare the results of a C++ hash function with the Fortran translation of that function. I therefore want to link the Fortran function, a Fortran driver, the original C++ code, and an “Extern C” wrapper for the C++ code. I do my work on a MacBook Pro, that runs an Intel processor, under MacOS 11.6. I’m attempting to use GNU Fortran (gfortran) and GNU C++ (g++), as installed using Mac Ports. The installed compilers don’t know the location of the system include files or libraries, so I am manually providing links to the directories with the link command. Everything now goes well until the link command where I get

    ...% make -f Makefile.validation

    gfortran -O3 -I../../mod_files -J./mod_files -L../../ -L./ -L/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/ \
    test_64_bit_hash_validation.o \
    spooky_wrapper.o \
    -lfortran_stdlib -lc_hash \
     -o test_64_bit_hash_validation -

    gfortran: error: -E or -x required when input is from standard input

As near as I can tell the reported error gfortran: error: -E or -x required when input is from standard input implies that “gfortran” thinks I am asking for the compilation of code being input through stdin, e.g., I am piping the source code to the command line . This leads to the following questions:

  1. Is there a better forum for this problem?
  2. Is my interpretation of the message correct?
  3. What am I doing wrong?
  4. How do I fix the problem?
3 Likes

Never mind. The problem is the hyphen at the end of the link command.

2 Likes

Yes, this is the right forum. I am glad you figured it out.

@wclodius I’ve recently been doing some Fortran/C++ mix. I ran across some cites saying you need to use the C++ linker at the end to make sure thing work properly.

When mixing Fortran and C++ in the same program, you need to invoke the C++ compiler to correctly link the final program.

I’ve noticed that sometimes when I link with gfortran instead (which still “looks” like it works at first), then there is strange behavior. For example, C++ exceptions, which should be caught, never get caught.