Help: Call a function

maabm78's Avatar, Join Date: Mar 2010
Newbie Member
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Include the header file and call as a normal function.
0
maabm78's Avatar, Join Date: Mar 2010
Newbie Member
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;
}
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
visit this link...
http://www.parashift.com/c++-faq-lit...c-and-cpp.html

ur problem is well explained here...
0
Abinila's Avatar, Join Date: Feb 2010
Light Poster
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');    
 }