Search code examples
c++visual-c++declspec

__declspec(dllimport) causes compiler crash on MSVC 2010


In a *.cpp file, trying to use a third party lib:

#define DLL_IMPORT 
#include <thirdParty.h>
// Third party header has code like:
// #ifdef DLL_IMPORT
//    #define DLL_DECL __declspec(dllimport)

// fatal error C1001: An internal error has occurred in the compiler.

Alternative:

#define NO_DLL
#include <thirdParty.h>
// Third party header has code like:
// #elif defined(NO_DLL)
//    #define DLL_DECL


// Compiles fine, but linker errors as can't find DLL functions

I can reproduce results by remove macros and #define all together and manually editing the third party files to have __declspec(dllimport) or not - so the preprocessor stuff above is just to show what's going on, it's not copy-paste.

Has anyone come across anything similar, or can hint at the cause? (which is created using CMake). Above is actual example of 2 line *.cpp that crashes so it's narrowed down to something in the #include.

The following also work fine:

  1. Compile the examples provided by the third party (they provide a *.sln) that use dllimport/export so it doesn't appear to be the fault of the library
  2. Compile the third party lib as part of the production project (so dllexport works fine)

I've trawled the project settings pages of the two projects to try and spot differences, but have come up blank. Of course, it's possible I'm missing something as those settings pages are not the easiest to navigate. I'll get access to VS2008 in a day or so, so can compare with that. The third party library is MySql++.


Solution

  • A compiler crash is definitely a bug in the compiler, so you're best off submitting an error report to the Microsoft Visual C++ team.

    As for the error

    #define DLL_DECL __declspec(dllimport)
    

    is the wrong way to go about things. There should be some project setting you need to set, a pre-processing directive you can define instead if DLL_DECL, or simply including a different file.