Search code examples
c++.netc++-cliclr

Import error (E0337) while linking C++ and C++/CLI projects (.NET Core)


I am currently trying to set up a .NET project. I have a existing back project in c++ that I want to display on a web interface. To do this I am trying to link my C++ to the .NET platforme via a CLR Class Library .NET Core (and ultimately link that to an ASP.NET web interface but that is the next step).

My problem comes when I try to import the original files to the C++/CLI project. When the file I try to import uses C++ vectors (#include <vector>) I get eight errors all containing E0337 linkage specification is incompatible with previous "bsearch_s" (decalred at line 26) in the file corecrt_search.h (being a windows 10 kit file). You can see full content of the errors on this screenshot.

The other errors are respectively for functions qsort_s, bsearch, _lfind_s, _lfind, _lsearch_s, _lsearch and qsort at lines 35, 48, 64, 74, 83, 93 and 56.

No errors arise when I the imported file doesn't use vectors.

The file corecrt_search.h contains indeed two declaration of these functions each should be compiled in a different context. Here is a little extract of this file (hope this is enough).

_CRT_BEGIN_C_HEADER

_Check_return_
_ACRTIMP void* __cdecl bsearch(...);


// Managed search routines
#if defined __cplusplus && defined _M_CEE
extern "C++"
{
    _Check_return_
    void* __clrcall bsearch(...);
}
#endif

I don't understand why compiling the CLI project creates a conflict...

Here is how I linked the two projects:

  • I added the original project as a dependency to the CLR

  • In Linker => Input => Delay loaded Dlls, I added original_project.dll (usefull to add the ASP.NET project afterwards I think)

  • In C/C++ => General => Additional Include Directories, I added the path to the original project

I work on Visual Studio Community 2019 Version 16.4.2

Does anyone knows how to prevent such an error to occur? Thanks a lot!


Solution

  • I had the same issue. I was getting the same error when using

    vector and Mat(OpenCV)

    Its just the IntelliSense error. Seems to be a bug in IntelliSense. I changed the mode to Build Only and the error was gone.

    enter image description here