INFO: Misplaced __declspec Causes Error C2062, C2059, or C4 (117687)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Editions 1.0
  • Microsoft Visual C++, 32-bit Editions 2.0
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Editions 5.0
  • Microsoft Visual C++, 32-bit Editions 6.0

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);
				

REFERENCES

For more information, search the online documentation included with Visual C++, 32-bit Edition, on "__declspec," "thread," "naked," "dllimport," "dllexport," or "storage-class attributes." For an in-depth discussion of these topics and more, see the "Programming Techniques" book in the Visual C++, 32-bit Edition, documentation set.

Modification Type:MajorLast Reviewed:12/2/2003
Keywords:kbCompiler kberrmsg kbinfo KB117687