링크를 지정 하려면 extern을 사용 하 여

extern string-literal { declaration-list }
extern string-literal declaration

설명

extern 키워드는 변수나 함수를 선언 하 고 외부 링크 (이름입니다 파일에 정의 된 것과 다른 표시)를 갖도록 지정 합니다.변수를 수정 하는 경우 extern 변수 (해당 할당 됩니다 프로그램이 시작 되 고 프로그램이 끝날 때 할당 취소 하는 경우) 고정 기간이 포함 되도록 지정 합니다.변수 또는 함수가 다른 소스 파일에서 또는 나중에 같은 파일에 정의할 수 있습니다.선언 변수 및 함수에서 파일 범위는 기본적으로 외부입니다.

예제

// specifying_linkage1.cpp
int i = 1;
void other();

int main() {
   // Reference to i, defined above:
   extern int i;
}

void other() {
   // Address of global i assigned to pointer variable:
   static int *external_i = &i;

   // i will be redefined; global i no longer visible:
   // int i = 16;
}

문자열을 사용 하는 경우 C++에서 extern 다른 언어 링크 규칙 declarator(s)에 대해 사용 됨을 지정 합니다.만 이전에 C 링크가 있는 것으로 선언 된 경우 c 함수 및 데이터 액세스할 수 있습니다.그러나 개별적으로 컴파일된 번역 단위에서 정의 되어야 합니다.

Microsoft C++ 문자열을 지 원하는 "C""C++" 에 있는 문자열 리터럴 필드.모든 표준 포함 파일의 사용은 extern "C" 런타임 라이브러리 함수를 C++ 프로그램에서 사용 되는 구문을.

다음 예제는 C 링크는 이름을 선언 하는 대체 방법을 보여 줍니다.

// specifying_linkage2.cpp
// compile with: /c
// Declare printf with C linkage.
extern "C" int printf( const char *fmt, ... );

//  Cause everything in the specified header files
//   to have C linkage.
extern "C" {
   // add your #include statements here
   #include <stdio.h>
}

//  Declare the two functions ShowChar and GetChar
//   with C linkage.
extern "C" {
   char ShowChar( char ch );
   char GetChar( void );
}

//  Define the two functions ShowChar and GetChar
//   with C linkage.
extern "C" char ShowChar( char ch ) {
   putchar( ch );
   return ch;
}

extern "C" char GetChar( void ) {
   char ch;

   ch = getchar();
   return ch;
}

// Declare a global variable, errno, with C linkage.
extern "C" int errno;

참고 항목

참조

C + + 키워드

링크 사양

extern 저장소 클래스 지정자

개념

식별자의 동작

링크