C++ wrapper for fortran functions

This is a discussion on C++ wrapper for fortran functions within the Fortran forums in Programming Languages category; Dear all, Though this is a mixed language issue, I did decide to post here, How can I can wrap a fortran function in C++? I am aware of extern linkage and the _ issues that have to be added. If I would like to wrap this, what should be done? extern "C" { void lapackfunctiontocall_(...) } I can make an extern "C" block in a header file and put all needed functions inside that block however I wondered how I can wrap them alone inside a function decleration most probably in a class decleration. Thanks for the advice. Umut...

Go Back   Application Development Forum > Programming Languages > Fortran

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-02-2008, 07:19 PM
utab
Guest
 
Default C++ wrapper for fortran functions

Dear all,

Though this is a mixed language issue, I did decide to post here,

How can I can wrap a fortran function in C++? I am aware of extern
linkage and the _ issues that have to be added. If I would like to wrap
this, what should be done?

extern "C"
{
void lapackfunctiontocall_(...)
}

I can make an extern "C" block in a header file and put all needed
functions inside that block however I wondered how I can wrap them alone
inside a function decleration most probably in a class decleration.

Thanks for the advice.

Umut
Reply With Quote
  #2  
Old 09-02-2008, 08:28 PM
Gary Scott
Guest
 
Default Re: C++ wrapper for fortran functions

utab wrote:
> Dear all,
>
> Though this is a mixed language issue, I did decide to post here,
>
> How can I can wrap a fortran function in C++? I am aware of extern
> linkage and the _ issues that have to be added. If I would like to wrap
> this, what should be done?


Do you really mean "wrapper" or do you mean that you want to write an
appropriate prototype so that you can use the Fortran function as-is
(subroutine?) from C? There may be reasons to write a wrapper, for
example to make it easier to call in one language or the other. If you
want a prototype, I think that if you provide the fortran
subroutine/function definition along with it's argument list
declaration, a precise prototype can be given. But it would likely be
unique in many if not most cases.

>
> extern "C"
> {
> void lapackfunctiontocall_(...)
> }
>
> I can make an extern "C" block in a header file and put all needed
> functions inside that block however I wondered how I can wrap them alone
> inside a function decleration most probably in a class decleration.
>
> Thanks for the advice.
>
> Umut



--

Gary Scott
mailto:garylscott@sbcglobal dot net

Fortran Library: http://www.fortranlib.com

Support the Original G95 Project: http://www.g95.org
-OR-
Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html

If you want to do the impossible, don't hire an expert because he knows
it can't be done.

-- Henry Ford
Reply With Quote
  #3  
Old 09-02-2008, 09:36 PM
Ron Ford
Guest
 
Default Re: C++ wrapper for fortran functions

On Wed, 03 Sep 2008 01:19:28 +0200, utab posted:

> Dear all,
>
> Though this is a mixed language issue, I did decide to post here,


clf is a better place because of its lack of jealousy along these lines.


>
> How can I can wrap a fortran function in C++? I am aware of extern
> linkage and the _ issues that have to be added. If I would like to wrap
> this, what should be done?
>
> extern "C"
> {
> void lapackfunctiontocall_(...)
> }


I think you need a function name like

extern "C" wrapper1
{
void lapackfunctiontocall_(...)

}

, and you might need parens on wrapper1.
>
> I can make an extern "C" block in a header file and put all needed
> functions inside that block however I wondered how I can wrap them alone
> inside a function decleration most probably in a class decleration.



I haven't fired up a c++ compiler for a while, so I'll be happy to
replicate, if you wonder how others' mileage varies.

What is your build environment?
--
Wealth - any income that is at least one hundred dollars more a year than
the income of one's wife's sister's husband. 6
H. L. Mencken
Reply With Quote
  #4  
Old 09-03-2008, 03:01 AM
Jugoslav Dujic
Guest
 
Default Re: C++ wrapper for fortran functions

utab wrote:
> Dear all,
>
> Though this is a mixed language issue, I did decide to post here,
>
> How can I can wrap a fortran function in C++? I am aware of extern
> linkage and the _ issues that have to be added. If I would like to wrap
> this, what should be done?
>
> extern "C"
> {
> void lapackfunctiontocall_(...)
> }
>
> I can make an extern "C" block in a header file and put all needed
> functions inside that block however I wondered how I can wrap them alone
> inside a function decleration most probably in a class decleration.


Well, you discovered how to properly declare a Lapack (or any other)
function in C++ (let's put aside the underscore issue, as well as
possible use of BIND(C) on Fortran side). From then on, you can use
that function much like any other external (non-class) C++ function.
So, it doesn't matter anymore if it's written in C, Fortran, Pascal
or anything else.

Thus, I don't get the direction of the question. It boils down
to "what is wrapping and how to do it in C++?". To which the answer
is "how do you know you need wrapping?" and/or "why do you need it?"...
or even "well, write a wrapper function or class...".

Sorry to be blunt, but you should be more specific...

--
Jugoslav
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
Reply With Quote
  #5  
Old 09-03-2008, 04:26 AM
utab
Guest
 
Default Re: C++ wrapper for fortran functions

On Wed, 03 Sep 2008 09:01:54 +0200, Jugoslav Dujic wrote:

> utab wrote:
>> Dear all,
>>
>> Though this is a mixed language issue, I did decide to post here,
>>
>> How can I can wrap a fortran function in C++? I am aware of extern
>> linkage and the _ issues that have to be added. If I would like to wrap
>> this, what should be done?
>>
>> extern "C"
>> {
>> void lapackfunctiontocall_(...)
>> }
>>
>> I can make an extern "C" block in a header file and put all needed
>> functions inside that block however I wondered how I can wrap them
>> alone inside a function decleration most probably in a class
>> decleration.

>
> Well, you discovered how to properly declare a Lapack (or any other)
> function in C++ (let's put aside the underscore issue, as well as
> possible use of BIND(C) on Fortran side). From then on, you can use that
> function much like any other external (non-class) C++ function. So, it
> doesn't matter anymore if it's written in C, Fortran, Pascal or anything
> else.
>
> Thus, I don't get the direction of the question. It boils down to "what
> is wrapping and how to do it in C++?". To which the answer is "how do
> you know you need wrapping?" and/or "why do you need it?"... or even
> "well, write a wrapper function or class...".
>
> Sorry to be blunt, but you should be more specific...


Hi, thanks for replies, what I would like to do was actually, sth like

void dspgv(int * itype, char* jobz, char* uplo, int* n, double* ap,
double* bp, double* w, double* z, int* ldz, double* work, int* info)
{
extern void dspgv_(int * itype, char* jobz, char* uplo, int* n, double*
ap, double* bp, double* w, double* z, int* ldz, double* work, int* info,
int, int); // LAPACK ROUTINE //
// actual call shoould go here
dspgv_(itype, jobz, uplo, n, ap, bp, w, z, ldz, work, info);
//}
}

This is not the wrapping, maybe, what most understood from my previous
message... I hope I made myself clear this time. But in this setting the
compiler should know that there will be an external linkage, that is the
point I am confused...

Thanks for the replies...
Best,

Reply With Quote
  #6  
Old 09-03-2008, 08:31 AM
Jugoslav Dujic
Guest
 
Default Re: C++ wrapper for fortran functions

utab wrote:
> On Wed, 03 Sep 2008 09:01:54 +0200, Jugoslav Dujic wrote:

<snip>
> Hi, thanks for replies, what I would like to do was actually, sth like
>
> void dspgv(int * itype, char* jobz, char* uplo, int* n, double* ap,
> double* bp, double* w, double* z, int* ldz, double* work, int* info)
> {
> extern void dspgv_(int * itype, char* jobz, char* uplo, int* n, double*
> ap, double* bp, double* w, double* z, int* ldz, double* work, int* info,
> int, int); // LAPACK ROUTINE //
> // actual call shoould go here
> dspgv_(itype, jobz, uplo, n, ap, bp, w, z, ldz, work, info);
> //}
> }
>
> This is not the wrapping, maybe, what most understood from my previous
> message... I hope I made myself clear this time. But in this setting the
> compiler should know that there will be an external linkage, that is the
> point I am confused...


Well, this is wrapping, but it's so trivial that I don't see a
purpose... the only your wrapper does exactly nothing, except for
"renaming" dspgv_ to dspgv. And a

#define dspgv_ dspgv

would do the job much simpler...

--
Jugoslav
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
Reply With Quote
  #7  
Old 09-03-2008, 09:25 AM
utab
Guest
 
Default Re: C++ wrapper for fortran functions

On Wed, 03 Sep 2008 14:31:00 +0200, Jugoslav Dujic wrote:
>
> Well, this is wrapping, but it's so trivial that I don't see a
> purpose... the only your wrapper does exactly nothing, except for
> "renaming" dspgv_ to dspgv. And a
>
> #define dspgv_ dspgv
>
> would do the job much simpler...


Yes, it is easy in this case, and thx for the suggestions, but the real
problem I have is that:

I am passing some character variables from the C++ side for some options,
and I should also pass the lengths of the character arrays as the last
argument in this wrapper function, let me give an example of what is
going on with an example case so that it is clear, idea is to use the toy
problem as explained above and my declarations for this case are: (the
header file, I am sure it is quite simple but I am getting undefined
reference error from dspgv_) I am using the -llapack -lgfortran -lg2c
options for g++

a simple try:

#ifndef LAPACKINTERFACE1_HH
#define LAPACKINTERFACE1_HH

// symmetric eigenvalue solver
//
void dspgv(int * itype, char* jobz, char* uplo, int* n, double* ap,
double* bp, double* w, double* z, int* ldz, double* work, int* info)
{
extern void dspgv_(int * itype, char* jobz, char* uplo, int* n, double*
ap, double* bp, double* w, double* z, int* ldz, double* work, int*
info); // LAPACK ROUTINE //
dspgv_(itype, jobz, uplo, n, ap, bp, w, z, ldz, work, info);
}

#endif




Reply With Quote
  #8  
Old 09-03-2008, 10:40 AM
utab
Guest
 
Default Re: C++ wrapper for fortran functions: solved

I forgot to include the my header inside the extern "C" block in the main
driver file.

Thx anyway for your time
Reply With Quote
  #9  
Old 09-03-2008, 04:21 PM
JayBee
Guest
 
Default Re: C++ wrapper for fortran functions

On 2008-09-02, utab <umut.tabak@gmail.com> wrote:
> How can I can wrap a fortran function in C++? I am aware of extern
> linkage and the _ issues that have to be added. If I would like to wrap
> this, what should be done?
>
> extern "C"
> {
> void lapackfunctiontocall_(...)
> }


In this particular case, why not use one of the available C++
LAPACK/BLAS wrappers? With these you don't need to worry about the
Fortran calling conventions, and some of them are also more high-level
and easier to use (like, say, the LAPACK95 which is a Fortran 95 wrapper
around LAPACK).

http://lapackpp.sourceforge.net/

http://sourceforge.net/projects/lpp/

http://home.gna.org/getfem/gmm_intro

http://flens.sourceforge.net/

--
JayBee
Reply With Quote
  #10  
Old 09-03-2008, 05:23 PM
utab
Guest
 
Default Re: C++ wrapper for fortran functions

On Wed, 03 Sep 2008 23:21:13 +0300, JayBee wrote:

> On 2008-09-02, utab <umut.tabak@gmail.com> wrote:
>> How can I can wrap a fortran function in C++? I am aware of extern
>> linkage and the _ issues that have to be added. If I would like to wrap
>> this, what should be done?
>>
>> extern "C"
>> {
>> void lapackfunctiontocall_(...)
>> }

>
> In this particular case, why not use one of the available C++
> LAPACK/BLAS wrappers? With these you don't need to worry about the
> Fortran calling conventions, and some of them are also more high-level
> and easier to use (like, say, the LAPACK95 which is a Fortran 95 wrapper
> around LAPACK).
>
> http://lapackpp.sourceforge.net/
>
> http://sourceforge.net/projects/lpp/
>
> http://home.gna.org/getfem/gmm_intro
>
> http://flens.sourceforge.net/


Thanks for the pointers, that was just a wonder to be able to learn
how it could be done, I have already been using boost for linear algebra
but these are also other options. Thx
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:34 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.