Extern C- Callinf Fortran from C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Extern C- Callinf Fortran from C++

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    5

    Extern C- Callinf Fortran from C++

    I really need help on this.

    Anyone has experience in calling FORTRAN from C++ (using extern C?)


    JErome

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Extern C- Callinf Fortran from C++

    Have you tried google 'calling fortran from c'? I get a lot of hits.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Mar 2013
    Posts
    5

    Re: Extern C- Callinf Fortran from C++

    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

    Code:
    #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';
    }
    FORTRAN

    Code:
    function ffunc(n1,n2)
          integer ffunc
          integer n1,n2
          ffunc = n1+n2
          return
          end
    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.

    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:ialogTypeError, 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?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Extern C- Callinf Fortran from C++

    Quote Originally Posted by jerome12345 View Post
    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
    This error message doesn't reference your Fortran function at all, unless I'm mistaken. So I suspect the actual problem is unrelated to the Fortran aspect, at least for the most part.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Extern C- Callinf Fortran from C++

    Have you added the library to Linker/Input/Additional Dependencies? Linker/General/Additional Library Directories should only have the path.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  6. #6
    Join Date
    Mar 2013
    Posts
    5

    Re: Extern C- Callinf Fortran from C++

    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?

  7. #7
    Join Date
    Mar 2013
    Posts
    5

    Re: Extern C- Callinf Fortran from C++

    Does anything need to be set in configuration propeorties?

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Extern C- Callinf Fortran from C++

    No that should be enough. Sure strip the project of all generated files, zip it and post it.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  9. #9
    Join Date
    Mar 2013
    Posts
    5

    Re: Extern C- Callinf Fortran from C++

    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
    Attached Files Attached Files

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Extern C- Callinf Fortran from C++

    Hm. For some reason downloading of those zips are blocked by the SmartScreen Filter.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center