C++: How would you call a C function from C++ code?
How would you call a C function from C++ code?
Using extern "C".
The function prototype must be preceded by extern "C". More than one C function can be grouped inside braces as shown below:
extern "C"
{
void f( ) ;
void f1( ) ;
}
// In cfunc.c
#include <stdio.h>
void f( )
{
printf ( "in f( )" ) ;
}
// In func.cpp
#include <iostream.h>
extern "C" void f( ) ;
void main( )
{
f( ) ;
}
Ensure that both .c and .cpp files are in the same project.




Comments
Log in or create a user account to comment.