473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C function in a C++ code

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...


Jul 23 '05 #1
13 1348

"kak3012" <s0*****@student.dtu.dk> wrote in message
news:ct***********@gnd.k-net.dk...
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
Jul 23 '05 #2
kak3012 wrote:
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! :-)

--
WW aka Attila
:::
According to my calculations the problem doesn't exist.
Jul 23 '05 #3
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.

Jul 23 '05 #4
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 );

}

kak3012 wrote:
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...


Jul 23 '05 #5
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

}
Jul 23 '05 #6
compiler is VS.NET 2003 by the way.
Jul 23 '05 #7
On Tue, 25 Jan 2005 22:05:16 +0100, kak3012 wrote:

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
Jul 23 '05 #8
no it did not help..
Jul 23 '05 #9
Change the "void" in the function signature to "float" or vice versa.

Jul 23 '05 #10
kak3012 wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #11
I want a sniper now..

I have found a

#define float double

line in nrutils.h

I want blode...


"Karl Heinz Buchegger" <kb******@gascad.at>, iletide sunu yazdi
news:41***************@gascad.at...
kak3012 wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at

Jul 23 '05 #12
"kak3012" <s0*****@student.dtu.dk> wrote in message
news:ct***********@gnd.k-net.dk...
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
Jul 23 '05 #13
yeap I usually write stupid things..

Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
3
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
2
by: Oltmans | last post by:
Hi all, I'm stuck in a situation where I need help. Any help will be highly appreciated. I've created an object, after creating the object, I'm assigning one of its functions to an event handler...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.