c++ - LNK2019 和 LNK1120 具有外部文件中的功能

标签 c++ linker linker-errors lnk2019

我已经从一个源文件中取出了一些函数到另一个文件中,因为我也想在其他文件中使用它们。当前结构如下

utils/extFuncs.h

#ifndef _extFuncs_h
#define _extFuncs_h
inline int someFunction (float v);
#endif

utils/extFuncs.cpp

#include "utils/extFuncs.h"
inline int someFunction (float v) {
    return 42;
}

foo/bar.h

#ifndef _bar_h
#define _bar_h
#include "utils/extFuncs.h"
class Bar {
public:
    Bar (float x);
};
#endif

foo/bar.cpp

#include "foo/bar.h"
Bar::Bar (float x) {
    int y = someFunction(x);
}

问题是,当我尝试编译它时,链接器提示说无法解析符号 someFunction

最佳答案

someFunction 被声明为inline,因此它必须在您的头文件中定义:

utils/extFuncs.h

#ifndef _extFuncs_h
#define _extFuncs_h
inline int someFunction (float v)
{
    return 42;
}
#endif

关于c++ - LNK2019 和 LNK1120 具有外部文件中的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3947960/

相关文章:

ios - Cocoapods 'Charts' 安装问题

c++ - 使用 C++ 从 SID 获取本地 Windows 用户名

gcc - AIX 5.3 (ld-xlc) 等效选项 Linux (ld-gcc) -rpath

c - 尝试在 Linux 上的 C/C++ 中使用 lp_solve 时出现 "undefined reference"

python - 使用 python 扩展时链接到其他库(例如 boost)

c++ - const 引用在 C++ 中是否有外部链接?

bash - 错误: could not compile `migrations_macros`

c++ - 公共(public)变量是否比使用 getter 和 setter 更快?

c++ - 解密过程中添加了多余的字母

c++ - Qt多重继承和信号