文件中定义的 Constexpr 构造函数.cpp会导致链接错误
Constexpr constructor defined in .cpp file causes linking error
我在一个Visual Studio解决方案中的两个项目之间链接constexpr构造函数时遇到问题。
我的Visual Studio 2019解决方案中有两个项目:
- NES_Core
- NES_Core_Tests
第一个是.lib项目,另一个是基本的GTest项目。两者都使用 C++17。
我在 NES_Core 年的 executor.h 中声明了以下类:
namespace nes::cpu::opcodes::immediate {
class Executor
{
public:
constexpr Executor(registers::Registers& registers) noexcept;
~Executor() = default;
Executor(Executor& rhs) = delete;
Executor(Executor&& rhs) = delete;
Executor& operator=(const Executor& rhs) = delete;
Executor& operator=(Executor&& rhs) = delete;
private:
registers::Registers& registers_;
};
}
以及执行器中的定义.cpp:
namespace nes::cpu::opcodes::immediate {
constexpr Executor::Executor(registers::Registers& registers) noexcept :
registers_(registers)
{
}
}
稍后,我尝试在OpcodesImmediateExecutorTests中创建Executor对象.cpp NES_Core_Tests项目中:
#include "pch.h"
#include "nes/cpu/registers/registers.h"
#include "nes/cpu/opcodes/immediate/executor.h"
class OPCodes_ : public ::testing::Test
{
public:
OPCodes_() :
reg_(),
ie_(reg_)
{
}
nes::cpu::registers::Registers reg_;
nes::cpu::opcodes::immediate::Executor ie_;
};
不幸的是,它在链接时失败:
OpcodesImmediateExecutorTests.obj : error LNK2019: 未解析的外部符号 "public: __thiscall nes::cpu::opcodes::immediate::Executor::Executor(struct nes::cpu::registers::Registers &(" (??0Executor@immediate@opcodes@cpu@nes@@QAE@AAURegisters@registers@34@@Z( 在函数"public: __thiscall OPCodes_::OPCodes_(void(" 中引用 (??0OPCodes_@@QAE@XZ(
更重要的是,当我从 .h 中删除 constexpr 关键字并且.cpp链接执行得很好时。你们知道为什么会发生这种情况吗?
该函数隐式是一个内联函数。它应该在任何调用它的编译单元中定义。
来自 c++17 标准(10.1.5 constexpr 说明符(
1 constexpr 说明符应仅适用于 变量或变量模板或函数的声明或 函数模板。声明的函数或静态数据成员 constexpr 说明符隐式是一个内联函数或 可变...
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误