fortran-iso-c-binding

Fortran interface to call a C function that returns a pointer to an array

a 夏天 提交于 2021-01-23 11:05:55
问题 After much searching, I found what I believe to be the closest answer to my problem is on Stack Overflow (SO) at Fortran interface to call a C function that return a pointer, (posted nearly 10 years ago!) I quote this because using that example keeps the code simple and still illustrates my problem. I want to return an array that has been created/memory allocated in C++ and be able to analyse the answer in Fortran, because that is where the bulk of the code for this application lies. My

Calling C++ function from Fortran in Visual Studio 2010

可紊 提交于 2020-06-23 14:13:53
问题 I want to call a C++ function from Fortran. To do that, I make a FORTRAN project in Visual Studio 2010. After that I add a Cpp project to that FORTRAN project. The following errors occur when I want to build the program: Error 1: unresolved external symbol print_C referenced in function MAIN_main.obj Error 2: 1 unresolved externals Following are the Fortran program and C++ function. Fortran program: program main use iso_c_binding, only : C_CHAR, C_NULL_CHAR implicit none interface subroutine

Calling C++ function from Fortran in Visual Studio 2010

烈酒焚心 提交于 2020-06-23 14:13:16
问题 I want to call a C++ function from Fortran. To do that, I make a FORTRAN project in Visual Studio 2010. After that I add a Cpp project to that FORTRAN project. The following errors occur when I want to build the program: Error 1: unresolved external symbol print_C referenced in function MAIN_main.obj Error 2: 1 unresolved externals Following are the Fortran program and C++ function. Fortran program: program main use iso_c_binding, only : C_CHAR, C_NULL_CHAR implicit none interface subroutine

Difference in Fortran pointer and Fortran allocatable in calling C_F_POINTER

元气小坏坏 提交于 2020-05-29 06:54:33
问题 The thing is, 'C_F_POINTER' compiles successfully(ifort version 19.0.5.281) with 'allocatable arrays' as its arguemnt, and it works in the exactly same way with the case in which 'pointer' is used as its argument. program test1 use mkl_spblas use omp_lib use iso_c_binding implicit none integer, parameter :: DIM_ = 4, DIM_2 = 6 integer :: stat, i integer :: irn(DIM_2), jcn(DIM_2) real*8 :: val(DIM_2) integer(c_int) :: indexing integer :: DIM_r, DIM_c type(c_ptr) :: rows_start_c, rows_end_c,

Why ISO_C_BINDING

China☆狼群 提交于 2020-01-24 03:27:13
问题 I am working on some fortran-calling-C code and am unclear about the use of the iso_c_binding module. I have fortran and C interfaces working successfully without iso_c_binding, and the question is if I should still explicitly bind functions and variables. For example, this works: program testprog ... interface subroutine c_parser(param) integer, intent(in) :: param end subroutine end interface integer :: a call c_parser(a) .. end program /****************/ void c_parser_ (int* param) So

Why is this attempt to pass a pointer from C++ to Fortran not working?

别来无恙 提交于 2020-01-14 14:44:22
问题 I need to pass a dynamic array from c++ to Fortran. I did a lot of research to put together an example that I thought should work, but it doesn't. The program should create an array in the c++ program, pass the pointer of that array to the Fortran routine, convert the C pointer to a Fortran pointer, and then print the array on the Fortran side. My c++ main program: using namespace std; extern "C" { void cinterface(int*,int*); } int main() { int carray[]={0,1,2,3,4}; int carray_siz=5;

Mixed Programming Fortran and C

你离开我真会死。 提交于 2020-01-14 02:53:08
问题 I am a Theoretical Physics research student, working in Cosmology. In course of my research I have use to rather huge library of Fortran codes and I used C for my programming needs. I have been able to link the two programs in numerous test files and they work brilliantly. But for them I have been using the object files to link them all. But when I tried to run the real deal through C, include reference to the Fortran header files. They seem to integrate and call each other fine but the

Calling a C function from Fortran where the C function name was originally passed in from C

核能气质少年 提交于 2020-01-11 10:50:09
问题 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

Passing allocated C_PTR to Fortran array to C

人盡茶涼 提交于 2020-01-04 09:32:04
问题 I'm having trouble with segfaults from accessing an array in C, which is allocated in the Fortran file below. There are a few artefacts of debugging, such as the fact that the file writes don't write anything meaningful and I initialise a variable i that I never use. However, I've found the following: Not initialising i (but still declaring it): no segfault Not opening the file in C: no segfault Not printing HESS (not HESS_COPY ) elsewhere in the code: no segfault Declaring and initialising i

Passing allocated C_PTR to Fortran array to C

[亡魂溺海] 提交于 2020-01-04 09:31:47
问题 I'm having trouble with segfaults from accessing an array in C, which is allocated in the Fortran file below. There are a few artefacts of debugging, such as the fact that the file writes don't write anything meaningful and I initialise a variable i that I never use. However, I've found the following: Not initialising i (but still declaring it): no segfault Not opening the file in C: no segfault Not printing HESS (not HESS_COPY ) elsewhere in the code: no segfault Declaring and initialising i