3

我已经在单独的头文件中声明了一些常量变量(即constant.h)。

我在debug.cpp中包含了 constant.h 以访问该变量。

我在main.cpp中包含了constant.hdebug.h来访问变量。

当我编译时,它显示的错误**multiple definition** of **IF_DEBUG_ENABLED**

请告诉我实际上我做错了什么。另外,请注意这是我第一个 c/c++应用程序的第一天。我什至从未在学校读过它。

我的代码源如下:as

/ -- 常量.h -- /

#ifndef CONSTANT_H
#define CONSTANT_H

const char* APP_NAME            = "ymcmcb";
const bool  IF_DEBUG_ENABLED    = true;

#endif // CONSTANT_H

/ -- 调试.h -- /

#ifndef DEBUG_H
#define DEBUG_H

#include <QString>

class Debug
{  
public:
    static void Log(QString Message);
};

#endif // DEBUG_H

/ -- 调试.cpp -- /

#include "constant.h"
#include "debug.h"

#include "QDebug"

static void Log(QString Message)
{
    if (IF_DEBUG_ENABLED)
        qDebug() << Message;    //It says problem is here
}

/ -- main.cpp -- /

#include "constant.h"
#include "debug.h"

int main(int argc, char *argv[])
{
    Debug::Log("New application has been run");
}
4

2 回答 2

4

C 和 C++ 有“编译单元”的概念,它本质上是“你告诉我编译的文件中的所有代码加上它包含的所有文件”。

C 编译的原始管道是首先运行“预处理器”以读取所有代码,处理宏和定义等,并将生成的代码输出到单个文件中(从内存中,.i 文件用于中间)

foo.cpp

#include "foo1.h"
FOO {
    #include "foo2.h"
}

foo1.h

extern "C" int puts(const char*);
#define FOO int main()

foo2.h

puts("Hello, world\n");

编译g++ -Wall -E -o foo.i foo.cppg++ -Wall -o foo.exe foo.i

foo.i文件如下所示:

# 1 "foo.cpp"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 30 "/usr/include/stdc-predef.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/bits/predefs.h" 1 3 4
# 31 "/usr/include/stdc-predef.h" 2 3 4
# 1 "<command-line>" 2
# 1 "foo.cpp"
# 1 "foo1.h" 1
extern "C" int puts(const char*);
# 2 "foo.cpp" 2

int main() {
# 1 "foo2.h" 1
 puts("Hello, world!\n");
# 5 "foo.cpp" 2
}

这是一个编译单元。这些天来,这个过程被简化了,编译器本身内置了预处理器,但编译单元的概念仍然存在。

您的代码的问题在于您正在定义 - 而不仅仅是声明 -IF_DEBUG_ENABLED在头文件中,因此可能在多个编译单元中。当链接器尝试将已编译的单元组合成可执行文件时,它会找到同名变量的多个实例。链接器无法判断它们应该是同一个东西。

要创建在多个编译单元(源文件)之间可见的全局变量或函数,您需要一个标头声明/原型和一个源文件定义/实例。

标题

extern bool IF_DEBUG_ENABLED; // variable declaration.
extern void herp();           // function prototype, but
void herp();                  // the extern is optional for functions.

为了能够使用其中任何一个,您现在需要通过实现来备份它们。

源文件

bool IF_DEBUG_ENABLED = true;

也就是说,假设您希望它是一个运行时变量。另一个选择是使用#define,就像你使用的守卫一样:

常数.h

#ifndef CONSTANT_H // poor choice, there may be a CONSTANT_H somewhere else.
#define CONSTANT_H 1

...
#define IF_DEBUG_ENABLED // comment out to disable

#endif

资源:

#if defined(IF_DEBUG_ENABLED)
   qDebug() << message;
#endif

此选项不允许您在运行时更改 IF_DEBUG_ENABLED,只有在编译时定义了 IF_DEBUG_ENABLED 时,“qDebug() << message”代码才会写入可执行文件。

最后,您可以在文件开头用一行替换所有三个,而不是使用 #if ... #define ... #endif 保护方法:

常数.h:

#pragma once  //<<-- compiler implements a guard for you.

#include <QString>

class Debug
{  
public:
    static void Log(QString Message);
};
于 2013-09-02T20:19:07.660 回答
4

您应该将定义放在 .cpp 文件中,而不是标题中。在标题中,您应该只放置声明extern const bool IF_DEBUG_ENABLED;这将告诉任何代码#include存在一些名为 的全局变量IF_DEBUG_ENABLED。在 debug.cpp 中,您应该放置实际定义。

守卫仅有助于防止您在单个编译单元中定义多次。但是你有两个编译单元:debug.cpp(加上头文件)和 main.cpp(加上头文件)。这意味着您在标头中有多个变量定义。


您还有另一个问题:您实现了一个静态函数Log(),但您应该实现一个静态类方法Debug::Log()

于 2013-09-02T19:06:32.820 回答