Could anybody help me in solving the problem of using c++ function/class in C code.
Given below is my code
Code:
//C code
#include <stdio.h>
//#include <code_cpp.cc>
extern int value(int, char**);
int main(int argc, char* argv[])
{
//main(int);
//int c;
return value(argc, argv);
int a = 1;
printf("value of a is %d \n", a);
return 0;
}
Code:
//C++ code
#include<iostream>
#ifdef __cplusplus
extern "C" int value()
{
#endif
int b = 2;
return b;
#ifdef __cplusplus
} // extern "C"
#endif
extern "C" int main ()
{
//std::cout << "Hello, world!\n";
int main_b;
main_b = value();
std::cout<<"value of main_b is "<<main_b<<std::endl;
return main_b;
}
On compiling c code I got the following error
Code:
gcc -Wall code_c.c -o code_c
/tmp/cctXTHYe.o: In function `main':
code_c.c:(.text+0x17): undefined reference to `value'
collect2: ld returned 1 exit status
I think there is some linking problem.
Thanks in advance!