6.7 External linkage

주홍영·2022년 3월 12일
0

Learncpp.com

목록 보기
66/199

https://www.learncpp.com/cpp-tutorial/external-linkage/

우리는 한 파일에서 function을 forward declaration하고
다른 파일에서 definition을 해서 사용할 수 있다는 것을 알고 있다
이는 function이 default로 external linkage이기 때문이다

Global variables with external linkage

우리는 다른 파일에서 variable에 접근하고 싶다면
extern을 통해서 접근할 수 있도록 만들 수 있다

int g_x { 2 }; // non-constant globals are external by default

extern const int g_y { 3 }; // const globals can be defined as extern, making them external
extern constexpr int g_z { 3 }; // constexpr globals can be defined as extern, making them external (but this is useless, see the note in the next section)

int main()
{
    return 0;
}

File scope vs. global scope

용어의 혼용으로 헷갈릴 수도 있다
일반적으로 global variable은 "file scope"이고
extern 으로 선언된 global variable은 external linkage를
가지고 이는 "global scope"로 보는 것이다

profile
청룡동거주민

0개의 댓글