Embedded Systems Design - March 2008 - (Page 10) 0308esd.p09to14 2/13/08 7:42 PM Page 10 programming pointers For object declarations that are valid in both C and C++, the rule for deciding when an object declaration is also a definition is—surprisingly—simpler in C++ than in C. In C++, an object declaration is also a definition unless it contains an extern specifier and no initializer. For example, all of the following object declarations are also definitions in C++: int i; static int j; extern int k = 0; // definition // definition // definition // foo.c #include “foo.h” int foo(int n) { } to ensure that the function defined in the source is working with the same declaration(s) as in the header. After preprocessing, the translation unit contains: int foo(int n); int foo(int n) { } // from the header They are definitions whether they appear at file scope, namespace scope, or block scope. The following object declaration is not a definition: extern int m; // non-defining declaration Again, the rule I just cited is how C++ distinguishes object definitions from other object declarations. The cor- From the compiler’s viewpoint, these are two declarations for the same function in the same scope. They refer to the same function because they have linkage. responding rule in C is complicated by the added presence of these things called tentative definitions. A tentative definition is an object declaration that might also be a definition, or it might not, depending on the presence of other definitions. In truth, the previous declarations for i and j are tentative definitions in C. C++ doesn’t recognize tentative definitions, so some combinations of object declarations that are valid in C aren’t valid in C++. The C++ rule for distinguishing object definitions from other object declarations works equally well in C as in C++. If you follow that rule in C, you won’t go wrong. THE CONCEPT OF LINKAGE In some cases, C and C++ let you declare the same entity more than once, in different translation units, different scopes of the same translation unit, or even in the same scope. For example, the common C technique for packaging a library function is to declare the function in a header: // foo.h int foo(int n); which declares the function twice. The second declaration is also the definition. From the compiler’s viewpoint, these are two declarations for the same function in the same scope. They refer to the same function because they have linkage. C and C++ provide for three levels of linkage: • A name with no linkage denotes an entity that can’t be referenced via names from anywhere else. • A name with internal linkage denotes an entity that can • be referenced via names declared in the same scope or in other scopes of the same translation unit. A name with external linkage denotes an entity that can be referenced via names declared in the same scope or in other scopes of the same translation unit (just as with internal linkage), or additionally in other translation units. Both function and object names can have either internal or external linkage. Object names can also have no linkage. Beyond that, all other names in C have no linkage. In contrast, other names in C++ can have external linkage, including names for classes, enumeration types and constants, namespaces, references, and templates. References and function templates can also have internal linkage. LINKAGE FOR FUNCTIONS A function declared without a storage class specifier normally has external linkage by default. Placing the keyword extern in a function declaration such as: extern int foo(int n); and define the function in a corresponding source file. That source file should include the header, as in: has no effect. With or without the extern specifier, you 10 MARCH 2008 | embedded systems design | www.embedded.com http://www.embedded.com
Table of Contents Feed for the Digital Edition of Embedded Systems Design - March 2008 Embedded Systems Design - March 2008 Contents #Include Programming Pointers Designing DSP-based Motor Control Using Fuzzy Logic Hardware/Software Verification Enters the Atomic Age Efficient CRC Calculation with Minimal Memory Footprint Programming Your Own Microcontroller Advertising Index Break Points Marketplace Embedded Systems Design - March 2008 Embedded Systems Design - March 2008 - (Page BB1) Embedded Systems Design - March 2008 - (Page BB2) Embedded Systems Design - March 2008 - Embedded Systems Design - March 2008 (Page Cover1) Embedded Systems Design - March 2008 - Embedded Systems Design - March 2008 (Page Cover2) Embedded Systems Design - March 2008 - Embedded Systems Design - March 2008 (Page 1) Embedded Systems Design - March 2008 - Embedded Systems Design - March 2008 (Page 2) Embedded Systems Design - March 2008 - Contents (Page 3) Embedded Systems Design - March 2008 - #Include (Page 4) Embedded Systems Design - March 2008 - #Include (Page 5) Embedded Systems Design - March 2008 - #Include (Page 6) Embedded Systems Design - March 2008 - #Include (Page 7) Embedded Systems Design - March 2008 - #Include (Page 8) Embedded Systems Design - March 2008 - Programming Pointers (Page 9) Embedded Systems Design - March 2008 - Programming Pointers (Page 10) Embedded Systems Design - March 2008 - Programming Pointers (Page 11) Embedded Systems Design - March 2008 - Programming Pointers (Page 12) Embedded Systems Design - March 2008 - Programming Pointers (Page 13) Embedded Systems Design - March 2008 - Programming Pointers (Page 14) Embedded Systems Design - March 2008 - Programming Pointers (Page 15) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 16) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 17) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 18) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 19) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 20) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 21) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 22) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 23) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 24) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 25) Embedded Systems Design - March 2008 - Designing DSP-based Motor Control Using Fuzzy Logic (Page 26) Embedded Systems Design - March 2008 - Hardware/Software Verification Enters the Atomic Age (Page 27) Embedded Systems Design - March 2008 - Hardware/Software Verification Enters the Atomic Age (Page 28) Embedded Systems Design - March 2008 - Hardware/Software Verification Enters the Atomic Age (Page 29) Embedded Systems Design - March 2008 - Hardware/Software Verification Enters the Atomic Age (Page 30) Embedded Systems Design - March 2008 - Hardware/Software Verification Enters the Atomic Age (Page 31) Embedded Systems Design - March 2008 - Hardware/Software Verification Enters the Atomic Age (Page 32) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 33) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 34) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 35) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 36) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 37) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 38) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 39) Embedded Systems Design - March 2008 - Efficient CRC Calculation with Minimal Memory Footprint (Page 40) Embedded Systems Design - March 2008 - Programming Your Own Microcontroller (Page 41) Embedded Systems Design - March 2008 - Programming Your Own Microcontroller (Page 42) Embedded Systems Design - March 2008 - Programming Your Own Microcontroller (Page 43) Embedded Systems Design - March 2008 - Programming Your Own Microcontroller (Page 44) Embedded Systems Design - March 2008 - Programming Your Own Microcontroller (Page 45) Embedded Systems Design - March 2008 - Programming Your Own Microcontroller (Page 46) Embedded Systems Design - March 2008 - Programming Your Own Microcontroller (Page 47) Embedded Systems Design - March 2008 - Advertising Index (Page 48) Embedded Systems Design - March 2008 - Break Points (Page 49) Embedded Systems Design - March 2008 - Break Points (Page 50) Embedded Systems Design - March 2008 - Marketplace (Page 51) Embedded Systems Design - March 2008 - Marketplace (Page 52) Embedded Systems Design - March 2008 - Marketplace (Page Cover3) Embedded Systems Design - March 2008 - Marketplace (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.