When Visual Studio is in Debug mode, there is a problem of definition conflict when the Eigen library is included in the MFC project

When Visual Studio is in Debug mode, there is a problem of definition conflict when the Eigen library is included in the MFC project

error message

Eigen\src\Core\PlainObjectBase.h(143,5): error C2061: syntax error: identifier "THIS_FILE"
Eigen\src\Core\PlainObjectBase.h(143,1): error C2333: "Eigen::PlainObjectBase: :operator new”: error in function declaration; skipping function body
Eigen\src\Core\CwiseNullaryOp.h(341,27): error C2039: “setConstant”: not a member of “Eigen::PlainObjectBase”

The above is only a part of the error message. When Visual Studio compiles and reports errors, don't look at the "Error List Window", but look at the "Output" window. Search for "error" in the output window to find the location of the first error message, which can quickly find out the cause of the error.
As shown below:
insert image description here

Solution

According to the error message, " THIS_FILE" is debuga macro enabled by MFC in mode, which is convenient for determining the location of the memory leak during debugging. It seems that the error message is related to it. Macros defined
with also include etc. The guess is that the operator is overloaded , and the operator is also overloaded in Eigen. Including Eigen later will cause an unclear error. So try putting Eigen's include location before the definition. Specifically, find the location of the macro definition in the code containing Eigen, as follows:THIS_FILEDEBUG_NEWDEBUG_NEWnewnewDEBUG_NEWnew
DEBUG_NEWDEBUG_NEW

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

Just place the include statement before the code segment. If you don't need to detect memory leaks, you can also delete this code directly.

If you encounter such problems elsewhere, the solution is nothing more than adjusting the inclusion location of Eigen to include it before including other various libraries. For example, include before including boostrelated header files Eigen/Core.

Guess you like

Origin blog.csdn.net/qq_42679415/article/details/132122406