【发布时间】:2021-12-24 20:37:06
【问题描述】:
我有一个仅包含标头的库,我们称之为foo.h,它包含在bar.h 中,它还包含在foobar.h 中。链接器抛出 LNK2005 错误,指出来自 foo.h 的函数和结构已在 foobar.obj 中定义。
foo.h 文件如下所示:
#pragma once
#ifndef FOO
#define FOO
//few structs and functions declarations and definitions (these aren't static nor inline) inside a namespace
#endif
//include guards added after I got the error for the first time
bar.h 看起来像这样:
#include "foo.h"
//some other code
foobar.h 看起来像这样:
#include "bar.h"
//some other code
这个问题可能是重复的:Why aren't include guards or pragma once working?
但是,除了使这些函数内联或静态之外,还有其他方法可以解决这个问题吗?也许根本不需要编辑foo.h?
【问题讨论】:
标签: linker linker-errors