由于歷史原因,以及不同開(kāi)發(fā)人員的技術(shù)偏好,C語(yǔ)言和C++語(yǔ)言都有一些獨(dú)有的非常有價(jià)值的項(xiàng)目,因而兩種語(yǔ)言的互操作,充分利用前人造的輪子是一件非常有價(jià)值的事情。
C++代碼調(diào)用C代碼很簡(jiǎn)單,只要分別在包含的C頭文件的開(kāi)頭和結(jié)尾加上如下的兩個(gè)塊:
1
2
3
|
#ifdef __cplusplus extern "C" { #endif |
和
1
2
3
|
#ifdef __cplusplus } #endif |
即可。
然而為了支持類(lèi)、重載等更加高級(jí)的特性,在編譯C++代碼時(shí),C++符號(hào)會(huì)被修飾。我們dump Linux平臺(tái)加密庫(kù) libcrypto++ 的符號(hào)表,可以看到如下的內(nèi)容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
$ 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 . . . . . . |
這與我們?cè)谠次募皖^文件里看到的那些函數(shù)、類(lèi)的聲明定義都不一樣。通過(guò)binutils的工具c++filt demangle這些符號(hào)可以讓我們看到它們?cè)诖a里的樣子:
1
2
3
4
|
$ 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 ) |
那到底有沒(méi)有辦法在C代碼中調(diào)用C++代碼呢?方法當(dāng)然是有的,而且還不止一種。
通過(guò)extern “C”調(diào)用
在 .cpp 文件中定義一個(gè)函數(shù),聲明為extern "C",則該函數(shù)可以方便地在C代碼中調(diào)用。由于該函數(shù)在 .cpp 文件中定義,因而在該函數(shù)的實(shí)現(xiàn)中,可以調(diào)用任意的C++代碼,包括C++函數(shù),創(chuàng)建C++類(lèi)等等。
C++頭文件:
1
2
3
4
5
6
7
8
9
10
11
|
#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++實(shí)現(xiàn)文件如下:
1
2
3
4
5
6
7
|
#include "CppFunctions.h" int cpp_func( int input) { return 5; } int c_func( int input) { return cpp_func(input); } |
在C代碼里調(diào)用C++函數(shù):
1
2
3
4
5
6
|
#include <stdio.h> #include "CppFunctions.h" int main( int argc, char **argv) { printf ( "%d\n" , c_func(10)); return 0; } |
在C++文件里定義的c_func函數(shù)就像一座橋一樣,連接了C代碼的世界和C++代碼的世界。但 C 函數(shù)c_func的參數(shù)及返回值的類(lèi)型自然是受到一定的限制的,但在函數(shù)實(shí)現(xiàn)中可以適配要調(diào)用的C++接口,做一些適配。
通過(guò)dlopen/dlsym調(diào)用
借助于在 .cpp 文件中定義的C函數(shù),間接地調(diào)用C++接口,固然是能實(shí)現(xiàn)在 C 代碼中調(diào)用C++代碼的目標(biāo),然而還是有些麻煩。通過(guò)libdl提供的接口,可以使我們的目標(biāo)通過(guò)更簡(jiǎn)便的方式實(shí)現(xiàn)。
為dlsym傳入經(jīng)過(guò)修飾的符號(hào),可以找到對(duì)應(yīng)的函數(shù)的地址。
通過(guò)如下命令將上面的CPPFunctions.cpp文件編譯為一個(gè)動(dòng)態(tài)鏈接庫(kù):
1
|
$ gcc -shared -fPIC CPPFunctions.cpp -o libCppLibTest.so |
通過(guò)dlopen和dlsym找到對(duì)應(yīng)的C++函數(shù),并將其強(qiáng)制類(lèi)型轉(zhuǎn)換為適當(dāng)類(lèi)型的函數(shù)指針,然后通過(guò)函數(shù)指針調(diào)用目標(biāo)函數(shù),如:
1
2
3
4
5
6
7
8
9
|
#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; } |
編譯并執(zhí)行上面的代碼,在我的機(jī)器上可以看到如下的輸出:
1
2
|
cpp_func = 0x7f35727a8650 cpp_func output = 5 |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
原文鏈接:https://www.wolfcstech.com/2017/01/24/在C代碼調(diào)用C++代碼/?utm_source=tuicool&utm_medium=referral