c++ - 一个莫名其妙的 LNK2019 错误

标签 c++ linker

这是第一个文件

extern void init();

int main()
{
    init();
}

现在是第二个也是最后一个文件

inline void init()
{
  return;
}

这 2 个 C++ 文件是在 Visual Studio 2015 下作为 Win32 控制台应用程序项目构建的。 .pdb 文件是在编译后生成的。 那么为什么链接器无法解析外部函数init()呢?

test.obj : error LNK2019: unresolved external symbol "void __cdecl init(void)" (?init@@YAXXZ) referenced in function _main

最佳答案

来自 C++11 3.2/3 “一个定义规则”:

An inline function shall be defined in every translation unit in which it is odr-used

您在包含 main() 的翻译单元中违反了该要求。 (或从 init() 的定义中删除 inline)

关于c++ - 一个莫名其妙的 LNK2019 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38163565/

相关文章:

c - GTK+ 海湾合作委员会 : undefined reference to all gtk functions when linking

c - libc.so 在一个进程中映射了四个段,为什么?

c++ - 如何修复丢失的 qt 小部件库 makefile 链接?

c++ - Boost directory_iterator : 'begin' and 'end' not declared

c++ - boost::asio::io_service::poll() 的问题

c++ - 仅具有 co_return 的协程是否有任何用例?在最后?

c++ - 仅在 C/C++ 中使用 header 时?

c - MAP 文件不包含内联函数

c++ - 将 C++ 库与 Objective-C 应用程序链接和使用

c++ - 为什么闭包类型/lambda 在 C++ 中没有默认构造函数