C function in a C++ code

K

kak3012

Hi,

I have a C based code but the main code is in C++ so I want to call the
function from C++ file.

I have added

extern "C"
{
}

at the begining of the C++ code.

All I am doing is sending to arrays and getting back a single one. It does
not come right, so I have writtine out the first number before sending to C
function and when the C code gets it.

it is 1.186674 when I am sending but it is 0.611370 when function get it.

How does this happend? Anything else to do to use a C function in C++?

Cheers...
 
M

Mike Wahler

kak3012 said:
Hi,

I have a C based code but the main code is in C++ so I want to call the
function from C++ file.

I have added

extern "C"
{
}

at the begining of the C++ code.

If that's the code verbatim, It will have absolutely no effect.
All I am doing is sending to arrays and getting back a single one. It does
not come right, so I have writtine out the first number before sending to C
function and when the C code gets it.

it is 1.186674 when I am sending but it is 0.611370 when function get it.

How does this happend? Anything else to do to use a C function in C++?


/* file.c */
#include <stdio.h>
void c_function(void)
{
puts("Hello from c_function()");
}


/* file.cpp */
extern "C"
{
void c_function(void);
}

int main()
{
c_function();
return 0;
}


1. Compile 'file.c' with a C compiler.
2. Compile 'file.cpp' with a C++ compiler.
3. Link the outputs from 1. and 2. to create
an executable program

Many compilers can act as either a C or C++ compiler,
so a single product will often be able to handle
both 1. and 2. Check the documentation for details.

Linking is beyond the scope of the C++ language,
so again, check your documentation for how to do that.

-Mike
 
W

White Wolf

kak3012 said:
Hi,

I have a C based code but the main code is in C++ so I want to call the
function from C++ file.

I have added

extern "C"
{
}

at the begining of the C++ code.

All I am doing is sending to arrays and getting back a single one. It
does not come right, so I have writtine out the first number before
sending to C function and when the C code gets it.

it is 1.186674 when I am sending but it is 0.611370 when function get it.

How does this happend? Anything else to do to use a C function in C++?

Talk is cheap, show some code! :)
 
H

Hariprasad Govardhanam

Hello,
Are you sending the value in the right datatype? It might be that
you printed the value while sending in C++ part using "cout" and
receiving in C part is printed using "printf". Try to use same
functions in both the places. And use the same datatype as well.
Actually, a C function is a C++ function as well. you can compile a C
function directly in a C++ source code. Anyway, it will be good if you
post a part of the code that deals with the error.
 
A

alexmdac

You need to put the extern "C" { ... } around the C function's
declaration in the cpp file.

e.g. /* main.cpp */

extern "C" {

void cfuncA( void );
void cfuncB( void );

}
 
K

kak3012

Here is the code, beware the "#include "nrutil.h"" devil !!!

Cheers...

file.cpp
-----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


extern "C" float fit_NR(float xx[],float yy[],int N);

int main(int argc, char *argv[])
{


float *Data,*d,*h,*w;

Data = (float*)malloc(sizeof(float)*258);
d = (float*)malloc(sizeof(float)*100);
w = (float*)malloc(sizeof(float)*100);

// I OPEN THE FILE READ THE BINARY DATA HERE
// I BELIVE READING IS CORRECT BECAUSE THE
// w[2]=1.186674 does match the file



printf("%f",w[2]);
fit_NR(d,w,100);

return 0;
}

file.c
-----------------------------------------------------------
#include <math.h>
#include "nrutil.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void fit_NR(float xx[],float yy[],int N)
{
float * Par;
int i;

printf("%f",yy[2]);
// COMES OUT yy[2]=0.611370

}
 
J

Jay Nabonne

You have this:
extern "C" float fit_NR(float xx[],float yy[],int N);


file.c
-----------------------------------------------------------

And this:
void fit_NR(float xx[],float yy[],int N)
{

The return types are different. And a return type that doesn't fit in a
register (on some compilers like VC++) can occupy stack space with the
arguments.

See if it fixes it to make them have the same signature.

- Jay
 
K

Karl Heinz Buchegger

kak3012 said:
no it did not help..

In this case:
I can't see anything wrong in your code with
respect to this. So it could be something
compiler dependent. Which compiler are you
useing?
 
K

kak3012

I want a sniper now..

I have found a

#define float double

line in nrutils.h

I want blode...
 
M

Mike Wahler

kak3012 said:
I want a sniper now..

I have found a

#define float double

What a stupid thing to write.

I believe that will produce undefined behavior.
(Perhaps its effect on some implementations
is to create a catamaran).



-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top