Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:29:14+00:00 2026-06-17T18:29:14+00:00

For reasons that are not relevant, I need to pass a C/C++ function name

  • 0

For reasons that are not relevant, I need to pass a C/C++ function name into a Fortran subroutine, which, in turn, calls that C function. What I have found is that I can succesfully pass the function name into the Fortran subroutine. In that subroutine I can call the correct C function. However, the arguments of the C function get broken on this call (when called directly from C it works fine). I have used ISO C Binding to try and get this to work, to no avail.

Here is a MWE:

fortranRoutine.h:

extern "C" {
    void fortranRoutine_(void(int status));
};

calledfromFortran.h:

void calledfromFortran(int status);

main.cpp:

#include "fortranRoutine.h"
#include "calledfromFortran.h" 

using namespace std;

int main(int argc, char** argv) {
    calledfromFortran(12);
    fortranRoutine_(calledfromFortran);
    return 0;
}

fortranRoutine.f90:

subroutine fortranRoutine(calledfromFortran)

    use iso_c_binding

    implicit none

    interface
        subroutine calledfromFortran(status) bind (c)
            use iso_c_binding
            integer(kind = c_int), intent(in) :: status
        end subroutine calledfromFortran
    end interface

    integer(kind = c_int) :: one

    one = 2
    call calledfromFortran(one)
    return

end subroutine fortranRoutine

calledfromFortran.cpp:

#include <iostream>
#include "stdlib.h"

using namespace std;

void calledfromFortran(int status) {
    cout << "In calledfromFortran:" << endl;
    cout << " status: " << status << endl;
}

Current results

Running this currently gives:

In calledfromFortran:
 status: 12
In calledfromFortran:
 status: -1641758848

The first call to calledfromFortran from main works correctly, but when it’s called from fortranRoutine the value is broken. Note, each time it’s run that latter value changes. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:29:16+00:00Added an answer on June 17, 2026 at 6:29 pm

    When passing scalar values between Fortran and C, you have basically two options:

    • You pass them by reference: On the C-side you have to make sure, that you use pointers to those scalars.

    • You pass them by value: On the Fortran side you have to make sure that you use the VALUE attribute, as already suggested by other posts.

    As for the intent(in) attribute, it can stay there, as it does not affect, whether the variable is passed by value or reference. In Fortran, arguments are always passed by reference unless you specify the VALUE attribute. The attribute intent(in) only tells the compiler to prevent a usage of that dummy argument in the routine, which would change its value.

    Additional note on the naming: You should specify your Fortran routine fortranRoutine also with bind(c). This way you can specify its name as seen from C, even if it is inside a module:

    module my_interface
      use iso_c_binding
    
    contains
    
      subroutine fortranRoutine(calledFromFortran) bind(c, name='fortranroutine')
      ...
      end subroutine fortranRoutine
    
    end module my_interface
    

    This way you can be sure, the name of the function to be called from C is fortranroutine, independent of the convention of the compiler to append underscores, prepend module names or convert names to lower case. Consequently, you would have a a header file in C, which should work compiler independently:

    extern "C" {
      void fortranroutine(void(int status));
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Due to some obscure reasons which are not relevant for this question, I need
(Let's assume that I have good reasons not to remove my NSAssert() checks in
I was downgraded from Vista to XP for reasons that are not relevant. I
For reasons I think are not relevant here I have one or more Threads
For reasons that I would not like to discuss, our master database schema is
For reasons that are irrelevant to this question I'll need to run several SQLite
I have some pages that have content that is relevant to both logged in
The relevant structures from my cube are that I have a Hierarchy with Class
I have a bunch of forms that I need to put in a list
Is there a reason that the object TreeSet.apply method returns SortedSet and not TreeSet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.