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

  • Home
  • SEARCH
  • 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 4260282

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:53:57+00:00 2026-05-21T05:53:57+00:00

I feel like this should be an easy question, but I can’t get it

  • 0

I feel like this should be an easy question, but I can’t get it to work. I have some Fortran code that takes an input like:

      SUBROUTINE TRACE(X,Y,NAME,XX,YY)
      EXTERNAL NAME
      CALL NAME(X,Y,XX,YY)

and I’m trying to pass in a name from C++ in the form:

float x,y,xx,yy;
char * name="IGRF";
trace_(&x,&y,name,&xx,&yy);

It compiles, but I always get segfaults when I try to call the NAME subroutine. A subroutine called IGRF is defined in the file, and I can call the IGRF subroutine directly from C++, but need this TRACE routine. When running in gdb, it says the NAME variable comes through as a pointer to void.

I’ve tried passing NAME, &NAME, &NAME[0], a char NAME[4] that’s stripped of its \0 to perfectly fit the name, and they all come back showing the same void pointer. Does anybody know how to get a function name from C++ into that EXTERNAL variable in Fortran?

Thank you

  • 0 0 Answers
  • 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-05-21T05:53:58+00:00Added an answer on May 21, 2026 at 5:53 am

    So one advantage of Fortran2003 and later is that C interoperability is defined into the standard; it’s a bit of a PITA to use, but once it’s done, it’s guaranteed to work across platforms and compilers.

    So here’s cprogram.c, calling a Fortran routine getstring:

    #include <stdio.h>
    
    int main(int argc, char **argv) {
        int l;
        char *name="IGRF";
    
        l = getstring(name);
    
        printf("In C: l = %d\n",l);
    
        return 0;
    }
    

    and here’s fortranroutine.f90:

    integer(kind=c_int) function getstring(instr) bind(C,name='getstring') 
        use, intrinsic :: iso_c_binding
        character(kind=c_char), dimension(*), intent(IN) :: instr
        integer :: len
        integer :: i
    
        len=0
        do
           if (instr(len+1) == C_NULL_CHAR) exit
           len = len + 1
        end do
    
    
        print *, 'In Fortran:'
        print *, 'Got string: ', (instr(i),i=1,len)
        getstring = len
    end function getstring
    

    The makefile is simple enough:

    CC=gcc
    FC=gfortran
    
    cprogram: cprogram.o fortranroutine.o
        $(CC) -o cprogram cprogram.o fortranroutine.o -lgfortran
    
    fortranroutine.o: fortranroutine.f90
        $(FC) -c $^
    
    clean:
        rm -f *.o cprogram *~
    

    and running it works, under both gcc/gfortran and icc/ifort:

     In Fortran:
     Got string: IGRF
    In C: l = 4
    

    Update: Oh, I just realized that what you’re doing is rather more elaborate than just passing a string; you’re essentially trying to pass a function pointer pointing to a C callback routine. That’s a little tricker, because you have to use Fortran interfaces to declare the C routine — just using extern won’t work (and isn’t as good as explicit interfaces anyway, as there’s no type checking, etc.) So this should work:

    cprogram.c:

    #include <stdio.h>
    
    /* fortran routine prototype*/
    int getstring(char *name, int (*)(int));
    
    int square(int i) {
        printf("In C called from Fortran:, ");
        printf("%d squared is %d!\n",i,i*i);
        return i*i;
    }
    
    
    int cube(int i) {
        printf("In C called from Fortran:, ");
        printf("%d cubed is %d!\n",i,i*i*i);
        return i*i*i;
    }
    
    int main(int argc, char **argv) {
        int l;
        char *name="IGRF";
    
        l = getstring(name, &square);
        printf("In C: l = %d\n",l);
        l = getstring(name, &cube);
        printf("In C: l = %d\n",l);
    
    
        return 0;
    }
    

    froutine.f90:

    integer(kind=c_int) function getstring(str,func) bind(C,name='getstring')
        use, intrinsic :: iso_c_binding
        implicit none
        character(kind=c_char), dimension(*), intent(in) :: str
        type(c_funptr), value :: func
    
        integer :: length
        integer :: i
    
        ! prototype for the C function; take a c_int, return a c_int
        interface
            integer (kind=c_int) function croutine(inint) bind(C)
                use, intrinsic :: iso_c_binding
                implicit none
                integer(kind=c_int), value :: inint
            end function croutine
        end interface
        procedure(croutine), pointer :: cfun
    
        integer(kind=c_int) :: clen
    
        ! convert C to fortran procedure pointer,
        ! that matches the prototype called "croutine"
        call c_f_procpointer(func, cfun)
    
        ! find string length
        length=0
        do
           if (str(length+1) == C_NULL_CHAR) exit
           length = length + 1
        end do
    
        print *, 'In Fortran, got string: ', (str(i),i=1,length), '(',length,').'
    
        print *, 'In Fortran, calling C function and passing length'
        clen = length
        getstring = cfun(clen)
    
    end function getstring
    

    And the results:

    $ gcc -g -Wall   -c -o cprogram.o cprogram.c
    $ gfortran -c fortranroutine.f90 -g -Wall
    $ gcc -o cprogram cprogram.o fortranroutine.o -lgfortran -g -Wall
    $ gpc-f103n084-$ ./cprogram 
    ./cprogram 
     In Fortran, got string: IGRF(           4 ).
     In Fortran, calling C function and passing length
    In C called from Fortran:, 4 squared is 16!
    In C: l = 16
     In Fortran, got string: IGRF(           4 ).
     In Fortran, calling C function and passing length
    In C called from Fortran:, 4 cubed is 64!
    In C: l = 64
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this should be an easy question but I can't seem to
I feel like this should be obvious to me, but for some reason I
I feel like this is a stupid question, but I can't think of a
I can't seem to find this and I feel like it should be easy.
I feel like this should be an easy thing to figure out, but I'm
Feel like this should be something easy that I'm missing. I have a table
I feel like this should be easy, but i am struggling to return the
I feel like this should be easy, but it seems to be stumping me.
I feel like this should be easy, but google is totally failing me at
I feel like this should be a no brainer, but clearly I'm missing something...

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.