There are times when it is required to mix the C and C++ code together. For example, while using a legacy C code or while using a specific C library the provides your C++ code with some specific functionality. So, some special steps are to be taken care of when using C code in C++ file or vice versa.
This article, through some examples discusses the steps required to mix C/C++ code.
1. Call C functions from C++
In this section we will discuss on how to call C functions from C++ code.
Here is the C code (Cfile.c):
#include <stdio.h> void f(void) { printf("\n This is a C code\n"); }
The first step is to create a library of this C code. The following steps create a shared library :
$ gcc -c -Wall -Werror -fPIC Cfile.c $ gcc -shared -o libCfile.so Cfile.o
The shared library libCfile.so is produced as a result of above two commands.
Here is the main C++ code (main.cpp) :
#include <iostream> extern "C" { void f(); } void func(void) { std::cout<<"\n being used within C++ code\n"; } int main(void) { f(); func(); return 0; }
The C function f() is declared within the notation extern “C” to tell the cpp compiler that it has C type linkage.
Now, compile the code (make sure that the shared library libCfile.so is linked to the code):
$ g++ -L/home/himanshu/practice/ -Wall main.cpp -o main -lCfile
Before running the executable make sure that the path of shared library is contain in the environment variable LD_LIBRARY_PATH.
$ export LD_LIBRARY_PATH=/home/himanshu/practice:$LD_LIBRARY_PATH
Now run the executable ‘main’ :
$ ./main This is a C code being used within C++ code
So we see that a C function was successfully called from a C++ code.
Also, read this for detailed information on how to create shared libraries in Linux.
2. Call C++ functions from C
In this section we will discuss on how to call C++ functions from C code.
Here is a C++ code (CPPfile.cpp) :
#include <iostream> void func(void) { std::cout<<"\n This is a C++ code\n"; }
We will see how the function func() can be called from a C code.
The first step for this is to change the declaration/definition of this function by introducing the notation extern “C”.
#include <iostream> extern "C" void func(void) { std::cout<<"\n This is a C++ code\n"; }
The next step is to create a library out of the code above. The following steps create a shared library:
g++ -c -Wall -Werror -fPIC CPPfile.cpp $ g++ -shared -o libCPPfile.so CPPfile.o
The above commands should result in libCPPfile.so shared library.
Here is main code in C language (main.c) :
#include <stdio.h> extern void func(void); void f(void) { printf("\n being used within C code\n"); } int main(void) { func(); f(); return 0; }
Please note that the C++ function is declared as extern here.
Compile the C code (main.c) like this:
gcc -L/home/himanshu/practice/ -Wall main.c -o main -lCPPfile
and add the current directory path to the environment variable LD_LIBRARY _PATH
export LD_LIBRARY_PATH=/home/himanshu/practice:$LD_LIBRARY_PATH
Now run the executable ‘main’ :
$ ./main This is a C++ code being used within C code
The output above shows that the C++ function was successfully called from C code.
Comments on this entry are closed.
The key to understand for me was that C linkage is standard for the entire platform regardless of the compiler or linker used. Any C compiler can link to any C library regardless on the same platform.
C++ linkage is extremely compiler/linker biased. This means that C++ libraries have to be created based on the C++ compiler used. Using a commercial C++ library created with a different compiler than you plan to use is not possible.
I hope this is clear.
Hello,
Normally if you are writing C code you would normally do the following in the header file.
ifdefine __cpluscplus
extern “C”
{
#endif
/* function declarations here */
ifdefine __cplusplus
}
#endif
I don’t think a c compiler will understand the extern “C” that is why you have to wrap it in a __cpluscplus macro.
The second part calling C++ from C. You would normally create a wrapper that is compiled using a g++ compiler. Then your C code will call the functions that are wrapped in the c++ wrapper.
Please comment on my above statement if I am incorrect, as I haven’t done this in a while. Maybe there are other tricks to doing this. I would be interested to know.
As always, nice articles.
Thanks,
Júlio.
Hi,
if CPP is not under
extern “C”
{
}
then it is not possible to use this CPP code in C!
reasons??
If You have show to the people, how to call functions from C++, could you show Us how to call functions form other libraries, that are not C alike….
Is this concept “how to call c functions from c + +” can be Implemented using turbo c + + and borland compiler
This is the only helpful post that explains the complete process compiling and linking c++ into c.
And i’ve searched a lot!!
Thanks!
Many many thanks & best regards !!!
Great example!
One thing… The line:
g++ -L/home/himanshu/practice/ -Wall main.cpp -o main -lCfile
…should be:
g++ -Wall main.cpp -o main Cfile.o
Your example would have worked if a Cfile.a library archive had been created in that folder.