C++ Crash Course: a fast-Paced Introduction


Intermingling C and C++ Object Files



Yüklə 7 Mb.
Pdf görüntüsü
səhifə31/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   27   28   29   30   31   32   33   34   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

Intermingling C and C++ Object Files
C and C++ code can coexist peacefully if you’re careful. Sometimes, it’s nec-
essary for a C compiler to link object files emitted by a C++ compiler (and 
vice versa). Although this is possible, it requires a bit of work. 
Two issues are related to linking the files. First, the calling conventions 
in the C and C++ code could potentially be mismatched. For example, the 
protocols for how the stack and registers are set when you call a function 
could be different. These calling conventions are language-level mismatches 
and aren’t generally related to how you’ve written your functions. Second, 
C++ compilers emit different symbols than C compilers do. Sometimes the 
linker must identify an object by name. C++ compilers assist by decorating 
the object, associating a string called a decorated name with the object. Because 
of function overloads, calling conventions, and 
namespace
usage, the compiler 
must encode additional information about a function beyond just its name 
through decoration. This is done to ensure that the linker can uniquely iden-
tify the function. Unfortunately, there is no standard for how this decoration 
occurs in C++ (which is why you should use the same tool chain and settings 
when linking between translation units). C linkers know nothing about C++ 
name decoration, which can cause problems if decoration isn’t suppressed 
whenever you link against C code within C++ (and vice versa).
The fix is simple. You wrap the code you want to compile with C-style 
linkages using the statement 
extern "C"
, as in Listing 13.
// header.h
#ifdef __cplusplus
extern "C" {
#endif
void extract_arkenstone();
struct MistyMountains {
int goblin_count;
};
#ifdef __cplusplus
}
#endif
Listing 13: Employing C-style linkage
This header can be shared between C and C++ code. It works because 
__cplusplus
is a special identifier that the C++ compiler defines (but the C 
compiler doesn’t). Accordingly, the C compiler sees the code in Listing 14 
after preprocessing completes. Listing 14 illustrates the code that remains.


xlvi
An Overture to C Programmers
void extract_arkenstone();
struct MistyMountains {
int goblin_count;
};
Listing 14: The code remaining after the preprocessor processes Listing 13 in a C 
environment
This is just a simple C header. The code between the 
#ifdef __cplusplus
statements is removed during preprocessing, so the 
extern "C"
wrapper isn’t 
visible. For the C++ compiler, 
__cplusplus
is defined in 
header.h
, so it sees the 
contents of Listing 15.
extern "C" {
void extract_arkenstone();
struct MistyMountains {
int goblin_count;
};
}
Listing 15: The code remaining after the preprocessor processes Listing 13 in a C++ 
environment
Both 
extract_arkenstone
and 
MistyMountains
are now wrapped with 
extern 
"C"
, so the compiler knows to use C linkage. Now your C source can call into 
compiled C++ code, and your C++ source can call into compiled C code.
C++ Themes
This section takes you on a brief tour of some core themes that make C++ 
the premier system-programming language. Don’t worry too much about the 
details. The point of the following subsections is to whet your appetite.

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   27   28   29   30   31   32   33   34   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin