Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help: Call a function (http://www.go4expert.com/forums/help-function-t21202/)

maabm78 5Mar2010 07:49

Help: Call a function
 
Dear All;

Please could anyone help me. How can I call a function in C file from C++ file?
Please it is urgent.

Best regards

shabbir 5Mar2010 08:36

Re: Help: Call a function
 
Include the header file and call as a normal function.

maabm78 5Mar2010 08:50

Re: Help: Call a function
 
I did that, but I got "[Linker error] undefined reference to `function_name'".
I used Dev-C++ compiler.

I did a simple code.
Code:

/*
* add.h
*/
#ifndef ADD1_H
#define ADD1_H

int addnum(int a, int b);


#endif

Code:

/*
* add.c
*/
#include "add.h"

int addnum(int a, int b)
{
return a+b;
}

Code:

/*
* main.cpp
*/
#include <stdio.h>
#include "add.h"

int main (void) {
int i,j,z;
i=4;
j=5;
z = addnum(i,j);
printf("%d\n", z);
return 0;
}


techgeek.in 5Mar2010 10:46

Re: Help: Call a function
 
visit this link...
http://www.parashift.com/c++-faq-lit...c-and-cpp.html

ur problem is well explained here...

Abinila 6Mar2010 12:32

Re: Help: Call a function
 
Try this,

Add the following line in your add.h file. Like,

Code:


#ifndef ADD1_H
#define ADD1_H
#include "add.c"

int addnum(int a, int b);
#endif



Other wise,



If you have an individual C function that you want to call, and for some reason you don't have or don't want to #include a C header file in which that function is declared, you can declare the individual C function in your C++ code using the extern "C" syntax. You need to use the full function prototype:

Code:


  extern "C" void f(int i, char c, );
 extern "C" {
  void  f(int i, char c);
  int    g(char* s, char* s2);
}

Call the function like,

Code:

int main()
 {
  f(10, 'x');   
 }



All times are GMT +5.5. The time now is 10:34.