My problem is I want to call C++ function in C.
My code is as follows. Here it gives In function `main':
: undefined reference to `cpp_function'
I am using GNU Compiler in LINUX.
------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
- //cpp_file.cpp
- #include "cpp_file.h"
- #include <iostream>
- void cpp_function(){
- foo f;
- f.see();
- cout<<"Hi, I am in Cpp file and called by C file\n"<<flush;
- }
Expand|Select|Wrap|Line Numbers
- //cpp_file.h
- #include <iostream.h>
- class foo {
- public:
- int i;
- foo() {i=0; }
- ~foo() {}
- void see() {
- cout << "--------------I am inside class ------------" << endl;
- }
- };
- void cpp_function();
Expand|Select|Wrap|Line Numbers
- //c_file.h
- #ifdef __cplusplus
- extern "C" {
- #endif
- void cpp_function( void );
- #ifdef __cplusplus
- }
- #endif
Expand|Select|Wrap|Line Numbers
- //c_file.c
- #include "c_file.h"
- int main(){
- cpp_function();
- return(0);
- }
//Makefile
Expand|Select|Wrap|Line Numbers
- exe: c_file.o cpp_file.o
- g++ -o exe c_file.o cpp_file.o c_file.h cpp_file.h
- c_file.o: c_file.c c_file.h
- g++ -c c_file.c
- cpp_file.o: cpp_file.cpp cpp_file.h
- g++ -c cpp_file.cpp
- clean:
- rm *.o