Microsoft KB Archive/117687

= INFO: Misplaced __declspec Causes Error C2062, C2059, or C4 =

Article ID: 117687

Article Last Modified on 12/2/2003

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5

-



This article was previously published under Q117687



SUMMARY
Compiling a C or C++ source file that contains an incorrectly placed __declspec keyword may cause the compiler to issue a warning or error message. The possible messages and the version of the 32-bit compiler generating them are listed below:   Source     CL.EXE Version    Error/Warning

C         8.xx              error C2062: type 'int' unexpected C++       8.xx              error C2059: syntax error : '__declspec(Storage_Class_Attribute)' C/C++     9.xx              error C2059: syntax error : '__declspec(Storage_Class_Attribute)' C         10.xx             error C2059: syntax error : 'type' C++       10.xx             warning C4230: anachronism used : modifiers/qualifiers interspersed, qualifier ignored where "Storage_Class_Attribute" is one of the following:
 * thread
 * naked
 * dllimport
 * dllexport



MORE INFORMATION
The compiler is generating one of these error message because an extended storage-class attribute specified with the __declspec keyword has been placed incorrectly. The extended storage-class attribute must appear to the left of the calling-convention keyword, if a keyword is specified.

The sample code shown below illustrates both the correct and incorrect placement of an extended storage-class attribute. In the declaration that fails, the __declspec keyword is placed after the __cdecl keyword. In the declaration that succeeds, the __declspec keyword is placed before the __cdecl keyword.

Sample Code
/* Compile options needed: none to compile as C source
 * /Tp to compile as C++ source (or use .cpp extension)

// This declaration will cause the C2062 and C2059 errors: void __cdecl __declspec(dllexport) Function2(void);

// This declaration will compile successfully: void __declspec(dllexport) __cdecl Function1(void);

