gpt4 book ai didi

c++ - 如何使用 Q_LOGGING_CATEGORY 并在模板函数中引用类别而不会出现多重实现错误?

转载 作者:行者123 更新时间:2023-11-28 01:22:22 27 4
gpt4 key购买 nike

我的一个头文件在 QT 项目中遇到问题。

通常我使用以下结构:

全局.h

#ifndef global
#define global
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(cat)
#endif

foo.h

#ifndef foo
#define foo
#include "global.h"
class foo{
public:
void bar();
};
#endif

foo.cpp

#include "foo.h"
Q_LOGGING_CATEGORY(cat, "awesomecategory")
void foo::bar(){
qCDebug(cat) << "helloworld";
}

效果很好。现在我有一个头文件,它从内部实现了一个模板化函数,我想记录到我的日志类别:

foo.h

#ifndef foo
#define foo

#include "global.h"

template <typename T>
bool bar(T, int val);

// Template definitions outside of header:
#include "foo.tpp"

#endif // foo

foo.tpp

#include <QLoggingCategory>
Q_LOGGING_CATEGORY(cat, "awesomecategory")
template <typename T>
bool bar(T, int val)
{
T baz;
// do stuff with baz
qCDebug(cat) << "helloworld";
}

如果我在另一个 cpp 文件中调用模板函数(当然,我因此在其中包含 foo.h),但只要我包含 foo.h 在我想调用该函数的另一个 cpp 文件中,我遇到了这样的多重定义错误:

In function `ZN11QStringListD1Ev':
C:\path\to\foo.h:{n}: multiple definition of `cat()'
C:\path\to\foo.h:{n}: first defined here
collect2.exe: error: ld returned 1 exit status

我必须在哪里放置我的 Q_LOGGING_CATEGORY 宏以便函数可以使用它并且我仍然可以在不同的地方使用标题?

最佳答案

您只能 Q_LOGGING_CATEGORY(cat, "awesomecategory") 一次,因为这实际上会从调用它的任何地方创建一个“全局”函数(见下文)。此外,当您 #include "foo.tpp" 时,您只是将该文件的内容放入标题中(例如,它不是像 .cpp 源文件那样的“单独单元”) .

如果您想要一个名为 cat 的全局日志记录类别,您可以直接在 global.h 中创建它。我认为 Q_LOGGING_CATEGORY(cat, "awesomecategory") 不会直接在 header 中工作,但这很容易解决。

下面是这些宏的作用...实际上非常简单,而且更清楚地看到发生了什么:

#define Q_DECLARE_LOGGING_CATEGORY(name) \
extern const QLoggingCategory &name();

#define Q_LOGGING_CATEGORY(name, ...) \
const QLoggingCategory &name() \
{ \
static const QLoggingCategory category(__VA_ARGS__); \
return category; \
}

所以 DECLARE 实际上只是一个导出函数的前向声明。您也可以将它放在 global.h 中(并完全跳过所有 Q 宏):

static const QLoggingCategory &cat()
{
static const QLoggingCategory category("awesomecategory");
return category;
}

它需要是全局的,或者至少在同一个默认命名空间内,或者只在一个单元内声明,因为...

#define qCDebug(category, ...) \
for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)

或者换句话说,先前创建的 cat() 函数在单元(或在我们的示例中为全局)上下文中调用。

当然,如果只从一个类的成员中调用类别样式的日志例程,那么 cat() 方法可能只属于该类。

或者 cat() 函数本身可以在命名空间中,甚至可以在它自己的类中...

class MyLoggers
{
public:
static const QLoggingCategory &cat()
{
static const QLoggingCategory category("awesomecategory");
return category;
}
}

// used somewhere else:
qCDebug(MyLoggers::cat) << "Hello cats!";

它比人们相信的那些简单的宏要灵活得多。

HTH

添加:下面的具体示例似乎没有错误。

global.h

#ifndef GLOBAL_H
#define GLOBAL_H

#include <QLoggingCategory>

static const QLoggingCategory &cat()
{
static const QLoggingCategory category("awesomecategory");
return category;
}

#endif // GLOBAL_H

foo.h

#ifndef FOO_H
#define FOO_H

#include "global.h"

template <typename T>
bool bar(T, int)
{
qCDebug(cat) << "helloworld";
return true;
}

#endif // FOO_H

baz.h

#ifndef BAZ_H
#define BAZ_H

#include "global.h"

inline void baz()
{
qCDebug(cat) << "baz() says meow.";
}

#endif // BAZ_H

main.cpp

#include "foo.h"
#include "baz.h"

int main(int argc, char *argv[])
{
bar<int>(2, 2);
baz();
return 0;
}

打印:

awesomecategory: helloworld
awesomecategory: baz() says meow.

关于c++ - 如何使用 Q_LOGGING_CATEGORY 并在模板函数中引用类别而不会出现多重实现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com