Undefined symbols: (gfortran)

Hi,

I am using gcc since I am a Mac user (many years). However, recently I installed gfortran. I tried many versions (now gcc version 4.3.0 20071026), but I am not able to compile simple programs such as:

****************************************************
IMPLICIT REAL*8(A-H,O-Z)
do i = 1, 100
x=sin(dble(i)*0.1)
write(6,*)i,x
enddo
end
****************************************************

When I try to compile I get:

>Marcel$ gfortran doloop.f
>Undefined symbols:
> "_MAIN__", referenced from:
> _main in libgfortranbegin.a(fmain.o)
>ld: symbol(s) not found

The thing is it that I was able to compile it on a Mac Book, but when I tried it on a Mac Book Pro (working-Mac) than it didn't work. However, the above posted example-program works on the Mac Book, but not a more complex program where I need to use an extern library. Than I get exactly the same error message: "Undefined symbols:..."

This looks like that:
>Marcel$ gfortran program.f libnag.a

It would be very nice if somebody could help me with that.

Marcel

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Hi Marcel, 1) How did you

Hi Marcel,

1) How did you install gfortran on the two machines (i.e. which installer package did you use, or did you use Fink/MacPorts, etc....)? Did you use the same method on both machines? Did you set any environment variables, like PATH or LD_LIBRARY_PATH?

2) Is it really *exactly* the same error message in the second case? I.e. does the "undefined symbol" again relate to libgfortranbegin.a? Because I have the feeling that the compiler just is unable to find libnag.a

BTW: do you really want to statically link the whole libnag? Or did you mean to do something like "gfortran -c program.f; gfortran -lnag program.o -o program"?

Cheers,
Stefan

Hi Stefan, 1) I tried

Hi Stefan,

1) I tried different packages, but not the Fink one... in the moment I have installed the version I got here: http://hpc.sourceforge.net/
And yes, I have set already the environment variables (in both macs):

> export PATH=/usr/local/bin:/opt/local/bin:/opt/local/sbin:$PATH
> LD_LIBRARY_PATH=/usr/local/gfortran/lib:/usr/local/lib:usr/lib:/lib
> export LD_LIBRARY_PATH

2) Yes, it is related every time to libgfortranbegin.a.
The libnag.a is in the same folder as the program-files are, so it's local.

The call with libnag.a is ok, since I got the program from a colleague, and it is working fine on his Mac. So it shouldn't be a problem of the program, or how I call link libnag.a!

thanks for trying helping me...

Marcel

I checked the version which

I checked the version which is available here:
http://gcc.gnu.org/wiki/GFortranBinaries
(gcc version 4.5.0 20090604 (experimental) [trunk revision 148180] (GCC))

The error message changed to this:
Undefined symbols:
"_main", referenced from:
start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

If somebody has an idea...!?

best
Marcel

Undefined symbols

I have seen that error before when I had multiple versions of gfortran running around my system. The linker was trying to link against the wrong version of libgfortran.a, I am not sure if that's the problem you are seeing now but trying to isolate which version of libgfortran you have and making sure it's the correct one for the compiler you are using might be a good place to start. One way to do this would be to use a macports or fink version of gfortran which would keep the versions and libraries separate from the usual place for such things.

Hope this helps!

Kyle

That could be a reason,

That could be a reason, because I tried to install many versions already.

I will try that.

thanks
Marcel

Undefined symbols

Yes, I am also pretty sure the "undefined symbols" come from the linker finding the wrong libraries when multiple versions of gfortran are installed. Have you already tried to deinstall them using pkgutil or something like desinstaller? If they got installed via pkgs or mpkgs, the receipts should still be in /Library/Receipts...

That can't be it. In the

That can't be it.

In the meanwhile I messed up my whole system. Thanks to Mac I could install the "system-core" without loosing my data and the rest of the stuff.
Anyway, so I had a fresh system without anything which was related to gfortran. However, I got the same problem again.

So I really don't know.

Suggestion

Hi -

Try:

gfortran -v -o doloop doloop.f

and see if the libraries it's attempting to link against match up with the reported version of gfortran you're using. Here's an excerpt of what I see on my system, showing the compiler version and where it's picking up the libraries it needs:

gcc version 4.3.0 20080125 (experimental) (GCC)

[snip]

/usr/local/libexec/gcc/i386-apple-darwin9.1.0/4.3.0/collect2 -dynamic -arch i386 -macosx_version_min 10.5.7 -weak_reference_mismatches non-weak -o doloop -lcrt1.10.5.o -L/usr/local/lib/gcc/i386-apple-darwin9.1.0/4.3.0 -L/usr/local/lib/gcc/i386-apple-darwin9.1.0/4.3.0/../../.. /var/folders/1j/1j2pEOuQF1am3E31uMWaRk+++TI/-Tmp-//ccxjXLSs.o -lgfortranbegin -lgfortran -lgcc_s.10.5 -lgcc -lSystem

[snip]

Following you suggestion

Ok, I tried:


gfortran -v -o doloop doloop.f

Driving: gfortran -mmacosx-version-min=10.5.7 -v -o doloop doloop.f -lgfortranbegin -lgfortran -shared-libgcc

Using built-in specs.
Target: i386-apple-darwin9.7.0

Configured with: ./configure --enable-languages=c,c++,fortran
Thread model: posix
gcc version 4.4.1 20090623 (prerelease) (GCC)

COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'doloop' '-shared-libgcc' '-mtune=generic'

/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/f951 doloop.f -ffixed-form -fPIC -quiet -dumpbase doloop.f -mmacosx-version-min=10.5.7 -mtune=generic -auxbase doloop -version -fintrinsic-modules-path /usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/finclude -o /var/folders/N3/N3SGiz1IGlGp4R7DZ60b9++++TI/-Tmp-//ccxn8kYh.s

GNU Fortran (GCC) version 4.4.1 20090623 (prerelease) (i386-apple-darwin9.7.0)
compiled by GNU C version 4.4.1 20090623 (prerelease), GMP version 4.2.1, MPFR version 2.3.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
doloop.f:2.37:

IMPLICIT REAL*8(A-H,O-Z) do i = 1, 100 x=sin(dble(i)*
1
Error: Syntax error in IMPLICIT statement at (1)

and I also tried:


gfortran -v -o bd_br_2obs bd_br_2obs.f libnag.a

Driving: gfortran -mmacosx-version-min=10.5.7 -v -o bd_br_2obs bd_br_2obs.f libnag.a -lgfortranbegin -lgfortran -shared-libgcc

Using built-in specs.
Target: i386-apple-darwin9.7.0
Configured with: ./configure --enable-languages=c,c++,fortran
Thread model: posix
gcc version 4.4.1 20090623 (prerelease) (GCC)

COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'bd_br_2obs' '-shared-libgcc' '-mtune=generic'

/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/f951 bd_br_2obs.f -ffixed-form -fPIC -quiet -dumpbase bd_br_2obs.f -mmacosx-version-min=10.5.7 -mtune=generic -auxbase bd_br_2obs -version -fintrinsic-modules-path /usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/finclude -o /var/folders/N3/N3SGiz1IGlGp4R7DZ60b9++++TI/-Tmp-//ccOhEG0I.s
GNU Fortran (GCC) version 4.4.1 20090623 (prerelease) (i386-apple-darwin9.7.0)
compiled by GNU C version 4.4.1 20090623 (prerelease), GMP version 4.2.1, MPFR version 2.3.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'bd_br_2obs' '-shared-libgcc' '-mtune=generic'
as -arch i386 -force_cpusubtype_ALL -o /var/folders/N3/N3SGiz1IGlGp4R7DZ60b9++++TI/-Tmp-//ccXYiPFS.o /var/folders/N3/N3SGiz1IGlGp4R7DZ60b9++++TI/-Tmp-//ccOhEG0I.s
COMPILER_PATH=/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/libexec/gcc/i386-apple-darwin9.7.0/:/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/lib/gcc/i386-apple-darwin9.7.0/
LIBRARY_PATH=/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/../../../:/usr/lib/
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'bd_br_2obs' '-shared-libgcc' '-mtune=generic'
/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/collect2 -dynamic -arch i386 -macosx_version_min 10.5.7 -weak_reference_mismatches non-weak -o bd_br_2obs -lcrt1.10.5.o -L/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1 -L/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/../../.. /var/folders/N3/N3SGiz1IGlGp4R7DZ60b9++++TI/-Tmp-//ccXYiPFS.o libnag.a -lgfortranbegin -lgfortran -lgcc_s.10.5 -lgcc -lSystem
Undefined symbols:
"_MAIN__", referenced from:
_main in libgfortranbegin.a(fmain.o)
ld: symbol(s) not found
collect2: ld returned 1 exit status

Is it giving you any clues?
The first program 'doloop.f' should run, since it is running on my home-Mac!

thanks for all your efforts...!!!

interesting

I checked the paths...

/usr/local/libexec/gcc/i386-apple-darwin9.7.0/...

that doesn't exsist...!
Instead, I have under .../libexec/gcc/:


darwin/
i686-apple-darwin9/
powerpc-apple-darwin9/

I also found under .../lib/gcc/

i686-apple-darwin9/
powerpc-apple-darwin9/

So I guess there is a library somewhere, which refers to a place which doesn't exists!?

BUT, I found i386-apple-darwin9.7.0 here:

/opt/local/var/macports/build/_opt_local_var_macports_sources_www.macports.org_files_ports_lang_gcc44/work/build/i386-apple-darwin9.7.0

I have not really an idea, but it looks like really messed up...

For comparison

Hi Marcel -

I installed the same version of the compiler that you have. It compiles correctly, and I've appended the output of "gfortran -v -o doloop doloop.f" below.

I'd suggest you try re-installing gfortran 4.4.1, and then remove your changes to your LD_LIBRARY_PATH (as an aside, where Linux uses LD_LIBRARY_PATH, Mac OS X instead uses DYLD_LIBRARY_PATH, so you shouldn't set LD_LIBRARY_PATH).

The version of gfortran you found in /opt/local/var/macports/build indicates you installed the macports gfortran at some point, even if you've disabled or removed it. So you might try temporarily renoving /opt/local/bin from your path, and ensure that you're running the version of gfortran you expect (i.e. /usr/local/bin/gfortran).

Here's the output of the verbose compile:

$ gfortran -v -o doloop doloop.f
Driving: gfortran -mmacosx-version-min=10.5.7 -v -o doloop doloop.f -lgfortranbegin -lgfortran -shared-libgcc
Using built-in specs.
Target: i386-apple-darwin9.7.0
Configured with: ./configure --enable-languages=fortran
Thread model: posix
gcc version 4.4.1 20090623 (prerelease) (GCC)
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'doloop' '-shared-libgcc' '-mtune=generic'
/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/f951 doloop.f -ffixed-form -fPIC -quiet -dumpbase doloop.f -mmacosx-version-min=10.5.7 -mtune=generic -auxbase doloop -version -fintrinsic-modules-path /usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/finclude -o /var/folders/1j/1j2pEOuQF1am3E31uMWaRk+++TI/-Tmp-//cc0kCbtl.s
GNU Fortran (GCC) version 4.4.1 20090623 (prerelease) (i386-apple-darwin9.7.0)
compiled by GNU C version 4.4.1 20090623 (prerelease), GMP version 4.2.1, MPFR version 2.3.0.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'doloop' '-shared-libgcc' '-mtune=generic'
as -arch i386 -force_cpusubtype_ALL -o /var/folders/1j/1j2pEOuQF1am3E31uMWaRk+++TI/-Tmp-//cc0uq0DK.o /var/folders/1j/1j2pEOuQF1am3E31uMWaRk+++TI/-Tmp-//cc0kCbtl.s
COMPILER_PATH=/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/libexec/gcc/i386-apple-darwin9.7.0/:/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/lib/gcc/i386-apple-darwin9.7.0/
LIBRARY_PATH=/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/:/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/../../../:/usr/lib/
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'doloop' '-shared-libgcc' '-mtune=generic'
/usr/local/libexec/gcc/i386-apple-darwin9.7.0/4.4.1/collect2 -dynamic -arch i386 -macosx_version_min 10.5.7 -weak_reference_mismatches non-weak -o doloop -lcrt1.10.5.o -L/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1 -L/usr/local/lib/gcc/i386-apple-darwin9.7.0/4.4.1/../../.. /var/folders/1j/1j2pEOuQF1am3E31uMWaRk+++TI/-Tmp-//cc0uq0DK.o -lgfortranbegin -lgfortran -lgcc_s.10.5 -lgcc -lSystem
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.5.7' '-v' '-o' 'doloop' '-shared-libgcc' '-mtune=generic'

I will try it.

Sorry for the late answer. In the moment I am to busy to try it. But I will do as you write.

I really appreciate all your help!

"****************************

"****************************************************
IMPLICIT REAL*8(A-H,O-Z)
do i = 1, 100
x=sin(dble(i)*0.1)
write(6,*)i,x
enddo
end
****************************************************

When I try to compile I get:

>Marcel$ gfortran doloop.f
>Undefined symbols:
> "_MAIN__", referenced from:
> _main in libgfortranbegin.a(fmain.o)
>ld: symbol(s) not found"

This refers to your main program, there is a mistake in your main program.
Or you may have forgotten to write that this was your main program at the beginning.
I had the same issues, i also named my file *.f90_something which also gives the above error, and it complains about the file not being the right architecture.
my suggestion on your above doloop.f:
****************************************************
program doloop

IMPLICIT REAL*8(A-H,O-Z)
do i = 1, 100
x=sin(dble(i)*0.1)
write(6,*)i,x
enddo
end
*****************************************************
might be further compiler issues if this doesn't work.
as a last resort, try change the extension from .f to .f90 (because it looks like you are trying to compile fortran 90 code with the f77 compiler of gfortran).

Check for ^M and of course columns

I had the same error message with a fortran program and discovered that the editor I was using at the time (XCode) was inserting the dreaded ^M (windows/dos format carriage return)..to remove them I opened up the file in BBEdit and saved as a UNIX file.

Also, I sometimes forget the comments in the first tab (I think 10 characters) rule when starting a new fortran program....just another possible thing to trip up.

As did the author of the original thread, I played the game of installing/compiling all the gcc/gfortran etc. A userful tool to see where gfortran lives is "#which gfortran" , the output should show the path where that lives...

The macports was a disaster (Id much rather deal with compiling gcc/gfortran on my own than deal with macports, my opinion)

Undefined symbols: gfortran

Hi all: I have followed the responses to Marcel's problem because it looks like mine. I've used Fortran77/95/etc on many other platforms but this is my first encounter with a Unix-based system and gfortran. I'm compiling a 2-line test program:
write(6, '(A)') "hello"
end

The gfortran output is:

charles-crawfords-macbook-2:src charlescrawford$ gfortran -o test test.f
/usr/bin/ld: warning unknown -macosx_version_min parameter value: 10.5.8 ignored (using 10.4)
/usr/bin/ld: can't locate file for: -lcrt1.10.5.o
collect2: ld returned 1 exit status
charles-crawfords-macbook-2:src charlescrawford$

The two warning/error comments make me think I have version problems. As far as I can tell there is no file whose name matches "*crt1*" anywhere in /usr. The gfortran I have comes from a downloaded file "gfortran-4.3-jan08.mpkg.zip". It is not the first gfortran I have installed,but I think I have uninstalled previous version ones. I am running OS X v. 10.5.8.

Thanks in advance for any help

Chuck

THANKS A LOT!

I finally got it. After I learned a lot of how to install gfortran and link it with other languages, I finally recognized that the problem was exactly the first guess of "apollo6375".

(from apollo6375)
I had the same error message with a fortran program and discovered that the editor I was using at the time (XCode) was inserting the dreaded ^M (windows/dos format carriage return)..to remove them I opened up the file in BBEdit and saved as a UNIX file.

I couldn't see the ^M, but I recogniced that when I push an extra "enter" after the first program line, the error message changed. I even got it compiled, but could still not execute the program. Therefore, I tried it as above explained...

Thanks a lot...