TCS
Company
Programming
Program
How do you call C functions from C++?
Read Solution (Total 5)
-
-
Compile the C code like this:
gcc -c -o somecode.o somecode.c
Then the C++ code like this:
g++ -c -o othercode.o othercode.cpp
Then link them together, with the C++ linker:
g++ -o yourprogram somecode.o othercode.o
You also have to tell the C++ compiler a C header is coming when you include the declaration for the C function. So othercode.cpp begins with:
extern "C" {
#include "somecode.h"
}
somecode.hshould contain something like:
#ifndef SOMECODE_H_
#define SOMECODE_H_
void foo();
#endif
- 10 years agoHelpfull: Yes(1) No(7)
- /*this is what the C++ code would look like
for the declaration of the foo function, which
is defined somewhere else in C code: */
extern "C" void foo( );
And then to call the function in the C++ code, it would look like this:
//the declaration:
extern "C" void foo();
void main()
{
// the function call:
foo( );
} - 10 years agoHelpfull: Yes(1) No(1)
- coding language
- 9 years agoHelpfull: Yes(1) No(0)
- member functions
- 10 years agoHelpfull: Yes(0) No(0)
- yes this correct
- 10 years agoHelpfull: Yes(0) No(0)
TCS Other Question