When attempting to invoke a C function from C , the "extern "C" foo()" approach may encounter compilation issues with g . To circumvent this challenge and successfully execute the C function, the following approach can be employed:
gcc -c -o somecode.o somecode.c
g++ -c -o othercode.o othercode.cpp
g++ -o yourprogram somecode.o othercode.o
extern "C" { #include "somecode.h" }
#ifndef SOMECODE_H_ #define SOMECODE_H_ void foo(); #endif
Note that you can use a compiler of your choice, as long as you follow the principle of compiling the C and C code separately and linking them together afterward.
The above is the detailed content of How Can I Successfully Call a C Function from Within C Code?. For more information, please follow other related articles on the PHP Chinese website!