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
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; }
visit this link... http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html ur problem is well explained here...
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'); }