Xcode Tutorial: Fortran only projects

There have been a few questions about using the Xcode IDE gfortran plugin. Most have been with regard to using it for Fortran only projects. To answer the general question of whether it can be used that way, the answer is yes. In this mini-tutorial I'll show you how.

Start up by firing up Xcode and create a New Project (Command line utility, standard tool).

Create a project called Test and save it on the Desktop (you can really call it whatever you want and save it anywhere though).

In the Source group, click on main.c and hit the delete key. Click on "Delete References and Files".

Create a new file (File...New File).

Then scroll down and in the Gfortran section, select the Gfortran file type.

Remove the code that is there and replace it with the following:


       program test        
       
	   write(*,*)'Hello World'
	   
       return
       end

Make sure ZeroLink is turned off in the build settings and click Build and Go. You should see something similar to the following:

That's it. You can add, import, modify just as you would with any other project. Note, that at the moment, the plugin takes all of the fortran compiler options in a setting called: OTHER_FORTRAN_FLAGS. This includes debug symbols, optimization settings and link flags.

You can download the project file here.

Comments

Comment viewing options

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

problems compiling

Dave,
thanks, this step is exactly what I was looking for. It compiles nicely now with the little hello world example.
However, there is a new problem... since I am new to Fortran, I can only guess, and it might have to do with different versions of Fortran. The code below compiles fine on my MS machine, but not under my new setup OSX, XCode, GFortran. Do you have any ideas why that might be?
Thanks,
Claus

The code:
---------

PROGRAM PascalDreieck

INTEGER :: basis(13)

CHARACTER :: fmt *10

DATA basis /13*1/, leerz /23/

WRITE (*,1111) basis(13), basis(12), basis(13)

1111 FORMAT(5X, 'PascalDreieck', 5X, I6/22X, I6, I5)

DO 20 i=12, 2, -1

DO j=i, 12

basis(j) = basis(j) + basis(j+1)

END DO

22 FORMAT ('(',I2, 'X,13I5)')

leerz = leerz - 2

WRITE(fmt, 22) leerz

WRITE(*, fmt) (basis(k), k=i-1, 13)

20 END DO

WRITE(*,*)

END PROGRAM PascalDreieck

The errors when I hit "build and go":
-------------------------------------

1111 FORMAT(5X, 'PascalDreieck', 5X, I6/22X, I6, I5)
1
Error: Unclassifiable statement at (1)

22 FORMAT ('(',I2, 'X,13I5)')
1
Error: Unclassifiable statement at (1)

20 END DO
1
Error: Unclassifiable statement at (1)

END PROGRAM PascalDreieck
1
Error: Expecting END DO statement at (1)

Re: problems compiling

Hi Claus,

I think the problem here is a formatting issue. I'm not sure about later Fortran, but for Fortran77 at least, all commands with the exception of control jump numbers and line continuations need to be inset to at least column 7. So for example:

123456789  <- column numbers
1111  FORMAT(5X, 'PascalDreieck', 5X, I6/22X, I6, I5)

Note that the F in FORMAT starts at column 7 (it's a command). But that the jump control numbers start in column 1.

You can turn on line gutters and column positions in Xcode -> Preferences ->Text Editing.

Regards,

Dave

formatting issues partly solved

Hi Dave,
you were right, it is a formatting issue. The code below works now. However, this setup seems weired:

1) I can NOT write anything into column 1
2) anything written that starts in column 2, automatically is green (i.e. a comment).

3) the jump controls now start in column 3
4) the actual code starts in column 9

Especially issues 1 and 2.
Is this an xcode specific thing or is this specified through your plug-in?
At any rate, I am very happy that I can compile fortran code on my mac! :-)

Cheers,
Claus

Note: this guestbook obviously gets rid of leading spaces, so the format of the following code is actually not what it is in xcode...

The code that works
-------------------

PROGRAM PascalDreieck

INTEGER :: basis(13)

CHARACTER :: fmt *10

DATA basis /13*1/, leerz /23/

WRITE (*, 23) basis(13), basis(12), basis(13)

23 FORMAT(5X, 'PascalDreieck', 5X, I6/22X, I6, I5)

DO i=12, 2, -1

DO j=i, 12

basis(j) = basis(j) + basis(j+1)

END DO

22 FORMAT ('(',I2, 'X,13I5)')

leerz = leerz - 2

WRITE(fmt, 22) leerz

WRITE(*, fmt) (basis(k), k=i-1, 13)

END DO

WRITE(*,*)

END PROGRAM PascalDreieck

Re: formatting issues partly solved

Hi Claus,

Again, I'm not familiar with the Fortran90/95 standards, but column 1 is the comment column in Fortran 77. If this is no longer true in Fortran90/95 you may need to specify a flag in the compiler settings to tell the compiler to use the 90/95 syntax (or you may need to simply change the extension on the file to ,f90. You can do this by right clicking on the file in the Groups and Files pane and selecting rename, this will also change the syntax coloring to f90 style).

Regarding issue 2 (syntax coloring), you can control this either by changing the file name, or by by selecting Format > Syntax Coloring > Fortran 90 in Xcode.

For issues 3 and 4, this might be due to the way Xcode autoindents. You can control the number of spaces that are inserted with the tab key in the Xcode preferences.

Also, as an aside, the fortran that is provided by our package installer, will also run on the command line and is installed in /usr/local/bin . I prefer using Xcode when I can, but sometimes it's easier to build from the command line. You can still edit your files as standalone files using Xcode, you just lose the project management capabilities.

Regarding the formatting on the website. You are correct, as raw HTML doesn't support multiple spaces. You can put preformatted text in these delimiters:

<pre>
  The text     with    the spacing I   want.
</pre>

Hope that helps,

Dave

*.f90 helped!

Hi Dave,
renaming the file to *.f90 solved the formatting issues. Doh!
One more question, regarding the IOSTAT flag on opening a file.
Is there a gFortran specific list what numbers are what errors (I get a 2)... couldn't find anything in google...

I really do appreciate your help Dave, I am hoping this is not getting common!

Thank you!
Claus

input files

Ok, I partly solved this myself:

Say the xcode project you’re working on is located at /Users/username/Documents

Say the xcode project is called “FileIO” Then an input file input.txt opened through the command

OPEN (UNIT = 3, FILE = filename, STATUS = 'OLD', ACTION = 'READ', &
IOSTAT = status)

needs to be stored in
/Users/username/Documents/FileIO/build/Debug

Otherwise an IOSTAT = 2 error occurs.

But I'd still be usefull to know what the error-codes are, and where they can be looked up... :-)

Cheers,
Claus

Re: input files

Hi Claus,

I'm not sure about the error codes, but you can change the default location where an executable appears to run at.

1) In the Files and Groups Pan click the disclosure triangle next to Executables
2) Click on the excutable of interest (if you have more than one target)
3) Click on the Info button

A new dialog will appear. One of the options is to change the current working directory.

This won't change where the executable is BUILT, but when Xcode does a Build and Go it will launch the executable as if it were launched at the path you specified.

Hope that helps,

Dave

Having Problems

I follow these directions exactly, and I get the following errors:

Building target “robCBTS” of project “robCBTS” with configuration “Release” — (1 error, 6 warnings)
Checking Dependencies cd /Users/rob/Desktop/robCBTS
/usr/bin/gcc-4.0 -o /Users/rob/Desktop/robCBTS/build/robCBTS.build/Release/robCBTS.build/Objects-normal/i386/robCBTS -L/Users/rob/Desktop/robCBTS/build/Release -F/Users/rob/Desktop/robCBTS/build/Release -filelist /Users/rob/Desktop/robCBTS/build/robCBTS.build/Release/robCBTS.build/Objects-normal/i386/robCBTS.LinkFileList -arch i386 -Wl,-Y,1455 -mmacosx-version-min=10.4 /usr/local/lib/libgfortran.a /usr/local/lib/gcc/powerpc-apple-darwin8.8.0/4.3.0/libgfortranbegin.a /usr/local/lib/gcc/powerpc-apple-darwin8.8.0/4.3.0/libgcc.a -isysroot /Developer/SDKs/MacOSX10.4u.sdk
/usr/bin/ld: warning -L: directory name (/Users/rob/Desktop/robCBTS/build/Release) does not exist
/usr/bin/ld: warning -F: directory name (/Users/rob/Desktop/robCBTS/build/Release) does not exist
/usr/bin/ld: warning /Users/rob/Desktop/robCBTS/build/robCBTS.build/Release/robCBTS.build/Objects-normal/i386/robCBTS.o cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (file not loaded)
/usr/bin/ld: warning /usr/local/lib/libgfortran.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: warning /usr/local/lib/gcc/powerpc-apple-darwin8.8.0/4.3.0/libgfortranbegin.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: warning /usr/local/lib/gcc/powerpc-apple-darwin8.8.0/4.3.0/libgcc.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: Undefined symbols:
_main
/Developer/SDKs/MacOSX10.4u.sdk/usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../crt1.o reference to undefined _main
collect2: ld returned 1 exit status
/usr/bin/ld: warning -L: directory name (/Users/rob/Desktop/robCBTS/build/Release) does not exist
/usr/bin/ld: warning -F: directory name (/Users/rob/Desktop/robCBTS/build/Release) does not exist
/usr/bin/ld: warning /Users/rob/Desktop/robCBTS/build/robCBTS.build/Release/robCBTS.build/Objects-normal/i386/robCBTS.o cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (file not loaded)
/usr/bin/ld: warning /usr/local/lib/libgfortran.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: warning /usr/local/lib/gcc/powerpc-apple-darwin8.8.0/4.3.0/libgfortranbegin.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: warning /usr/local/lib/gcc/powerpc-apple-darwin8.8.0/4.3.0/libgcc.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: Undefined symbols:
_main
/Developer/SDKs/MacOSX10.4u.sdk/usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../crt1.o reference to undefined _main
collect2: ld returned 1 exit status
Build failed (1 error, 6 warnings)

I am no expert, but it looks like it is trying to link with intel based files. I am on a DP G5, and I have installed the PPC version of Gfortran, and I just don't know what to do next. Any help is appreciated.

thanks,
rob

Re: Having Problems

The Xcode plugin doesn't build Universal binaries. When you set it to release, you didn't remove the flag for compiling the i386 code. In the Xcode project:

1) Expand the Targets group in Groups and Files

2) Click on the executable target (in the example above it will be called Test)

3) Click the Info button (or command-I)

4) Remove the i386 variable in the Build tab for Architectures.

Dave

Thanks so much. I was

Thanks so much. I was looking everywhere for this kind of thing, I just wasn't sure where it was or what needed to be changed. Worked like a charm!

rob

Compiling issues

Hello.
Thank you much for all of your help. Most everything worked solid. The only problem that I'm running in to is my program is successfully building, however I get a warning saying that there is no rule to process 'untitled.f'. I was wondering if you could help me out. Thank you

Sunny

fortran only - compiling with "include '*.h" header files

Hello.

I have not been able to successfully compile some legacy fortran 90 code that uses header files.

Example:

program test
include 'par.h'
...
...
end

where par.h is text file in local directory with fortran declarations such as:
integer, parameter::M=1, N=1
...
...

I suspect strongly that xcode is not treating the contents of the *.h file as fortran code... and that my issue is not using xcode/gfortran correctly.

I have changed the 'GetInfo' file type to 'sourcecode.fortran.f90' but apparently to no effect.

Also, I know that this code does compile in a commercial PC fortran package.

----

Any suggestions on how to do this correctly would be greatly appreciated.

Cheers.

missing rule

I have the same problem reported by Skarnarni in March (builds happily but doesn't produce an executable because it has no rule). On my target/info/rules page, my only Fortran choice is another Fortran compiler - which produces an error code 41 because that compiler really isn't there......

I found the fortran.xcbuildrules file in the application support area, but Xcode doesn't seem to be using it. Please help.

Thanks,
Therese

Pobleme compiling

Hello,

I'm just starting working on fortran and on mac.

I just instal the gfortran plugin and try the hello word test.
I don't succed to built,
I've got two warning and I error as:

Building target “Test” of project “Test” with configuration “Debug” — (1 error, 2 warnings)
cd /Users/n......./Documents/Test
/usr/bin/gcc-4.0 -o /Users/n......./Documents/Test/build/Debug/Test -L/Users/n......./Documents/Test/build/Debug -F/Users/n......./Documents/Test/build/Debug -filelist /Users/n......./Documents/Test/build/Test.build/Debug/Test.build/Objects-normal/i386/Test.LinkFileList -arch i386 -Wl,-Y,1455 -mmacosx-version-min=10.4 /usr/local/lib/libgfortran.a /usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0//libgfortranbegin.a /usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0/libgcc.a -isysroot /Developer/SDKs/MacOSX10.4u.sdk
/usr/bin/ld: warning /Users/n......./Documents/Test/build/Test.build/Debug/Test.build/Objects-normal/i386/hello.o cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (file not loaded)
/usr/bin/ld: warning /usr/local/lib/libgfortran.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: Undefined symbols:
_MAIN__
__gfortran_set_args
__gfortran_store_exe_path
/usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0//libgfortranbegin.a(fmain.o) reference to undefined _MAIN__
/usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0//libgfortranbegin.a(fmain.o) reference to undefined __gfortran_set_args
/usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0//libgfortranbegin.a(fmain.o) reference to undefined __gfortran_store_exe_path
collect2: ld returned 1 exit status
/usr/bin/ld: warning /Users/n......./Documents/Test/build/Test.build/Debug/Test.build/Objects-normal/i386/hello.o cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (file not loaded)
/usr/bin/ld: warning /usr/local/lib/libgfortran.a archive's cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (can't load from it)
/usr/bin/ld: Undefined symbols:
_MAIN__
__gfortran_set_args
__gfortran_store_exe_path
/usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0//libgfortranbegin.a(fmain.o) reference to undefined _MAIN__
/usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0//libgfortranbegin.a(fmain.o) reference to undefined __gfortran_set_args
/usr/local/lib/gcc/i386-apple-darwin8.10.1/4.3.0//libgfortranbegin.a(fmain.o) reference to undefined __gfortran_store_exe_path
collect2: ld returned 1 exit status
Build failed (1 error, 2 warnings)

Is it a problem of plugin instalation or it's my prog??

than you

nils

It somehow looks like you're

It somehow looks like you're trying to build for PPC with a i386 compiler.

but I have a macbook wich is

but I have a macbook wich is intel and not ppc.
how to change ?
thanks

Default Fortran Compiler

How do I reset the default fortran compiler for XCode v. 3.0 (leopard)? I installed gfortran but now I want to use Intel fortran (ifort) instead.

Debra Baker
Department of Atmospheric
and Oceanic Sciences
University of Maryland

Building errors

I am a relative newbie attempting to compile simple Fortran programs on my Macbook Pro. I am running OS 10.4.11 with the Xcode 2.2.1 that came with my machine.

I have an old-ish version of the gfortran plug-in running (im not sure which one is compatible with Xcode 2.2 - or whether it would help if I downloaded Xcode 2.5 (2.4 is no longer available)).

When trying the above tutorial I get an error "No launchable executable at path. (PROJECT_DIR)/test/build/Debug/test"

I also get an error in build results - "warning: no rule to process '$(PROJECT_DIR)/test.f'"

When I try to add a new compiler rule for Fortran files i pick "FORTRAN files" from the "Process" drop down menu - then when I look in the "using compiler" drop down I can't find anything to do with fortran or gfortran - should I pick one of the GCC options?

I would really appreciate any tips.

Thanks

Jack Snape

Re: Building errors

Hi Jack,

If you don't see an option for using gfortran when try to associate FORTRAN files, then the plugin didn't register with Xcode. If you are on Tiger, you'll have to use Xcode 2.4. If you are on Leopard, then you need Xcode 3.0. I don't have Xcode 2.5 or Tiger machines anymore, so I can't test the current plugin with Xcode 2.5, but I'm pretty sure it doesn't work. You may be able to use the Xcode 3.0 plugin that was recently released with Xcode 2.5, but I haven't tried that.

Regards,

Dave

Thanks Dave, I think I'll

Thanks Dave, I think I'll try and find a copy of Xcode 2.4

Regards

Jack

The gfortran plugin is not registered with Xcode 3.0

Hi there,

I could use some insight on a problem I have. I have an Intel iMac with Leopard plus Xcode 3.0. I installed the "Jan 8 gfortran build" that the installer declared was successful. I can run gfortran from the command line but my problem is that Xcode 3.0 does not show a gfortran option. There is a fortran option but no gfortran option. I am following the tutorial "Xcode Tutorial: Fortran only projects".

Other info: I installed gfortran on a non-admin account plus I installed gfortran on the admin account. Same problem.

I looked into Library/Application Support/Xcode/Plug-ins and only see "FortranPlgin.pbplugin".

gfortran installed into usr/local/bin.

Is there any further steps I can try to get Xcode to register the gfortran plugin?

Thanks in advance.

Don

same problem + mtune tip

I have the same problem as Don: 1) the Gfortran file type is not available (I have "Fortran-> TemplateInfo, unnamed" types available; looks like "unnamed" is the valid .f type). 2) When I try to compile a basic program for the new Penryn MBP I get an error about unrecognized mtune architecture (plugin tries to compile with "-mtune G5" even though its an intel proc). 3) Looking past the mtune problem, I get an "Undefined reference to _main" error during the linking phase (if I delete the C file and try to compile a pure F90 project).

First, that mtune problem is interesting because I couldnt get past it until I edited /Library/Application Support/Developer/Xcode/Plugins/FortranPlugin.pbplugin/Contents/Resources/fortran.pbcompspec and replace the occurrence of "-mtune=${0}" with "-mtune=generic" or "-mtune=i686". I still dont see why the spec is failing (its limited to powerpc archs only).

Second, the linking problem is due to Xcode attempting to use gcc to link all of the .o files into a command line utility. If the plugin took advantage of the "RequiredLinker" spec in fortran.pbcompspec it could specify the use of gfortran to link the .o's. Alternatively, some people would have luck if they create a symbolic link from their libgfortranbegin.a (specific to the version of gfortran used by Xcode) to the /usr/local/lib/libgfortranbegin.a

I vote for an update to this plugin AND the documentation. If you provide source for the latest build we could help proceed.

-B

gfortran/xcode/MacOS X versions.... it is a mess

I've been looking around on this and other sites for a while, trying to get gfortran to run with xcode 3.0 on Mac OS X 10.5.5. I have not been able to get it to work... used the installer found here (Jan 08). Downloaded the test project above, it doesn't compile (even though gfortran in /usr/local/bin
I wish someone with a better understanding of the issues could summarize this as an article and not have it spread over many pages in responses to replies... maybe it is time for a wiki page for the gfortran/xcode effort that offers updated instructions for the different configurations of platform (intel/ppc), os version, gfortran version and xcode version in ONE place.

Thanks
HJ

Same problems

I have the same problems as Jack and HJ, and I've tried uninstall/reinstall several times to no avail. I upgraded to xCode 3.1.2 and this did not solve the problem. I can run gfortran in terminal and it compiles my raw text files fine, but now I get a warning every time I compile that says:

ld warning: duplicate dylib /usr/local/lib/libgcc_s.1.dylib

Which is no doubt because of my uninstall/reinstall fiasco. There are lots of dylib files.. Which ones are ok to delete, and, any word on when the plugin will work with xCode 3.x?

Fortran only projects

I did exactly what dgohara described and additional inserted libgfortran.a under "Source" (I have a scilab installed gfortran in my Intel iMac Mac OS X 10.5.6 and Xcode 3.1.2), compilation works but does not execute.

When downloading the readily packaged Test.zip, the same thing happens- I end up with "Failed x1".

What can I do now?

hn

The gfortran plugin is not registered with Xcode 3.0

I have the same problem at 10.5.6 with Xcode 3.1.2 using the package installer found elsewhere on this site. Gfortran installed okay and the FortranPlgin.pbplugin shows up in Library/Application Support/Xcode/Plug-ins, but there is no Fortran option in Xcode anywhere regardless of what I try.

Any advice would be appreciated. I've exhausted all other options.

Thanks,
jamey

plug in Fixed! (sorta)

Go here for a fix to the plugin to allow it to function with xcode 3.1.3: http://www.macresearch.org/fortran-plugin-xcode-31#comment-15419

Hi I have a written Fortran

Hi
I have a written Fortran code which has one main program in one file and couple of subroutines in different files, now I want to use X code to debug my program. How I can use X code to do this? For one simple code I can use X code, but I don't know how to add subroutines to that.

thanks

This did not work for me.

This did not work for me. The Build failed, as follows:
error: can't exec '/Developer/Library/Xcode/Plug-ins/CoreBuildTasks.xcplugin/Contents/Resources/gcc' (No such file or directory)

I cannot seem to find a gdb that works with gfortran. That is my goal. But since the Intel Mac, there are numerous gfortran compilers, but no debugger that works. Sigh.

-Tony