Re: using dot_product from c++ II
Gerry Ford wrote:
I keep tapping away at a program that calls a c++ function from
another syntax:
The fortran is:
program vector3
implicit none
real(kind=8), external :: dot_product_from_cplusplus
integer index, i
integer, parameter :: some_kind_number = selected_real_kind (p=16)
real (kind = some_kind_number), dimension(4):: vec_a, vec_b
real (kind = some_kind_number) :: res
index = 4
do i = 1, index
vec_a(i)= i**.5
vec_b(i)= (-1.0)*(i**2)
end do
res = dot_product_from_cplusplus(vec_a, vec_b)
write (*,*) vec_a, vec_b
write (*,*) res
end program vector3
! gfortran2 -o vector3 b.o vector3.f95 >text55.txt 2>text56.txt
! vector3 >text55.txt 2>text56.txt
!! end of the fortran part begin c++ function
#include <cmath>
#include <vector>
#include <iostream>
#include <iterator>
double dot_product_from_cplusplus (const std::vector<double>& vec_a,
const std::vector<double>& vec_b)
{
if ( vec_a.size() != vec_b.size() )
{
std::cerr << "Vectors for dot product are not same size!\n";
return 0.0;
}
double sum = 0;
for ( std::size_t i = 0; i < vec_a.size(); ++i )
{
sum += vec_a[i] * vec_b[i];
}
return std::pow(sum, .5);
}
// g++ -c b.cpp
// end function begin list of linker errors
Following errors have nothing to do with the code posted, but some other
code.
b.o:b.cpp:(.text+0xe): undefined reference to `std::string::size()
this is complaining about std::string. You are not using std::string in
this code, so it is somewhere else.
const' b.o:b.cpp:(.text+0x52): undefined reference to
`std::string::operator[](unsigned int) const'
b.o:b.cpp:(.text+0x8b): undefined reference to
`std::string::operator[](unsigned int) const'
b.o:b.cpp:(.text+0xcc): undefined reference to
`std::string::operator[](unsigned int) const'
All those are also complaining about std::string
b.o:b.cpp:(.text+0x116): undefined reference to
`std::ios_base::Init::Init()'
b.o:b.cpp:(.text+0x135): undefined reference to
`std::ios_base::Init::~Init()'
This is complaining about ios_base, which iostream is deriving from
b.o:b.cpp:(.text+0x1a3): undefined reference to `std::cerr'
Also an iostream stream
b.o:b.cpp:(.text+0x1a8): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::operator<<
<std::char_traits<char>
ostream also part of iostream
(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:\DOCUME~1\dan\LOCALS~1\Temp/ccIftQ0c.o:vector3.f95:(.text+0xb2):
undefined reference to `_dot_product_from_cplusplus_'
And this is the only error that seems to apply to this particular code.
collect2: ld returned 1 exit status
So clearly I haven't quite got it all down pat yet. Folks in c.l.f.
are telling me that they doubt I'll ever get it to work with
std::vector and instead council to send a pointer instead like so:
extern "C" double dot_product_from_cplusplus(double const * a,
double const * b)
, instead of:
double dot_product_from_cplusplus (const std::vector<double>& vec_a,
const std::vector<double>& vec_b)
This may be, but the errors you posted have nothing to do with the code as
supplied. Are you missing linking some library. I don't know what library
it is however.
It sounds like you may be attempting to link the fortran object with the C++
object but forgetting to also link one of the required c++ libraries.
What library that is depends on your compiler and OS, etc... What are you
compiling with? What compiler and what OS? Looks like windows because of
the path, but doesn't look like MSVC++
So, if a points to vec_a, and likewise with b, how do I rewrite Jim
Langston's function to calculate an inner product from scratch?
Grateful for your help.
--
Jim Langston
tazmaster@rocketmail.com