Fortran and Xcode

Discussion in 'Mac Programming' started by jasnw, Apr 6, 2015.

  1. jasnw macrumors 6502a


    Nov 15, 2013
    Seattle Area (NOT! Microsoft)
    I'm in the process of moving from Linux to OS X and I've got 40+ years of Fortran code that I'll be wanting to compile/link/run. I've got gfortran installed (Mavericks) and have it working in Terminal (although debugging tools are a bit on the lousy side), and am thinking maybe I'd like to be using Xcode 6 as a way forward with these codes. I know lots about Fortran, but zero about Xcode. How do I go about using Xcode with gfortran in such a way that I can make use of Xcode debugging, and perhaps downstream putting Swift-based GUI front-ends on my Fortran back-end codes.

    If gfortran isn't the best choice of Fortran compiler for use with Xcode, what is?
  2. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    I guess theoretically XCode could be taught what to do with Fortran files, but the support would be limited. I just looked and I didn't see any options in the menu or preferences that looked like there would be a way to teach XCode what to do with Fortran files. I'm guessing it would take a deeper understanding of how XCode works to add that support.

    Maybe someone could find a way to get XCode to deal with Fortran files, but I'm afraid you'd be fighting at uphill battle. Whenever XCode is updated you run a risk of the mods not working any more.

    You'd probably be better off making a compiled binary library with your Fortran files external to XCode using Makefiles or whatever build system you want and then adding your binary library to a Swift/Obj-C project and calling the routines in the binary library.

    Remember that when calling Fortran from C that all parameters to Fortran functions are by reference whereas parameters to C functions are by value.
  3. Senor Cuete macrumors 6502

    Nov 9, 2011
    No, a C function can require parameters to be either reference or value.
  4. jasnw thread starter macrumors 6502a


    Nov 15, 2013
    Seattle Area (NOT! Microsoft)
    RE C-vrs-Fortran argument passing. I don't know anything about Swift - does it allow for either by-value or by-reference argument passing?
  5. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Xcode is nothing but a huge pile of plugins from Apple. The system isn't officially documented (at least not publically), and the unofficial documentation is extremely spotty/incomplete. I started on an Xcode plugin a few years ago - it still works as well as ever (which is to say, not much works) in Xcode 5... I assume it works in Xcode 6, too.

    Anyways, writing the plugin would require Obj-C. And honestly, it would probably be vastly easier to just write an IDE from scratch for Fortran.
  6. jasnw thread starter macrumors 6502a


    Nov 15, 2013
    Seattle Area (NOT! Microsoft)
    After googling around on this issue I think you're probably right. Keep the Fortran work outside Xcode and only use Xcode for the Swift parts.
  7. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    I'm fairly sure Xcode still has Build Rules. Here's a page on it at Apple, but it's not useful in any practical sense: a Build Rule.html

    Searching for xcode build rule finds other articles, but their dates suggest they haven't been revised in a while. Example from 2012:

    and one from 2013:

    If recent Xcode versions still have Build Rules at all, they should work pretty much the same as they did in the past.

    The gist of it is that Xcode acts sort of like 'make' when it comes to building. It has a number of "things to build" (targets). It also has a list of "things needed by the fully built target". For make, that list is explicit (the OBJECTS files); in Xcode it's implicit (based on a file being used by the target). Both 'make' and Xcode also have sets of rules for transforming files, for example, to transform a .c file into a .o file, the rule consists of the input suffix (.c), the output suffix (.o), and a command that produces the output from the input (cc -o OUT.o IN.c).

    Both 'make' and Xcode let you add rules. I know older versions of Xcode let you do this, because I use that feature often. I don't know for sure if newer Xcode versions have this, nor do I know where one would look to add or modify the build rules. That's where finding a recent article will be useful. The above article from 2012 may give enough clues to find it, in whatever Xcode version the OP is using.

    Once the "Where do I put a build rule?" question is answered, the next question is what to put into the rule. The first thing is going to be the suffixes: .f or .for for the input, and .o for the output. Next is the transformation action (command). That's usually going to be a command-line, such as:
    Once a rule is defined, then when Xcode sees a .for file (or .f, or whatever) it will know to run the command in the rule, using the specific files involved.

    There will always be some tricky stuff, like ensuring the output file gets put into the proper place (the Build folder). Consult the Xcode reference on Build Rules, Build Variables, and don't be afraid to look at a verbose build log, or the log produced by running 'make'. Lots of details can be found there.

    The last step will be linking all the .o files into a complete executable. For that, a custom Run Script build step might be useful. It can contain something that looks similar to however the executable is built without Xcode. That is, if 'make' or a shell script runs the 'ld' command with a bunch of options, then do the same thing in the Xcode build step.

    I've used past Xcode versions with custom build rules that:
    • Compile & download Atmel AVR code for Arduino boards.
    • Run an 8051-family assembler for a custom-designed board.
    • Process HTML, CSS, & JavaScript for embedding into a ConnectOne wifi module.
    These were all done for various different projects, and when I moved to the next project I'd generally start with the project that most resembled my new one, and then tweak build rules, linker phases, etc. None of this required an Xcode plugin, just Build Rules and Run Script build phases.

    I'm still using older Xcode versions, so I can't say if recent versions still have Build Rules, Build Phases, and so on. If they do, then the above is a general guide. You'll still have to navigate Xcode to access and define build rules, add phases, and so on. You'll also need to know about Build Variables, which hold important things like the name of the file to compile, the Build folder for objects, and the options set by the user.

    The last point I'll mention is this: "Start Simple". That is, start with a single-file Fortran program, that needs only the Fortran intrinsic functions and libraries. It will be much simpler to develop build rules, linker phases, etc. on a trivial program than on the final multi-file one.

    Once the single-file program is working, add one file to the project. Make sure it still builds and runs correctly. At that point you can move to more complex test cases, or even the actual program you wanted. The key is to start simple and work up, rather than jumping directly into the deep end.

    Worst case there's always 'make'. It may be primitive and repetitive, but it does work, and there are tutorials for it.
  8. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    My recommendation would be to build a compatability layer between your fortran and Objective-C in C. I'd gun for a static or dynamic library (.a or .so). From there, I'd take a .h and your library and add this as a dependency in Xcode. You can easily call the C code from Objective-C. You can probably go directly, but it seems like a clean C API could save headaches.

    Basically: setup your library external to Xcode, including a debug compiled version. Build your Objective-C in Xcode against this library. Doing your fortran directly sounds like a very onerous process likely to waste a lot of time.

    I can't comment on Swift. I would be inclined to setup that interface in Objective-C unless calling C from swift is trivial.

    I never took it to production, but I did some proof of concept projects to jump between Fortran and Java and Fortran and C# using C glue. It seems feasible here. Good luck.

  9. RightHalfPlane macrumors newbie

    Mar 9, 2015
    Intel Fortran for Mac OSX

    We used the Intel Fortran on the Macintosh a few years ago. It worked with xcode and the programs ran nearly twice as fast when compared to when they were compiled with gfortran. I think that it is still available - perhaps you should give it a try. Currently, we are using Intel Fortran on Linux and it is still generates code that is twice as fast as gfortran.
  10. AlliePallie macrumors newbie

    Feb 27, 2015
    Linux in OS-X

    Pardon my innocence;t has been a long, long time since I did any programming. However, IIRC, Linux will run under Parallels. So there seems to be no reason why you cannot continue your fortran programming in Linux running on your Mac.
  11. macrumors newbie

    Jul 26, 2012
    Hi Allie, I believe the OP is intending to build an executable that runs on Mac OS X. While Linux under Parallels is a possible, if clunky, solution for debugging of code, it doesn't address building and deploying on Mac OS X.
  12. jasnw thread starter macrumors 6502a


    Nov 15, 2013
    Seattle Area (NOT! Microsoft)
    You are correct, and I've been running a Linux VM using VMWare Fusion for several years. However, that ends up giving me two platforms to keep current rather than just one. This is a move to simplify my life if possible.

    Also, this falls a little into the "if you're not learning you're dying" category. I can continue along running on Linux using vi and make as my "IDE" running everything on the command line, or I can learn some new stuff that could (a) make my life a little easier in the long run and (b) be more fun (I hope!). If the frustration factor gets too high, however, it's over the side with Xcode and I'll look for other, non-Apple, solutions.
  13. AustinZ macrumors member


    Aug 6, 2008
    The Xcode and Swift Apple developer forums might be worth posting topics to. In particular, there are compiler engineers who frequent the Swift forum and sometimes reply to questions.

    Swift's FFI only really supports C and Objective-C right now (not C++), and the former only to a limited extent (for example, you can pass C function pointers around in Swift code, but you can't invoke the functions themselves). Like Lee suggested, I would recommend seeing if you can leverage C-Fortran interoperability to first build an Objective-C library that exposes an API for your Fortran code (see this old Stack Overflow topic). Once you have that working, importing the Objective-C library into Swift and building your frontend there should be relatively trivial.
  14. jasnw thread starter macrumors 6502a


    Nov 15, 2013
    Seattle Area (NOT! Microsoft)
    Thanks for the suggestions. I think my first approach will follow what I did in a project several years ago when I was developing a system to access legacy databases and software (models) over the Internet using Java. I'll use Swift for an interface GUI that builds input files for the Fortran code I want to run and then constructs displays (tables, plots, maps) of the results. Sloppier than compiling together, but makes compiler and code-level interface issues go away.
  15. Senor Cuete macrumors 6502

    Nov 9, 2011
    Fortran to C

    You could use one of several available converters to convert your code to C.
  16. p.zhao macrumors newbie

    Sep 30, 2015
    I started installing intel fortran but it did not work at all: I downloaded the dmg file, double clicked it but nothing happened. I am using El Capitan Beta. Could you help me on this? Thank you.

    Attached Files:

  17. RightHalfPlane macrumors newbie

    Mar 9, 2015

    Have you tied it now that real El Capitan is out ?
  18. LarryJW Suspended

    Oct 29, 2015
    Though not directly related to your question, for a VM, I used Oracles open source VirtualBox. I stopped using Parallels and VMWare Fusion because they monopolized my Mac. VirtualBox doesn't have that problem.

Share This Page