Example of a method in C language code that calls C++ code
- 2020-05-12 02:58:12
- OfStack
Due to historical reasons and the technical preferences of different developers, both the C and C++ languages have a number of unique and very valuable projects, so the interoperation of the two languages to take full advantage of the pre-artificial wheels is a very valuable thing.
The C++ code calls the C code simply by adding the following two blocks to the beginning and end of the included C header file:
#ifdef __cplusplus
extern "C" {
#endif
and
#ifdef __cplusplus
}
#endif
Can.
However, in order to support more advanced features such as classes and overloading, the C++ symbol is modified when the C++ code is compiled. The symbol table of dump Linux platform encryption library libcrypto++ can be seen as follows:
$ readelf -s /usr/lib/libcrypto++.so
Symbol table '.dynsym' contains 9607 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000001daa58 0 SECTION LOCAL DEFAULT 9
2: 0000000000000000 0 OBJECT GLOBAL DEFAULT UND _ZTIi@CXXABI_1.3 (2)
3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __errno_location@GLIBC_2.2.5 (3)
4: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZSt18uncaught_exceptionv@GLIBCXX_3.4 (4)
5: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt8__detail15_List_node_base7_M_hookEPS0_@GLIBCXX_3.4.15 (5)
6: 0000000000000000 0 FUNC GLOBAL DEFAULT UND getservbyname@GLIBC_2.2.5 (6)
7: 0000000000000000 0 FUNC GLOBAL DEFAULT UND bind@GLIBC_2.2.5 (6)
8: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZSt29_Rb_tree_insert_and_rebalancebPSt18_Rb_tree_node_baseS0_RS_@GLIBCXX_3.4 (4)
9: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __longjmp_chk@GLIBC_2.11 (7)
10: 0000000000000000 0 OBJECT GLOBAL DEFAULT UND _ZTIh@CXXABI_1.3 (2)
11: 0000000000000000 0 OBJECT GLOBAL DEFAULT UND _ZTVSt9basic_iosIcSt11char_traitsIcEE@GLIBCXX_3.4 (4)
12: 0000000000000000 0 FUNC GLOBAL DEFAULT UND socket@GLIBC_2.2.5 (6)
13: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt14basic_ifstreamIcSt11char_traitsIcEED1Ev@GLIBCXX_3.4 (4)
. . . . . .
86: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSo5writeEPKcl@GLIBCXX_3.4 (4)
87: 0000000000000000 0 FUNC GLOBAL DEFAULT UND malloc@GLIBC_2.2.5 (6)
88: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSt9basic_iosIcSt11char_traitsIcEE4initEPSt15basic_streambufIcS1_E@GLIBCXX_3.4 (4)
89: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZNSi5seekgElSt12_Ios_Seekdir@GLIBCXX_3.4 (4)
90: 0000000000000000 0 FUNC GLOBAL DEFAULT UND pthread_key_delete@GLIBC_2.2.5 (3)
91: 0000000000000000 0 FUNC GLOBAL DEFAULT UND shutdown@GLIBC_2.2.5 (6)
92: 0000000000000000 0 FUNC GLOBAL DEFAULT UND _ZSt15set_new_handlerPFvvE@GLIBCXX_3.4 (4)
93: 0000000000000000 0 FUNC GLOBAL DEFAULT UND pthread_getspecific@GLIBC_2.2.5 (3)
94: 0000000000000000 0 FUNC GLOBAL DEFAULT UND strcmp@GLIBC_2.2.5 (6)
95: 0000000000000000 0 FUNC GLOBAL DEFAULT UND strtol@GLIBC_2.2.5 (6)
96: 0000000000000000 0 FUNC GLOBAL DEFAULT UND ioctl@GLIBC_2.2.5 (6)
. . . . . .
186: 00000000002c5a80 142 FUNC GLOBAL DEFAULT 12 _ZN8CryptoPP6xorbufEPhPKhS2_m
187: 00000000002fd6d0 9 FUNC WEAK DEFAULT 12 _ZN8CryptoPP21InvertibleRSAFunction9BERDecodeERNS_22BufferedTransformationE
188: 00000000001ea840 73 FUNC GLOBAL DEFAULT 12 _ZN8CryptoPP13Base64Decoder22GetDecodingLookupArrayEv
189: 0000000000249760 6 FUNC WEAK DEFAULT 12 _ZThn8_N8CryptoPP13DL_SignerImplINS_25DL_SignatureSchemeOptionsINS_5DL_SSINS_13DL_Keys_ECDSAINS_4EC2NEEENS_18DL_Algorithm_ECDSAIS4_EENS_37DL_SignatureMessageEncodingMethod_DSAENS_6SHA256EiEES5_S7_S8_S9_EEED0Ev
190: 0000000000278b60 86 FUNC WEAK DEFAULT 12 _ZN8CryptoPP8Rijndael3DecD1Ev
191: 00000000001fd1f0 2 FUNC WEAK DEFAULT 12 _ZN8CryptoPP23DefaultEncryptorWithMAC8FirstPutEPKh
192: 000000000026a490 51 FUNC GLOBAL DEFAULT 12 _ZN8CryptoPP23FilterWithBufferedInputC2EPNS_22BufferedTransformationE
193: 0000000000285180 6 FUNC WEAK DEFAULT 12 _ZNK8CryptoPP8GCM_Base6IVSizeEv
194: 000000000032e830 510 FUNC WEAK DEFAULT 12 _ZN8CryptoPP18StandardReallocateItNS_20AllocatorWithCleanupItLb0EEEEENT0_7pointerERS3_PT_NS3_9size_typeES8_b
195: 00000000002a1790 185 FUNC WEAK DEFAULT 12 _ZSt18uninitialized_copyISt15_Deque_iteratorIyRKyPS1_ES0_IyRyPyEET0_T_S9_S8_
196: 0000000000355610 25 OBJECT WEAK DEFAULT 14 _ZTSN8CryptoPP11RSAFunctionE
. . . . . .
This is not the same as the declared definitions of the functions and classes we see in the source and header files. We can see them in the code by c++filt demangle:
$ c++filt _ZTSN8CryptoPP11RSAFunctionE
typeinfo name for CryptoPP::RSAFunction
$ c++filt _ZN8CryptoPP18StandardReallocateItNS_20AllocatorWithCleanupItLb0EEEEENT0_7pointerERS3_PT_NS3_9size_typeES8_b
CryptoPP::AllocatorWithCleanup<unsigned short, false>::pointer CryptoPP::StandardReallocate<unsigned short, CryptoPP::AllocatorWithCleanup<unsigned short, false> >(CryptoPP::AllocatorWithCleanup<unsigned short, false>&, unsigned short*, CryptoPP::AllocatorWithCleanup<unsigned short, false>::size_type, CryptoPP::AllocatorWithCleanup<unsigned short, false>::size_type, bool)
Is there a way to call C++ in C code? There are, of course, more than one.
Called via extern "C"
A function is defined in the.cpp file and declared as extern "C", which can be easily called in C code. Since this function is defined in the.cpp file, you can call any C++ code in the implementation of this function, including C++ functions, creating C++ classes, and so on.
C++ header file:
#ifndef CPPFUNCTIONS_H_
#define CPPFUNCTIONS_H_
#ifdef __cplusplus
int cpp_func(int input);
extern "C" {
#endif
int c_func(int input);
#ifdef __cplusplus
}
#endif
#endif /* CPPFUNCTIONS_H_ */
C++ implementation file is as follows:
#include "CppFunctions.h"
int cpp_func(int input) {
return 5;
}
int c_func(int input) {
return cpp_func(input);
}
Call C++ function in C code:
#include <stdio.h>
#include "CppFunctions.h"
int main(int argc, char **argv) {
printf("%d\n", c_func(10));
return 0;
}
The c_func function, defined in the C++ file, is like a bridge between the C code world and the C++ code world. However, the parameters of C function c_func and the type of return value are naturally limited by 1, but the function implementation can adapt the C++ interface to be called to do some adaptation.
Called through dlopen/dlsym
Calling the C++ interface indirectly with the help of the C function defined in the.cpp file is, of course, the goal of calling C++ code in the C code, but there is still some trouble. Through the interface provided by libdl, our goal can be achieved in a more convenient way.
Pass in a modified symbol for dlsym, and you can find the address of the corresponding function.
Compile the above CPPFunctions.cpp file into a dynamically linked library with the following command:
$ gcc -shared -fPIC CPPFunctions.cpp -o libCppLibTest.so
Find the corresponding C++ function through dlopen and dlsym, and cast its type into the appropriate function pointer, and then call the target function through the function pointer, such as:
#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char **argv) {
void *libCPPTest = dlopen("/home/hanpfei0306/workspace_java/CppLibTest/Debug/libCppLibTest.so", RTLD_NOW);
int (*cpp_func)(int) = (int (*)(int))dlsym(libCPPTest, "_Z8cpp_funci");
printf("cpp_func = %p\n", cpp_func);
printf("cpp_func output = %d\n", cpp_func(10));
return 0;
}
Compile and execute the above code, and see the following output on my machine:
cpp_func = 0x7f35727a8650
cpp_func output = 5
conclusion
The above is the entire content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.