정의와 선언 (C++)
Microsoft 전용
일부 프로그램에서 시스템의 내보낼 수 알려진 모든 항목 (함수 및 데이터) DLL 인터페이스가입니다. 로 선언 된 모든 항목, 즉 dllimport 또는 dllexport.DLL 인터페이스에 포함 된 모든 선언을 지정 합니다는 dllimport 또는 dllexport 특성입니다.하지만 정의 지정 해야 해당 dllexport 특성.예를 들어, 다음 함수 정의 컴파일러 오류가 생성 됩니다.
__declspec( dllimport ) int func() { // Error; dllimport
// prohibited on definition.
return 1;
}
이 코드는 또한 오류를 생성 합니다.
#define DllImport __declspec( dllimport )
__declspec( dllimport ) int i = 10; // Error; this is a
// definition.
그러나이 올바른 구문입니다.
__declspec( dllexport ) int i = 10; // Okay--export definition
사용 하는 dllexport 정의 알 수 있듯이 동안 dllimport 선언 의미 합니다.사용 해야는 extern 키워드와 dllexport 를 사용 하도록 선언 됩니다. 그렇지 않으면 정의가 포함 됩니다.따라서 다음 예제에서는 올바른 있습니다.
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
extern DllImport int k; // These are both correct and imply a
DllImport int j; // declaration.
다음 예제에서는 앞의 명확 하 게 설명 합니다.
static __declspec( dllimport ) int l; // Error; not declared extern.
void func() {
static __declspec( dllimport ) int s; // Error; not declared
// extern.
__declspec( dllimport ) int m; // Okay; this is a
// declaration.
__declspec( dllexport ) int n; // Error; implies external
// definition in local scope.
extern __declspec( dllimport ) int i; // Okay; this is a
// declaration.
extern __declspec( dllexport ) int k; // Okay; extern implies
// declaration.
__declspec( dllexport ) int x = 5; // Error; implies external
// definition in local scope.
}