Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cannot access the values inside the Fortran labelled common blocks and I haven't found any example about this problem.
I'm able to call the Fortran routine that I've put inside a module but I cannot access to the Common Block Variables and notwithstanding several attempts I've always got zeros for the variables in common blocks.
I've created two separate projects one for C++ Console and one for Fortran dynamic link library inside a solution, in which the C++ project depends on the Fortran project.

Thank You very much for your answers in advance.


Carlo Timossi

What I have tried:

C++ code:
#include <iostream>

typedef struct
{
    short int   nstream;
    short int   nunit;
    short int   ncomp;
}  generaltype;

typedef struct 
{
    float tau;
} PItype;

extern "C" {
    generaltype general;
    PItype pi;
}

extern "C" {
    void ADD1(void);
}


int main()
{
    ADD1();
    std::cout << general.ncomp;
    std::cout << general.nunit;
    std::cout << general.ncomp;
    std::cout << pi.tau;
}


Fortran code:
module testfor
    use, intrinsic :: iso_c_binding
    implicit none
contains
    subroutine ADD1() bind(C,name="ADD1")
    !DEC$ ATTRIBUTES DLLEXPORT:: ADD1
    implicit none
    include "Comm.for"
    nstream=5
    nunit=1
    ncomp=3
    tau = 3.14159
    return
    end subroutine
end module testfor


Comm.for code:
REAL tau
     !DEC$ ATTRIBUTES alias:'pi' :: pi
     COMMON /pi/ tau
     INTEGER(2) nstream, nunit, ncomp
     !DEC$ ATTRIBUTES alias:'general' :: general
     COMMON /general/ nstream, nunit, ncomp
Posted
Updated 17-Nov-21 5:49am
v2
Comments
Richard MacCutchan 17-Nov-21 11:32am    
Where are the corresponding structures defined in the Fortran code?

1 solution

You need to declare general and pi as extern linkage, so the linker finds the references in the Fortran subroutine. See How to access FORTRAN COMMON blocks from C/C++ - Advanced Features and User Routines - FLUKA User Forum[^].
 
Share this answer
 
v2
Comments
Carlo Timossi 2021 17-Nov-21 12:08pm    
Thank you for your prompt answer but I've tried but it does not work unfortunately:
I've put in C++ code:
extern "C" {
extern struct
{
short int nstream;
short int nunit;
short int ncomp;
} general_;

extern struct {
float tau;
} pi_;
}
This structure has been suggested by the article " How to access FORTRAN COMMON blocks from C/C++ - Advanced Features and User Routines - FLUKA User Forum[^]."
But I've got the following error list:

Severity Code Description Project File Line Suppression State
Error LNK1120 2 unresolved externals ConsoleApplication1 C:\Users\carlo.timossi\Projects\Examples\ConsoleApplication1\ConsoleApplication1\Debug\ConsoleApplication1.exe 1
Error (active) E0147 declaration is incompatible with "struct <unnamed> general_" (declared at line 12) ConsoleApplication1 C:\Users\carlo.timossi\Projects\Examples\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 12
Error (active) E0147 declaration is incompatible with "struct <unnamed> pi_" (declared at line 16) ConsoleApplication1 C:\Users\carlo.timossi\Projects\Examples\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 16
Error LNK2001 unresolved external symbol _general_ ConsoleApplication1 C:\Users\carlo.timossi\Projects\Examples\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.obj 1
Error LNK2001 unresolved external symbol _pi_ ConsoleApplication1 C:\Users\carlo.timossi\Projects\Examples\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.obj 1
Richard MacCutchan 17-Nov-21 12:17pm    
Sorry, but I have no way to test this as I do not have (or need) a copy of the Intel Fortran system. I would suggest you try the Intel web site to see if anyone there has tried it. But my impression from reading some other questions is that it is not a good way of sharing data.

You could also try to get some information from the Fortran compiled code to see exactly what names it uses for its exported common blocks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900