Translation units aur linkage
Separate source contexts ko compatible declarations, internal/external linkage aur consistent definitions se connect karein.
Is chapter mein8 sections
Har source file apne context mein translate hoti hai. Linkage selected declarations ko un contexts mein same entity refer karne deti hai. Declaration share karna, object define karna aur naam ko ek translation unit tak rakhna alag samjhenge.
1. Translation unit ki apni declarations hoti hain
Translation unit source file aur included material ki preprocessing ke baad ka result hai, jisme excluded groups removed hain. Header many translation units mein declarations contribute kar sakta hai. Ek .c file ki preprocessing mein defined macro automatically doosri mein exist nahi karta. Build flags intentionally same macro dono ko de sakti hain, par woh separate configuration action hai.
- visits.c + visits.h
- Counter aur operation define karta hai
- main.c + visits.h
- Interface declare aur use karta hai
2. Interface declare, objects aur functions define karein
#ifndef CODEDILE_VISITS_H
#define CODEDILE_VISITS_H
extern unsigned codedile_visits;
void codedile_visit(void);
#endif
extern unsigned codedile_visits; yahan definition provide kiye bina object declare karta hai. Function declaration prototype deti hai. Same object ya function refer karne wali har declaration ka compatible type hona chahiye. Implementations aur callers shared header use karein; guessed declaration manually repeat karne se mismatch ho sakta hai jo traditional linker detect na kare.
#include "visits.h"
#include <limits.h>
unsigned codedile_visits = 0;
static unsigned next_count(unsigned current)
{
return current == UINT_MAX ? current : current + 1U;
}
void codedile_visit(void)
{
codedile_visits = next_count(codedile_visits);
}
Initialized file-scope object definition hai, aur function body bhi definition hai. extern hamesha declaration-only nahi: file-scope extern int value = 3; definition hai. Small helper UINT_MAX par saturate karke wraparound prevent karta hai. Shared global yahan linkage explain karne ke liye exposed hai; many application interfaces mutable state private rakhti hain.
#include "visits.h"
#include <assert.h>
int main(void)
{
assert(codedile_visits == 0);
codedile_visit();
assert(codedile_visits == 1);
return 0;
}
3. Internal, external aur no linkage name identity batati hain
| Category | Meaning |
|---|---|
| External linkage | Compatible declarations translation units ke across same entity denote kar sakti hain. |
| Internal linkage | Ek translation unit mein us identifier ki declarations uski local entity denote karti hain. |
| No linkage | Declaration entity identify karti hai bina doosri declaration se linkage-based identity ke. |
File scope par object ya function ka static identifier ko internal linkage deta hai. Doosri translation unit same spelling se different internal entity rakh sakti hai. Block-scope automatic variable normally no linkage rakhta hai. Block-scope static object ki bhi no linkage hai, lekin storage duration static hai. Isliye linkage, scope aur storage duration ko sirf ek word se infer nahi kar sakte.
Internal linkage external name linkage restrict karti hai, har possible access nahi. Static function module ke deliberately returned function pointer ke through elsewhere call ho sakta hai; private object deliberately exposed pointer se access ho sakta hai. Same translation unit ke same identifier ke liye internal aur external linkage mix na karein. Yeh program meaning ke rules hain, security boundary nahi.
4. Tentative definitions within-unit rule hain
#include <assert.h>
int codedile_total;
int codedile_total;
int main(void)
{
assert(codedile_total == 0);
return 0;
}
Same translation unit ki yeh two compatible file-scope tentative definitions, full definition absent hone par one zero-initialized definition mein result karti hain. Tentative definition ko many source files ke included header mein rakhna different situation hai: har translation unit definition supply kar sakti hai. Aise design ko portable banane ke liye linker ki legacy common-symbol merging par rely na karein.
5. External entity ki consistent program definition chahiye
Agla deliberately invalid project same externally linked object ki two initialized definitions deta hai. Checked GCC build ise link time par reject karta hai. Tool bad program accept bhi kare toh C ki program-level definition requirements apply rehti hain; exact diagnostic standard C promise nahi. Files sirf failure explain karne ke liye hain.
int codedile_shared = 1;
int codedile_shared = 2;
extern int codedile_shared;
int main(void) { return codedile_shared == 1 ? 0 : 1; }
Expression mein used externally linked identifier ke liye C normally program mein exactly one external definition require karta hai; specified unevaluated uses exceptions hain. Aise use na bhi ho, phir bhi at most one external definition honi chahiye. Successful link saare type, lifetime, bounds ya ownership contracts establish nahi karta.
Primary references aur aage ki reading
- WG14 N1570 — public C11 committee draft: 5.1.1.1–5.1.1.2, 6.2.1–6.2.4, 6.2.7, 6.9, 6.9.2. C17 baseline ke stable core rules ke liye; final ISO publication nahi.
Aage badhne se pehle
- Har translation unit ka apna preprocessing aur declaration context hai.
- Linkage appropriate contexts ki same-entity declarations connect karti hai.
- Shared interfaces ko compatible declarations aur correct program-level definitions chahiye.
In ideas ko alag rakhein
- Internal linkage / Block scope
- Internal linkage declarations mein entity identity ka rule hai; block scope name visibility ka rule hai.
Do files translate hokar bhi shared entity ke interface par disagree kaise kar sakti hain?
Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.Apni understanding check karein
Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.
Har choice ki explanation padhein
- Nahi. Unke identifiers ki apni translation units mein internal linkage hai.
✓ Sahi reasoning. Same spelling, internal-linkage identifiers ki shared external identity establish nahi karti.
- Haan, har matching spelling ek global object name karti hai.
Scope aur linkage rules matter karte hain; sirf matching text enough nahi.
- static ka matlab hamesha block scope hai.
static file-scope declarations mein bhi ho sakta hai; scope declaration location par depend karta hai.
C17
Agla idea padhne ke liye tayyar?
Mark this chapter as read whenever you feel ready.