When trying to compile a .dll, I'm getting an

2019-05-14 21:52发布

问题:

I have set up a project in Visual Studio to create a .dll. I have included an external library in the project which uses the keyword "interface". This is giving me the following error:

error C2146: syntax error : missing ';' before identifier 'INuiAudioBeam'

These are the lines of code where the error occurs:

#ifndef __INuiAudioBeam_FWD_DEFINED__
#define __INuiAudioBeam_FWD_DEFINED__
typedef interface INuiAudioBeam INuiAudioBeam; //Error on this line
#endif

The above code is part of a header file in the external library I have included. The project builds successfully without any errors when compiling without including the headers for the library (Note: Linking the library does not cause any problems).

What is the solution to this? Is it because I have an external library I'm using to create my dll? Should I create a .lib instead of a dll?

回答1:

If anyone else searches this, the problem is most likely

#define WIN32_LEAN_AND_MEAN

Before

#include <windows.h>

Remove the define, make sure your including windows.h, and that should define interface keyword for you



回答2:

You can get C2146 with indictment of the interface keyword. It may be confusing as to how a keyword may be undefined, especially that in other compilation units or projects that use the same header file - the inclusion of which brought about the problem - all goes smooth. However, the apparent keyword "interface" is still a compiler #define directive. It just happens to be buried inside a header file that a few developers have reasons to inspect: the <objbase.h>. There it is defined in two steps:

#define __STRUCT__ struct
#define interface __STRUCT__

Since the <objbase.h> is included by multiple other header files - such as ole2.h, oleauto.h, and a slew of ATL headers - you may be surprised with this error when none of those headers were included in the current compilation unit.