Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

OpenMP and g77

  1. Sep 2, 2007 #1
    I'm trying to get a fortran 77 program to run faster on a computer with a dual-core processer, so I'm trying to learn how OpenMP works. Unfortunately, I can't get the "hello world" file on the wikipedia page to compile.

    (Here's the code:
    Code (Text):
          ID = OMP_GET_THREAD_NUM()
          IF ( ID .EQ. 0 ) THEN
          END IF
    I'm using G77 as a compiler. I think I'm supposed to compile it as

    Code (Text):
    ...> g77 HelloWorld.f -openmp
    I get the following error:

    Code (Text):
    /tmp/cc2XahMo.o(.text+0xe): In function 'MAIN__':
    : undefined reference to 'omp_get_thread_num__'
    /tmp/cc2XahMo.o(.text+0x75): In function 'MAIN__':
    : undefined reference to 'omp_get_num_threads__'
    collect2: ld returned 1 exit status
    I don't think the problem's that g77 doesn't accept openmp, because if I have

    Code (Text):
    ...> g77 -openmp
    without the filename, it has a different error message than replacing "-openmp" with "-gdfgs" or something, specifically,
    Code (Text):
    g77: no input files; unwilling to write output files
    rather than
    Code (Text):
    g77: no input files
    Could someone who knows something about g77 or openMP help me?
  2. jcsd
  3. Sep 2, 2007 #2


    User Avatar
    Science Advisor
    Homework Helper

    Most likely the problem is that the openMP library is meant to link to C or C++ programs not Fortran.

    The clue is the fact that the unresolved reference is omp_get_thread_num__ (with some underscores at the end) not omp_get_thread_num which is presumably the C function you are trying to call.

    Either you need a Fortran interface library, or you need to tell your compiler these are C functions. I expect there's a way to do that, but I don't have g77.

    FWIW the command g77 -openmp is probably trying to create an object file called penmp.o, or an executable called penmp or penmp.exe, independent of the name of your .f file - which is a legal command, but not what you wanted to do!

    Also FWIW some compiler systems understand the option -mp (not -openmp).
  4. Sep 3, 2007 #3
    What version of GCC are you using? (i.e. g77 -v)
  5. Sep 3, 2007 #4
    You need to tell g77 to link to your OpenMP library file with the -l option. something like
    Code (Text):
    g77 helloworld.f -o helloworld -l<openmp library name>
    where you replace <openmp library name> with the name of the OpenMP library minus the leading 'lib' and the extension
    i.e. if your openMP library is called libopenmp.so (or libopenmp.a), then you use libopenmp

    you might also have to specify the path to your OpenMP library with -L if it's not in the compiler's library search path

    It's been a while since I worked with g77, but since it's just a front-end to gcc now, it should work. I've also not worked with OpenMP before, so I'm not positive there's a library that goes with it but if there is this is what you'd need to do.
    Last edited: Sep 3, 2007
  6. Sep 3, 2007 #5
    I was able to build using GCC 4.2. As far as I understand, GCC did not support openmp prior to 4.2 (although some people built it into 4.1 on a provisional basis, e.g. fedora core 5/6).

    I built GCC 4.2, than ran

    /my/gcc4.2dir/gfortran -fopenmp hello_f77.f

    with GCC 4.2 directories (including gmp and mpfr) at the front of LD_LIBRARY_PATH.

    Not sure g77 will do that for you, as g77 is an artifact of pre-4.0 (unless you renamed your GCC 4.2 gfortran g77).

    Maybe simply linking against the openmp lib will do it, pls let us know if that works.
  7. Sep 3, 2007 #6
    Yes, -openmp was apparently trying to create a file named penmp.out or something. I must've read this site, which has the tag as -fopenmp, and forgot the f. Unfortunately, here's what I got:
    Code (Text):
    ...> g77 HelloWorld.f -fopenmp
    f771: error: unrecognized command line option "-fopenmp"
    I also tried -mp and -MP, but neither worked.

    FYI, I'm using g77 which was installed on my school's computers, and I'm using tera term (unix).

    nmtim, here's what I got for g77 -v:
    Code (Text):
    ...> g77 -v
    Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/u
    sr/shar/info --enable-shared --enable-threads=posix --disable-checking --with-s
    ystem-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-aw
    t=gtk --host=x86_64-redhat-linux
    Thread model: posix
    gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
    So, to answer your question, version 3.4.6.

    imabug: I'd try that, but I'm not sure where the OpenMP library is.
  8. Sep 3, 2007 #7
    After doing a little more reading, I get the impression that OpenMP is a set of compiler extensions that's usually built in, so there may not be a library. nmtim is probably correct and your compiler version may not support the OpenMP extensions.
  9. Sep 5, 2007 #8
    I'm guessing nmtim is right about openMP not being included.

    So, next question: A while back I installed gfortran on the computer in my directory. However, I can't figure out how to use it in unix. (The file is gfortran.exe, and I've used it in the DOS command prompt without problems.) Can you run .exe files in a unix terminal?
  10. Sep 5, 2007 #9
    It might run through WINE, but generally, no
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook