Linking with Fortran

Posted on 2006-03-27
Last Modified: 2013-11-08

I've ran into a problem linking C++ code with code calling functions from a Fortran compiled library.

I have
1. precompiled binary called glib.o which connects to a Fortran library
2. a small piece of C++ code called which calls functions from glib.o

If I compile like this it works fine

g77 glib.o -lf2c -lm -lgribexR64 -lstdc++

however if I change it to

g++ glib.o -lf2c -lm -lgribexR64

it fails and outputs linker errors all which look like

/usr/local/lib64/libgribexR64.a(grsdef.o)(.text+0xbc): In function `grsdef_':
: undefined reference to `G77_getenv_0'
/usr/local/lib64/libgribexR64.a(grsdef.o)(.text+0x16c): In function `grsdef_':
: undefined reference to `G77_getenv_0'

It appears that the reason for this is that when I compile with g77 I include the standard C++ library however when I compile with g++ I don't include its fortran equivalent. If someone could provide the necessary library or some other way of compiling this using g++ and not g77 it would be very helpful.

Thank you,

Question by:alex123

    Author Comment

    Forgot to mention I am using gcc 3.4.5 on Suse9, Intel 64bit CPU
    LVL 11

    Accepted Solution

    Try changing the fortran runtime library name - this was changed in later versions of the GNU toolset

    i.e. change
    g++ glib.o -lf2c -lm -lgribexR64
    g++ glib.o -lg2c -lm -lgribexR64

    Author Comment

    Thank you! This did it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C++ Help from Top Experts

    Solve your toughest problems, fast.
    C++ experts are online now and ready to help you.

    Suggested Solutions

    Title # Comments Views Activity
    matchUp  challenge 9 47
    How to convert MFC APP to Win32 APP. 19 35
    countHi2 challenge 7 30
    word0 challenge 4 34
    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now