The curious case of error C2065: ‘IID_InterfaceName’: undeclared identifier

Recently, I stumbled across a peculiar error that I found nothing about on the web. Although I got rid of it, I didn’t figure out what the problem was. This post is merely the story of what happened.

I am working on a project that contains both C++ and C# projects. The C++ ones used to be built with /std:c++17 and we wanted to upgrade. So I switched to /std:c++latest and /std:c++20 for those containing C++/CLI code. Of course, this didn’t come for free, I had to fix a large amount of errors first, but in the end everything was building and working fine. Except that it didn’t.

The next day after committing the changes, we found out that the nightly built had failed. Looking in the logs, the following error appeared several times:

139>C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\atlmfc\include\atlctl.h(4397,61): error C2065: 'IID_InterfaceName': undeclared identifier

This was odd and to check what was going on I opened the sources with Visual Studio and built the solution on the build machine and there was no error. But running the build scripts produced this error every time.

We were using Visual Studio 2022 17.6.x. So it happened that that day version 17.7 was released and we upgraded to that thinking it might be something in the build tools that might have been fixed, but of course, there was no change. The same error continued to occur.

Looking at the build scripts, I realized the actual build was done by running the following command:

devenv.exe "C:\path\mysolution.sln" /Out "C:\Build\Logs\mysolution.log" /Rebuild "Release"

So we weren’t using MSBuild but Visual Studio itself to run the build. This is unfortunate because there are only a few options you can pass to devenv.exe. For instance, you can’t specify the verbosity of the build. If you need more detailed logs, you first need to start Visual Studio, go to Options and change the MSBuild project build output verbosity.

It looked like that opening devenv.exe and running a rebuild from the UI worked fine, but passing the solution as argument to devenv.exe and running without any UI didn’t work well. Looking at the detailed log, I could see a cl.exe command that looked like this (a simplified form is shown here):

C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\bin\HostX86\x86\CL.exe /c /I.\Include /Zi /nologo /W3 /WX- /diagnostics:column /O2 /Ob2 /Oy- /D HAVE_CPP_STDLIB /D NDEBUG /D WIN32 /D _WINDOWS /D _ATL_NO_UUIDOF /D _VC80_UPGRADE=0x0710 /D _MBCS /D _AFXDLL /GF /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /std:c++latest /Yu"stdafx.h" /Fd"..\..\Output\Release\vc143.pdb" /external:W3 /Gd /TP /analyze- /FC /errorReport:prompt /Zm200 /FS MyFile.cpp

That didn’t ring any bell though. But running the command by hand in a command prompt I could reproduce the error.

So the header where the error occurred was atlctl.h. It looked like we didn’t even need to include this one, so removing it immediately solved the build problems. But the mystery remains. I was not able to identify the cause for the different results in compiling the same sources with the same tools. If anyone has any knowledge of the cause of this I’d love to hear about it!

2 Replies to “The curious case of error C2065: ‘IID_InterfaceName’: undeclared identifier”

  1. What is the issue described in the article “The curious case of error C2065: ‘IID_InterfaceName’: undeclared identifier,” and how did it manifest during the project build? Can you explain the steps taken to investigate and attempt to resolve the problem, including any version upgrades and changes to compiler options?
    Telkom Telekomunikasi

  2. From deepseek chat:

    The error you’re encountering is likely related to the `/D _ATL_NO_UUIDOF` flag in your compilation command. This flag disables the `__uuidof` operator in ATL (Active Template Library), which is often used to retrieve the GUID (IID) of COM interfaces. If your code relies on `__uuidof` or any ATL functionality that uses it, this flag will cause compilation errors.

    ### Why the Error Occurs
    – The `/D _ATL_NO_UUIDOF` flag is defined, which means the compiler will not recognize the `__uuidof` operator.
    – If your code (or the ATL headers included in your project) uses `__uuidof` to retrieve interface IDs (IIDs), the compiler will fail with errors like `C2065: undeclared identifier` or similar.

    ### How to Fix It

    #### 1. **Remove the `/D _ATL_NO_UUIDOF` Flag**
    – If your project does not explicitly require this flag, remove it from the compilation command:
    “`plaintext
    /D _ATL_NO_UUIDOF
    “`
    – This will allow the `__uuidof` operator to work as expected.

    Updated command:
    “`plaintext
    C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\bin\HostX86\x86\CL.exe /c /I.\Include /Zi /nologo /W3 /WX- /diagnostics:column /O2 /Ob2 /Oy- /D HAVE_CPP_STDLIB /D NDEBUG /D WIN32 /D _WINDOWS /D _VC80_UPGRADE=0x0710 /D _MBCS /D _AFXDLL /GF /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /std:c++latest /Yu”stdafx.h” /Fd”..\..\Output\Release\vc143.pdb” /external:W3 /Gd /TP /analyze- /FC /errorReport:prompt /Zm200 /FS MyFile.cpp
    “`

    #### 2. **Manually Define IIDs (if you must keep `/D _ATL_NO_UUIDOF`)**
    – If you cannot remove the `/D _ATL_NO_UUIDOF` flag (e.g., for compatibility reasons), you will need to manually define the IIDs (GUIDs) for the interfaces you are using.
    – For example, if your code uses `IUnknown`, you can define its IID manually:
    “`cpp
    const IID IID_IUnknown = { 0x00000000, 0x0000, 0x0000, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
    “`
    – Replace `IID_IUnknown` with the appropriate interface name and GUID.

    #### 3. **Check ATL Headers**
    – If the error occurs in an ATL header file (e.g., `atlctl.h`), ensure that your project is correctly configured to use ATL.
    – In Visual Studio:
    1. Go to **Project Properties** > **Configuration Properties** > **General**.
    2. Set **Use of ATL** to **Static Link to ATL** or **Dynamic Link to ATL**, depending on your project requirements.

    #### 4. **Verify ATL/MFC Dependencies**
    – Ensure that your project includes the necessary ATL/MFC headers and libraries. For example:
    – Include “ for ATL functionality.
    – Include “ for MFC functionality.
    – Link against the appropriate libraries (e.g., `atl.lib`, `mfc140.lib`).

    #### 5. **Update Visual Studio**
    – If the issue persists, ensure that your Visual Studio installation is up to date. Sometimes, bugs in older versions of the compiler or libraries can cause such issues.

    ### Example Fix
    If your code uses `__uuidof` to retrieve an interface ID, you can replace it with the explicit IID. For example:

    #### Before:
    “`cpp
    REFIID riid = __uuidof(IMyInterface);
    “`

    #### After:
    “`cpp
    const IID IID_IMyInterface = { /* GUID for IMyInterface */ };
    REFIID riid = IID_IMyInterface;
    “`

    ### Summary
    – The `/D _ATL_NO_UUIDOF` flag is causing the issue. Remove it if possible.
    – If you must keep the flag, manually define the IIDs for the interfaces you are using.
    – Ensure that your project is correctly configured to use ATL/MFC and includes the necessary headers and libraries.

    If you provide more details about the specific error or the code causing it, I can offer more targeted advice.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.