简体   繁体   English

MSVC 多行宏编译器错误

[英]MSVC multiline macro Compiler errors

I have a macro defined in a header, the function that the macro is based on is also in the same header.我在标题中定义了一个宏,该宏所基于的函数也在同一个标​​题中。

Here is a very basic example, not the exact code but hopefully it gives enough to illustrate the problem:这是一个非常基本的示例,不是确切的代码,但希望它足以说明问题:

myMacro.h: myMacro.h:

    #ifndef MYMACRO_H
        #define MYMACRO_H

        #ifdef _DEBUG
            bool myAssertFn(int test, char const* desc, char const* file, int line) {
                if (test != 0) {
                //Test passes, no action required
                    return true;
                }
                std::string msg;

                if (desc != nullptr) {
                    msg += "\n Context: ";
                    msg += desc;
                }
                if (file != NULL) {
                    msg += "\n    File: ";
                    msg += file;
                }
                if (line > 0) {
                    msg += "\n    Line: ";
                    msg += std::to_string(line);
                }
                //Construct filename
                time_t tClock = time(0);
                char szTime[24];
                tm tmNow;
                //Get system time
                localtime_s(&tmNow, &tClock);
                //Assertion Log File, path and name
                static const char* assertLogFile = "./ALF.log";
                //Build time / date of day prefix
                sprintf_s(szTime, sizeof(szTime), "%04d/%02d/%02d %02d:%02d:%02d "
                        , tmNow.tm_year + 1900, tmNow.tm_mon + 1, tmNow.tm_mday
                        , tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec);
                //Does file exist?
                std::ofstream logFile(assertLogFile, std::ios_base::app);
                //Write the content to the file
                logFile << szTime << msg.c_str() << std::endl;
                return false;
            }
            //Macro
            #define myAssert(test, desc)\               
                        myAssertFn((test), (desc), __FILE__, __LINE__)      
        #else
            #define myAssert(test, desc)\
                        (void)0
        #endif
    #endif

The purpose of this macro is to include debug information and be a replacement for the standard assert function with the added benefit of logging the results to a file.此宏的目的是包含调试信息并替代标准断言函数,并具有将结果记录到文件的额外好处。

The issue is when compiling I get:问题是编译时我得到:

error C2065: 'test' : undeclared identifier
error C2065: 'desc' : undeclared identifier

Other errors where the macro is used in a source file:在源文件中使用宏的其他错误:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2365: 'myAssertFn' : redefinition; previous definition was 'function' myAssert.h(37) : see declaration of 'myAssertFn'
error C2078: too many initializers
error C2143: syntax error : missing ';' before 'const'

In any source file that needs this macro I simply include the header and use the macro as follows:在任何需要这个宏的源文件中,我只需包含标题并按如下方式使用宏:

    myAssert(ptr != NULL, "ptr != NULL");

If the test returns 0 then the description will be logged to file with a date, time stamp, the file name and the line number the fault occurs at.如果测试返回 0,则描述将与日期、时间戳、文件名和发生故障的行号一起记录到文件中。

When you do your define, you have trailing characters :当你做你的定义时,你有尾随字符:

#define myAssert(test, desc)\               
                              ^^^^^^^^^^^^^^

That means that your backslash applies on a space and not on the endline, so myAssertFn((test), (desc), __FILE__, __LINE__) usage not part of the macro anymore.这意味着您的反斜杠应用于空格而不是结束行,因此myAssertFn((test), (desc), __FILE__, __LINE__)用法不再是宏的一部分。

Your errors on the source file likely comes from the fact that you didn't remove the definition from the header file, so there is a redefinition error.您在源文件上的错误可能是因为您没有从头文件中删除定义,因此存在重新定义错误。

With multi-line macros the \\ character must be the very last on the line.对于多行宏, \\字符必须是该行的最后一个。

Even whitespaces will disrupt multi-line macros and you have multiple spaces at the end of the line.即使是空格也会破坏多行宏,并且行尾有多个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM