13 to Intermixing C and C++</title><shortdesc>When porting an open source application or any C (or C++) applications |
13 to Intermixing C and C++</title><shortdesc>When porting an open source application or any C (or C++) applications |
14 on top of Symbian/S60 using P.I.P.S., the developer will come across situations |
14 on top of Symbian/S60 using P.I.P.S., the developer will come across situations |
15 where C and C++ (and Symbian C++ as well) codes will be used together. The |
15 where C and C++ (and Symbian C++ as well) codes will be used together. The |
16 open source community implements a vast number of libraries that export C |
16 open source community implements a vast number of libraries that export C |
17 APIs to the user of such libraries.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody> |
17 APIs to the user of such libraries.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody> |
18 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-7-1-11-1-1-5-1-3-1-7-1-4-1-4-1-3-1"> <title>When |
18 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-10-1-12-1-1-5-1-3-1-7-1-4-1-4-1-3-1"> <title>When |
19 and why to use C linkage</title> <p>While porting such applications, |
19 and why to use C linkage</title> <p>While porting such applications, |
20 if developers have to intermix C and C++ code, they then have to depend on |
20 if developers have to intermix C and C++ code, they then have to depend on |
21 C++ language features like extern "C" for giving C linkage to some set of |
21 C++ language features like extern "C" for giving C linkage to some set of |
22 APIs. This is required because C and C++ compilers handle function prototype |
22 APIs. This is required because C and C++ compilers handle function prototype |
23 in a different way. C++ compilers use name mangling, (or name decoration) |
23 in a different way. C++ compilers use name mangling, (or name decoration) |
30 or defined in C++ source or headers are subject to name mangling. When it |
30 or defined in C++ source or headers are subject to name mangling. When it |
31 comes to C, however, there is nothing called name mangling or overloading. |
31 comes to C, however, there is nothing called name mangling or overloading. |
32 It applies when the developer tries to use C APIs by including corresponding |
32 It applies when the developer tries to use C APIs by including corresponding |
33 headers. To avoid name mangling, the developer should mention explicitly that |
33 headers. To avoid name mangling, the developer should mention explicitly that |
34 those APIs are C APIs, by using the extern "C" keyword. </p> </section> |
34 those APIs are C APIs, by using the extern "C" keyword. </p> </section> |
35 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-7-1-11-1-1-5-1-3-1-7-1-4-1-4-1-3-2"> <title>Syntax |
35 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-10-1-12-1-1-5-1-3-1-7-1-4-1-4-1-3-2"> <title>Syntax |
36 of extern C</title> <p>The syntax of extern "C" is shown below: </p><codeblock xml:space="preserve">extern "C" declaration ; |
36 of extern C</title> <p>The syntax of extern "C" is shown below: </p><codeblock xml:space="preserve">extern "C" declaration ; |
37 </codeblock><p>The declaration (or definition) that immediately follows extern |
37 </codeblock><p>The declaration (or definition) that immediately follows extern |
38 "C" has the C linkage. </p><codeblock xml:space="preserve">extern "C" { |
38 "C" has the C linkage. </p><codeblock xml:space="preserve">extern "C" { |
39 declaration ; |
39 declaration ; |
40 declaration ; |
40 declaration ; |
41 ... |
41 ... |
42 }</codeblock><p>Everything between the curly braces has C linkage, unless |
42 }</codeblock><p>Everything between the curly braces has C linkage, unless |
43 declared otherwise. </p> </section> |
43 declared otherwise. </p> </section> |
44 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-7-1-11-1-1-5-1-3-1-7-1-4-1-4-1-3-3"> <title>How |
44 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-10-1-12-1-1-5-1-3-1-7-1-4-1-4-1-3-3"> <title>How |
45 to use extern C</title> <p>While writing header files with C functions |
45 to use extern C</title> <p>While writing header files with C functions |
46 which will be included by both C and C++ source files, the user must use extern |
46 which will be included by both C and C++ source files, the user must use extern |
47 "C" properly. See the example below: </p><codeblock xml:space="preserve">/*File: GoodCHeader.h */ |
47 "C" properly. See the example below: </p><codeblock xml:space="preserve">/*File: GoodCHeader.h */ |
48 /* Can be used by C/C++ header and source files directly */ |
48 /* Can be used by C/C++ header and source files directly */ |
49 |
49 |
81 void Foo() { |
81 void Foo() { |
82 // use those C APIs here |
82 // use those C APIs here |
83 int ret = Function2(10); |
83 int ret = Function2(10); |
84 } |
84 } |
85 </codeblock> </section> |
85 </codeblock> </section> |
86 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-7-1-11-1-1-5-1-3-1-7-1-4-1-4-1-3-4"> <title>Mixing |
86 <section id="GUID-88635D46-AEF6-4E8E-969D-D3E56941F289-GENID-1-10-1-12-1-1-5-1-3-1-7-1-4-1-4-1-3-4"> <title>Mixing |
87 C and C++ features using extern "C"</title> <p>The developer |
87 C and C++ features using extern "C"</title> <p>The developer |
88 can use all the features of C++ except templates within C by giving those |
88 can use all the features of C++ except templates within C by giving those |
89 functions extern "C" linkage. See the example below:</p><codeblock xml:space="preserve">#include <iostream> |
89 functions extern "C" linkage. See the example below:</p><codeblock xml:space="preserve">#include <iostream> |
90 using namespace std; |
90 using namespace std; |
91 |
91 |