内联函数的未解析符号
Unresolved symbol for inline function
请考虑以下代码:
测试2.h:
#ifndef ABCD
#define ABCD
#ifdef __cplusplus
extern "C" {
#endif
void Foo();
#ifdef __cplusplus
}
#endif
#endif // ABCD
测试2.cpp
#include "StdAfx.h"
#include "Test2.h"
inline void Foo()
{
}
测试.cpp:
#include "stdafx.h"
#include "Test2.h"
int _tmain(int argc, _TCHAR* argv[])
{
Foo();
return 0;
}
当我编译此代码时,出现LNK2019错误(未解析的外部符号_Foo)。 我可以通过两种方式解决它。
- 删除内联关键字。
- 将 extern 添加到函数声明中。
假设我想要内联这个函数,为什么我必须在声明中添加 extern?
我使用VS2008。
谢谢。
C++11 标准第 3.2.3 段:
内联函数应在使用它的每个翻译单元中定义
。
您有 2 个翻译单元,首先由Test2.cpp
...:
// ... code expanded from including "StdAfx.h"
extern "C" { void Foo(); }
inline void Foo() { }
。第二个由Test.cpp
制成:
// ... code expanded from including "StdAfx.h"
extern "C" { void Foo(); }
int _tmain(int argc, _TCHAR* argv[])
{
Foo();
return 0;
}
在第二个 TU 中,缺少Foo
的定义。
为什么不简单地将Foo
的定义放入头文件中?如果编译器看不到其代码,则无法内联其代码。
相关文章:
- 用符号版本替换对函数的所有调用
- C++模板函数,用于比较任何无符号整数和有符号整数
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- 为函数定义符号不明确的指针参数
- 在函数中返回无符号字符数组,但不返回指针
- 如何返回实际值(在我的例子中是无符号字符数组)而不是来自 C++ 函数的指针?
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 仅在 MacOS 上析构函数的未定义符号
- clang:使用 O3 导出隐式实例化函数的符号
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 在函数内初始化无符号字符指针将返回空指针
- 如何识别符号表中的符号是我'application'函数
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 函数指针给我"重新声明为不同类型的符号"错误
- 类模板上一个特定函数的未解析外部符号
- 如何设计符号表来支持函数重载
- G++ 切换为不包含自己的符号函数名称(和调试数据)-
- 无符号函数,用于返回使用任何数字的数字
- 2 个未解析的外部符号(C++函数基础知识)
- C++错误LNK2001:未解析的外部符号函数_main