Hi,
I have been understanding how to call C/C++ code from C++/C. Firstly I tried calling a C function from C++ code :
I compiled the above code using the following command :Code:// cpp code #include<iostream> extern "C" { void func(); } int main(void) { func(); return 0; }
g++ -c main.cpp
Now, here is the C code :
Compiled the above code using :Code:#include<stdio.h> void func() { printf("\n Inside func()\n"); }
gcc -c func.c
Finally, I linked the C and c++ code using :
g++ main.o func.o -o out
Now, when I execute 'out', I could see the output 'Inside func()'.
As a second step, I defined a function 'fun()' in cpp file and tried to call it from C code..
Code://C++ code #include<iostream> #include<stdio.h> void fun(); extern "C" { void func(); } void fun() { printf("\n inside fun()\n"); } int main(void) { func(); return 0; }Now when I run the following command :Code:// C code #include<stdio.h> extern void fun(); void func() { printf("\n Inside func()\n"); fun(); }
g++ -Wall main.o func.o -o out
I get the following error :
I am not able to understand the reason????Code:~/practice $ g++ main.o func.o -o func func.o: In function `func': func.c:(.text+0x14): undefined reference to `fun' collect2: ld returned 1 exit status



LinkBack URL
About LinkBacks


