Linux上Poco::Logger的编译器错误-函数声明被视为宏

Compiler errors with Poco::Logger on Linux - function declaration treated as macro

本文关键字:声明 函数 错误 Poco Logger 编译器 Linux      更新时间:2023-10-16

我一直在尝试构建使用Poco::Application类作为其基础的可执行文件,但编译器给出了指向Poco代码的以下错误。

In file included from /home/mie/Poco/poco-1.4.6p4/Util/include/Poco/Util/Application.h:48:0,
                 from /home/mie/myproject/src/main.cpp:15:
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:152:59: error: macro "log" passed 3 arguments, but takes just 1
In file included from /home/mie/Poco/poco-1.4.6p4/Util/include/Poco/Util/Application.h:48:0,
                 from /home/mie/myproject/src/main.cpp:15:
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:427:58: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:428:86: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:601:72: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:610:100: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:621:30: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:627:42: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:633:33: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:639:45: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:645:30: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:651:42: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:657:32: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:663:44: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:669:31: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:675:43: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:681:36: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:687:48: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:693:30: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:699:42: error: macro "log" passed 4 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:705:30: error: macro "log" passed 2 arguments, but takes just 1
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:711:42: error: macro "log" passed 4 arguments, but takes just 1
In file included from /home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:44:0,
                 from /home/mie/Poco/poco-1.4.6p4/Util/include/Poco/Util/Application.h:48,
                 from /home/mie/myproject/src/main.cpp:15:
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Channel.h:75:15: error: expected unqualified-id before ‘__extension__’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Channel.h:75:15: error: expected ‘)’ before ‘__extension__’
In file included from /home/mie/Poco/poco-1.4.6p4/Util/include/Poco/Util/Application.h:48:0,
                 from /home/mie/myproject/src/main.cpp:15:
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:145:7: error: expected unqualified-id before ‘__extension__’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:145:7: error: expected ‘)’ before ‘__extension__’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:149:7: error: expected unqualified-id before ‘__extension__’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:149:7: error: expected ‘)’ before ‘__extension__’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:152:7: error: variable or field ‘log’ declared void
In file included from /home/mie/Poco/poco-1.4.6p4/Util/include/Poco/Util/Application.h:48:0,
                 from /home/mie/myproject/src/main.cpp:15:
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:427:7: error: variable or field ‘log’ declared void
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:428:7: error: variable or field ‘log’ declared void
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:601:21: error: variable or field ‘log’ declared void
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:601:21: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:603:2: error: expected primary-expression before ‘if’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:603:2: error: expected ‘}’ before ‘if’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:610:13: error: ‘Logger’ has not been declared
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:610:21: error: variable or field ‘log’ declared void
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:610:21: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:612:2: error: expected primary-expression before ‘if’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:612:2: error: expected ‘}’ before ‘if’
/home/mie/Poco/poco-1.4.6p4/Foundation/include/Poco/Logger.h:616:1: error: expected declaration before ‘}’ token
make[2]: *** [CMakeFiles/app.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2

这里我包括最新版本的Poco (1.4.6p4)从我的主目录,但我已经尝试了1.4.2p1-2。从CentOS 6.5存储库中获得相同的结果。在OS X上我没有这个问题(我相信库的版本是1.4.2)。

如果我看一下Poco/Logger.h中的第152行,第一个错误指向的地方,它似乎是一个函数声明,而不是错误状态下的宏。

//
// Logger.h
//
// $Id: //poco/1.4/Foundation/include/Poco/Logger.h#5 $
//
// Library: Foundation
// Package: Logging
// Module:  Logger
//
// Definition of the Logger class.
... (comment continues)

#ifndef Foundation_Logger_INCLUDED
#define Foundation_Logger_INCLUDED

#include "Poco/Foundation.h"
#include "Poco/Channel.h"
#include "Poco/Message.h"
#include "Poco/Format.h"
#include <map>
#include <vector>
#include <cstddef>

namespace Poco {

class Exception;

class Foundation_API Logger: public Channel
        /// Logger is a special Channel that acts as the main
        /// entry point into the logging framework.
    ... (comment continues)
{
public:
    ... (other declarations)
    void log(const Exception& exc, const char* file, int line);     <= line 152
                /// Logs the given exception with priority PRIO_ERROR.  
                ///
                /// File must be a static string, such as the value of
                /// the __FILE__ macro. The string is not copied
                /// internally for performance reasons.
    ... (and so on)

那么为什么我的编译器将这个函数声明作为对宏的引用?

作为旁注,在构建过程的早期有一个奇怪的错误,我刚刚注释掉了。也许这些是相关的。我尝试在内联函数中使用来自tgmath.h的fmod函数,编译器为一行代码给出了六次此错误:

/.../header.h:418: error: ”void*” is not a pointer-to-object type
/.../header.h:418: error: ”void*” is not a pointer-to-object type
/.../header.h:418: error: ”void*” is not a pointer-to-object type
/.../header.h:418: error: ”void*” is not a pointer-to-object type
/.../header.h:418: error: ”void*” is not a pointer-to-object type
/.../header.h:418: error: ”void*” is not a pointer-to-object type

下面是触发错误的头文件中的代码:

inline void ProgressCounter::createTimeString(
    std::string& timeString,
    const long long& elapsedTotalS)
{
    short elapsedH = (short)elapsedTotalS / 3600;
    //short elapsedS = 0;
    short elapsedS = fmod(elapsedTotalS, 60);           <= error here
    short elapsedM = 0;
    //short elapsedM = fmod(elapsedTotalS - elapsedS, 3600) / 60;
    char timeCString[10];
    snprintf(timeCString, 10, "%02u:%02u:%02u", elapsedH, elapsedM, elapsedS);
    timeString = timeCString;
}

包含头tgmath.h,并且在OS x上编译的代码没有问题。我不明白错误中的void*指的是哪里。

我在CentOS 6.5上使用GCC 4.4.7,在Ubuntu 12.04上使用GCC 4.4.6.3-1ubuntu5。

<math.h><tgmath.h>定义了很多宏,包括一个log宏,它与任何同名的宏冲突。

在c++代码中,您应该包含<cmath><ctgmath>,其中类型泛型宏被c++函数重载取代。

PS:至少对于gcc, <ctgmath>似乎需要c++11支持,所以你必须在编译选项中添加"-std=c++0x"或"-std=c++11"

先尝试包含您的POCO库,看看是否有帮助。就在几天前,我在Windows上的VS 2010中遇到了一个问题,当我最后包含POCO库时,很多Windows api突然变得未定义。将它们移到其他#include前面可以修复这个问题。

修复后,我没有深入分析到底是什么导致了这个问题