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.
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")
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)
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.
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?
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.
//------------------------------------------------------------------------------
[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());
}
}
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.
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
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.
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)