I really need help on this.
Anyone has experience in calling FORTRAN from C++ (using extern C?)
JErome
Printable View
I really need help on this.
Anyone has experience in calling FORTRAN from C++ (using extern C?)
JErome
Have you tried google 'calling fortran from c'? I get a lot of hits.
Yes- I was able to get good information.
I tried a small program which worked well. It was a simple one like this:
C++ code
FORTRANCode:#include <iostream>
extern "C"
{
int ffunc(int*, int*);
}
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 " << r << '\n';
}
Now, I have a template created by a wizard . This template eseentially contains C++ source to guide you in the construction of my Block application dialog. The generation of your dialog file (.dlx extension) is the first step towards dialog construction within the external software I'm using.Code:function ffunc(n1,n2)
integer ffunc
integer n1,n2
ffunc = n1+n2
return
end
Here, in teh template, I have added the code in green
void calculator::dialogShown_cb()
{
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"
{
//int ffunc(int,int);
int ffunc(int*, int*);
}
It throws the follwoign error while compiling indicate that there is no function found (the funtion of FORTRAN). I have fefined the FORTRAN library through Linker->Geneeral->Addnl library directories
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
Can anyone please help?
Have you added the library to Linker/Input/Additional Dependencies? Linker/General/Additional Library Directories should only have the path.
Yes- I did
Linker /general /Addnl dependencies has the path as below
D:\jerome_ABCD\proj\fortran_to_cpp\flib\flib\Debug
and Linker/input/addnl dependencies had the library name flib.lib
Would you liek to see the project?
Does anything need to be set in configuration propeorties?
No that should be enough. Sure strip the project of all generated files, zip it and post it.
Thanks a lot,
Please find attached a file anmed calculator.zip.
It is actually generally through the wizard which of an external software which has all the class definitions.
As you see it has extern c defined.
NExt, I have also attached test.zip which ahs the fortran function in test.F
There is also a file test.cpp.
With the same linker and config settings, if I call the FORTRAN function from test.cpp it works.
But, when I call from calculator.cpp it doesn't.
I shall be grateful and obliged if helped as it is giving me sleeples nights for my task .
Thanks in advance
Hm. For some reason downloading of those zips are blocked by the SmartScreen Filter.