找到一个或多个乘法定义的符号 [英] One or more multiply defined symbols found

查看:441
本文介绍了找到一个或多个乘法定义的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DebugUtil.h

DebugUtil.h

#ifndef DEBUG_UTIL_H
#define DEBUG_UTIL_H

#include <windows.h>

int DebugMessage(const char* message)
{
    const int MAX_CHARS = 1023;
    static char s_buffer[MAX_CHARS+1];

    return 0;
}

#endif

我得到这个错误:


Terrain.obj:error LNK2005:int
__cdecl DebugMessage(char const *) DebugMessage @@ YAHPBD @ Z)已在Loodus.obj中定义

Terrain.obj : error LNK2005: "int __cdecl DebugMessage(char const *)" (?DebugMessage@@YAHPBD@Z) already defined in Loodus.obj

Renderer.obj:error LNK2005:int
__cdecl DebugMessage在Loodus.obj中定义的

Renderer.obj : error LNK2005: "int __cdecl DebugMessage(char const *)" (?DebugMessage@@YAHPBD@Z) already defined in Loodus.obj

test.obj:error LNK2005:int __cdecl
DebugMessage(char const *)
(?DebugMessage @@ YAHPBD @ Z)已经
在Loodus.obj中定义

test.obj : error LNK2005: "int __cdecl DebugMessage(char const *)" (?DebugMessage@@YAHPBD@Z) already defined in Loodus.obj

C:\ Users \Tiago\Desktop\Loodus
Engine \Debug\Loodus Engine.exe:fatal
错误LNK1169:一个或多个乘法
找到定义的符号

C:\Users\Tiago\Desktop\Loodus Engine\Debug\Loodus Engine.exe : fatal error LNK1169: one or more multiply defined symbols found

但是为什么会这样呢?我在标题中有#ifndef #define和#endif,因此不应该发生多个定义

But why does this happen? I have #ifndef #define and #endif in the header so multiple definitions shouldn't happen

推荐答案

在cpp文件中,并且只留下在ah文件中的声明。包含警卫只能在一个翻译单元(也称为源文件)中运行,而不是在所有程序中运行。

Put the definition (body) in a cpp file and leave only the declaration in a h file. Include guards operate only within one translation unit (aka source file), not across all your program.

C ++标准的一个定义规则规定,定义在程序中使用的每个非内联函数。所以,另一个选择是让你的函数内联。

The One Definition Rule of the C++ standard states that there shall appear exactly one definition of each non-inline function that is used in the program. So, another alternative would be to make your function inline.

这篇关于找到一个或多个乘法定义的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆