| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| 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 |
|
#4
| |||
| |||
| 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. |
|
#5
| |||
| |||
| 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, |
|
#6
| |||
| |||
| 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. |
|
#7
| |||
| |||
| 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 |
|
#8
| |||
| |||
| I forgot to include the my header inside the extern "C" block in the main driver file. Thx anyway for your time ![]() |
|
#9
| |||
| |||
| 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 |
|
#10
| |||
| |||
| 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 learnhow it could be done, I have already been using boost for linear algebra but these are also other options. Thx |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.