Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
16 Views

About calling and using Fortran dll in c

Hi! After I posted my problem about calling Fortran dll in c in this forum, But it is hard way. I struggled to try it these days. I want to use nonlinear regression analysis by odrpack. All information and program is put here:TOMS 869. I create a dll containing  lpkbls.f,  odr.f,  real_precision.f 3 files. For odr.f, I added  !DEC$ ATTRIBUTES DLLEXPORT :: ODR below SUBROUTINE ODR. Next I took a prog simple_example.f90 translated into C  for testing  dll. This is my unfinished code:

#include "stdafx.h"
#include< iostream>
using namespace std;
extern"C"{void ODR( void* a, int *N, int *M, int *NP, int *NQ, double *BETA, double *Y, double *X); }

void FCN(int *N, int *M, int *NP, int *NQ, int *LDN, int *LDM,\
	int *LDNP, double *BETA, double *XPLUSD ,int *IFIXB, int *IFIXX, int *LDIFX,\
	int *IDEVAL, double *F, double *FJACB,double *FJACD, int *ISTOP);

int _tmain(int argc, _TCHAR* argv[])
{
	int NP = 2, N = 4, M = 1, NQ = 1,;

	double X[] = { 0.982, 1.998, 4.978, 6.01 }, Y[] = { 2.7, 7.4, 148.0, 403.0 };
	double BETA[] = { 2.0, 0.5 }, L[] = { 0.0, 0.0 }, U[] = {10,0.9};

	cout << ODR( FCN, &N, &M, &NP, &NQ,BETA,Y,X) << endl;

	system("pause");
	return 0;
}

void FCN(int *N, int *M, int *NP, int *NQ, int *LDN, int *LDM, int *LDNP,\
	double *BETA, double *XPLUSD, int *IFIXB, int *IFIXX, int *LDIFX, \
	int *IDEVAL, double *F, double *FJACB, double *FJACD, int *ISTOP){
	ISTOP = 0;

	if (fmod(IDEVAL,10)!=0){
		for (int i = 0, i < N; i++){
			F[0] = BETA[0] * exp(BETA[1] * XPLUSD);
		}
	}

	if (fmod(IDEVAL/10, 10) != 0){
		for (int i = 0, i <  N; i++){
			FJACB[0][0] = BETA[0] * exp(BETA[1] * XPLUSD[0]);
			FJACB[1][0] = BETA[0] * XPLUSD *exp(BETA[1] * XPLUSD[0]);
		}
	}

	if (fmod(IDEVAL / 100, 10) != 0){
		for (int i = 0, i < N; i++){

			FJACD[0][0] = BETA[1] * BETA[0]* XPLUSD[1]* exp(BETA[1] * XPLUSD[0]);
		}
	}

}

My operating environment is Visual Studio 2013, and my program languages are Visual Fortran,and Visual C++.

About user defined function FCN, you can check in guide.ps page 21-24.

Currently, I found some problems. 

1. subroutine ODR needs user-defined FCN function as argument, how to program it in C so that I can pass it to dll? 

2. In void FCN, it declares int *IDEVAL and use it, however I cannot find where it is set value. 

Last, I am glad  you give other advice or information about my problem, thanks for you helping.

0 Kudos
3 Replies
Highlighted
Black Belt
16 Views

What you are attempting to do is almost impossible. The version of Odrpack that you chose to use is in Fortran 95, and the main entry point to the package is a subroutine with some arguments with the attribute OPTIONAL. There is no support for calling such a subroutine from C.

You may use the older Fortran 77 version at www.netlib.org/toms/676.gz, but that is old and the source code is distributed in an obsolete format (tape image) that can be a nuisance to work with. The only reason to use that older package is that it is callable from C.

The simplest solution that I would choose is to do all your work in Fortran 95. If that is not acceptable, I suggest that you contact the authors of Odrpack at NIST and ask for their suggestions.

Your C code is full of incorrect usage of multidimensional array arguments, and will not work.

0 Kudos
Highlighted
16 Views

Actually, there is support for OPTIONAL arguments when called from C. It's done the same way you'd do it calling C, pass a zero by value. If one is using the Fortran 2015 C interoperability features, which Intel Fortran 16 supports, it's even standard-conforming to do so.

For a callback function, again, you can pass it just the way you'd do it calling C by passing the address of the function. Of course, you need to make sure that the callback function has exactly the correct set of arguments, types and passing conventions that Fortran expects. The declaration of FCN looks ok to me in that regard.

IDEVAL is passed in from whoever calls FCN.

Retired 12/31/2016
0 Kudos
Highlighted
Beginner
16 Views

Ok! I try it!

0 Kudos