| Re: using dot_product from c++ II |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.fortran · Group Profile
Author: Gerry FordGerry Ford Date: Feb 23, 2008 15:57
"Craig Powers" hal-pc.org> wrote in message
news:47bef912$0$29923$a726171b@news.hal-pc.org...
> Gerry Ford wrote:
>>
>> double dot_product_from_cplusplus (const std::vector& vec_a,
>> const
>> std::vector& vec_b)
>
> As I told utab, you are highly unlikely to be able to interface between a
> Fortran array and a C++ std::vector.
>
> The correct signature for this function if you want to be able to call it
> from Fortran is:
>
> extern "C" double dot_product_from_cplusplus(double const * a,
> double const * b)
>
> The 'extern "C"' is necessary to disable C++ name mangling, which is used
> to make symbols that distinguish by arguments for overloading purposes
> (Fortran accomplishes the same thing by using a generic interface to tie
> external symbols with different names together).
I didn't necessarily read what you wrote to utab on this. I'll take a
second gander at that thread.
I set a goal for an easier inter-syntax problem and was able to get results:
Main is in fortran:
PROGRAM Vector_Sqrt
IMPLICIT NONE
!add function from c++
real(kind=8), external :: sqrt_from_c
INTEGER, PARAMETER :: dp = KIND(0.0d0)
REAL(DP) :: four_vector(4), j
INTEGER :: i
DO i = 1, 4
j= 1.0*i
four_vector(i) = SQRT_FROM_C(j)
! four_vector(i) = SQRT(REAL(i, DP))
ENDDO
WRITE(*,'(4F19.16)') four_vector
END
! g++ -c cat.cpp && gfortran2 -c vector4.f95
! gfortran2 cat.o vector4.o
,and the sqrt is from c++:
//$ cat v.c
#include
extern "C" double sqrt_from_c_ (double *x)
{
return sqrt(*x);
}
// g++ -c cat.cpp
Much of this notation is mixed and sloppy. I'd been playing around with
different things and got this to work. I still don't know what cat would
mean to FX. I did find out that the double ampersand *does* work on dos
and comes highly recommended when using multiple commands.
I'll clean up this notation and try to get squared away with a dotproduct,
which doesn't require much more than a sqrt. It's not surprising that
passing a pointer to a real would be effective here. It would seem that
adding extern "C" and a trailing underscore is what works.
Thanks for your help.
--
Gerry Ford
"Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
beziehend.
|