来自网络

 

1. Make sure you have installed a fortran compiler (I'm using gfortran version 4.3.0, installed by MacPorts).

2. Start a new project in Xcode using the c++ command line tool.
Under "Choose a template for your new project:" select Application > Command Line Tool. In the drop-down-menu, select "C++ stdc++".

3. Select the destination folder for the project and name it.

4. Select Debug mode. In the Groups and files pane, click the Source folder triangle to view the documents you will be compiling. Right now, there should be one main.cpp file and no others.

5. Add a fortran file to the source folder: First, click select the source folder in the Groups & Files pane. Now select File > New File and a window will appear so you can select the file template. Under the Mac OS X icon, select "Other" and choose "Empty File" before clicking "Next". When you name the new file, attach the fortran suffix, ".f" to the end so the file will be recognized as a fortran file during the build phase. In the projects window, make sure the new fortran file is in the Source folder.

6. Now edit the files.
%% Here is a very basic sample code to run:
%% main.cpp
#include
using namespace std;

extern "C"
{
void ffunction_(float *a, float*b);
}

int main ()
{
cout << "Hello, World!\n";

float a = 1.0, b = 2.0;

cout << "Before running Fortran function:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

ffunction_(&a,&b);

cout << "After running Fortran function:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

return 0;
}

%% The fortran file, fortran_test.f

subroutine ffunction(a,b)
a = 3.0
b = 4.0
end
7. Now, in the Groups & Files pane, click the triangle to the left of Targets and select the target icon. Right-click it and select "Get Info", or just click the info icon at the top of the project window. Select the "Rules" tab.

8. One last thing. You need to know the pathname for your copy of gfortran. Open a terminal window and type: "where gfortran" The pathname will be on the next line. My gfortran is /usr/local/bin/gfortran

9. Click the "+" at the lower left of the rules panel to add a new rule to your build to handle fortran files. Choose:
Process: "Fortran source files"
using: "Custom script:"
In the new typing field, type the following:
/usr/local/bin/gfortran -g -c ${PROJECT_DIR}/${INPUT_FILE_NAME} -o ${TARGET_BUILD_DIR}/${INPUT_FILE_BASE}.o
%% Replace "/usr/local/bin/gfortran" with your own gfortran
%% pathname if needed.
In the "with output files:" field, type:
$(TARGET_BUILD_DIR)/$(INPUT_FILE_BASE).o
%% the custom script line needs curly braces, { }, because it talks
%% to the command line. The output files line is telling Xcode where
%% the output file is, and Xcode uses regular parentheses ( ).

10. Now, everything should be ready. Click "Build and Run" or "Build and Debug". If it compiled successfully, go to the Groups & Files pane, open the products folder by clicking the triangle and double-click the executable displayed there.

用户登录