Fortran to C++ converter

I have a FORTRAN code.

I want to convert it into a C++ code. I'm trying to investigate whether it is possible to have a converter or rewrite the whole code which is a huge exercise.

Can anyone tell me what is the ebst way to go about? The code is modular-I'd say most of it.

I did see through google of feeble but not yet tried.If anyone has done such an exercise, it would be useful.

Most of FORTRAN code is pure math calculation oriented.

Any tips would be useful.

Yes- the FORTRAN code has several external library functions which it uses.

Jerome
There are fortran compilers available from most vendors (GNU, Intel, Sun, etc) and the objects they compile can be linked together with C++ objects. No need to rewrite your existing code, just call it directly from C++ (or call C++ from Fotran, if you remember extern "C")
Can you provide some example? I'm trying to take a small piece of code from here and do a prototype.

If you could just elaborate slightly of what exactly needs to be done, shall be grateful.

JErome
Examples of what exactly? Calling fortran from C++ and back?

test.cc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
extern "C" {
    int ffunc_(int*, int*);
    int cppfunc_(int* n1, int* n2) {
        std::cout << " Inside C++, args = " << *n1 << ", " << *n2 << '\n';
        return *n1 + *n2;
    }
}

int main()
{
    int n1 = 1;
    int n2 = 2;
    std::cout << "Calling from C++ to Fortran, arguments: " << n1 << ", " << n2 << '\n';
    int r = ffunc_(&n1, &n2);
    std::cout << "THe return value from fortran was " << std::hex << r << '\n';
}


test.f:
1
2
3
4
5
6
7
8
      function ffunc(n1, n2)
      integer ffunc,cppfunc,cppr
      write(*,*) 'Inside Fortran, args= ',n1,',',n2
      cppr = cppfunc(n1, n2)
      write(*,*) 'The return value from C++ was ',cppr
      ffunc = n1+n2
      return
      end


$ g++ -O3 -W -Wall -Wextra -pedantic -o test test.cc test.f -lgfortran
$ ./test
Calling from C++ to Fortran, arguments: 1, 2
 Inside Fortran, args=            1 ,           2
 Inside C++, args = 1, 2
 The return value from C++ was            3
THe return value from fortran was 3


Just be mindful of the calling conventions (e.g. strings are passed as a pointer and a length), array indexing (1-based vs 0-based), array layout (column-major vs row-major), string layout (space-padded vs. zero-terminated)
Last edited on
There exists some Fortran to C converter like "f2c". They're really old stuff. I've never tried them out but maybe you will give them a try.
Cubbi,

Sorry to ask such a question.

Can I craete this code in Visual Studo 8 and call C++, fortarn and vice versa?
Cubbi,

I craeted a .cpp file and a .F file.

Created a Win 32 blank project but it doees not allow me to add files with .F extension.

Can you tell me a way to go about this?
As far as I know (I don't use Windows so I can't be sure), Microsoft doesn't ship a Fortran compiler with Visual Studio. Your options are to install Intel Visual Fortran or another plug-in compiler, or leave Visual Studio environment and use GCC.

http://stackoverflow.com/questions/5617236/visual-studio-and-fortran-working-together

http://social.msdn.microsoft.com/Forums/en-US/vssetup/thread/2f3480d2-ff6c-4f15-8245-0a155cc1cdc1/

or, indeed, try f2c, if your fortran source is sufficiently old.
Last edited on
I do have a FORTRAN compiler installed separately and I do use fortran projects in Visual studio environment.Can you sugegst soemthign in PRoject settings?
Can't help there.. I use Fortran with C++ on IBM, Sun, and Linux platforms.
Thanks Cubbi it works....

Does calling FORTRAN from C++ create any problems when we have namespaaces defined in a C++ project?
Cubbi-it works with the FORTRAN compiler installed.

IS there any problem caused when we call FORTRAN from C++ if 'namespace' is defined in the code?

I have a code template defined through a wizard and have put in the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int calculator::apply_cb()
{
    int errorCode = 0;
    try
    {
		BlockStyler::PropertyList *number1Properties = number1->GetProperties();
		BlockStyler::PropertyList *number2Properties = number2->GetProperties();
		BlockStyler::PropertyList *number3Properties = number3->GetProperties();

		int number1Value = number1Properties->GetInteger("Value");
		int number2Value = number2Properties->GetInteger("Value");

		int number3Value = number1Value + number2Value;

		/*int number3Value = ffunc_(number1Value,number2Value);

		number3Properties->SetInteger("Value",number3Value);*/


    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        calculator::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}


The moment I uncomment

/*int number3Value = ffunc_(number1Value,number2Value);

number3Properties->SetInteger("Value",number3Value);*/

I get an error .

The project has extern C defiend multiple times as well as has namespace defined
That sounds like an error on the C++ side of things, but there isn't enough information to diagnose it. Regarding namespaces, the only relevant rule is that an extern "C" function with any given name can only be defined in one namespace.
There are sveral extern C generate by wizard as below:

extern "C" DllExport int ufusr_ask_unload()
{
//return (int)Session::LibraryUnloadOptionExplicitly;
return (int)Session::LibraryUnloadOptionImmediately;
//return (int)Session::LibraryUnloadOptionAtTermination;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//------------------------------------------------------------------------------
[code]// Following method cleanup any housekeeping chores that may be needed.
// This method is automatically called by NX.
//------------------------------------------------------------------------------
extern "C" DllExport void ufusr_cleanup(void)
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        calculator::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}


extern "C" DllExport int ufusr_ask_unload()
{
//return (int)Session::LibraryUnloadOptionExplicitly;
return (int)Session::LibraryUnloadOptionImmediately;
//return (int)Session::LibraryUnloadOptionAtTermination;
}[/code]

I added the following;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
extern "C"
{
	int ffunc_(int&,int&);
}

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
int calculator::apply_cb()
{
    int errorCode = 0;
    try
    {
		BlockStyler::PropertyList *number1Properties = number1->GetProperties();
		BlockStyler::PropertyList *number2Properties = number2->GetProperties();
		BlockStyler::PropertyList *number3Properties = number3->GetProperties();

		int number1Value = number1Properties->GetInteger("Value");
		int number2Value = number2Properties->GetInteger("Value");

		//int number3Value = number1Value + number2Value;

		int number3Value = ffunc_(number1Value,number2Value);

		number3Properties->SetInteger("Value",number3Value);


    }


Anything wrong in extern C definition?ffunc is the fortran program.
The extern C definition of ffunc_ is indeed wrong (functions with C language linkage cannot take reference arguments), but to begin investigating the problem reported by the compiler, it would help to see the actual, complete compiler diagnostic message ("The project has extern C defiend multiple times as well as has namespace defined" is incomplete) with the source code referenced in the message.
Cubbi,

Thats the error I have been getting...

Error 5 error LNK2019: unresolved external symbol ffunc referenced in function "public: int __cdecl calculator::apply_cb(void)" (?apply_cb@calculator@@QEAAHXZ) calculator.obj calculator

Cubi,

I have been getting the same error (as above) even if I definition of ffunc as below:

1
2
3
4
5
extern "C"
{
	//int  ffunc(int*,int*);
	int ffunc_(int*, int*);
}
That's a linker error. It tells you that there is nothing in your project that provides a definition for this function. It expects either a C program that defines a function int ffunc_(int*, int*) or a Fortran program that defines a function ffunc(integer, integer) similar to what I posted in the beginning. I can't help with project configuration.
Is there a problem calling fortran function from a callback functtion?
Cubbi,

I think there is some other problem because the call to fortarn function works correctly if I do not sue the custom wizard to get the template through which creates the default classes (plz see above as well)
Topic archived. No new replies allowed.