.cpp 文件中定义的 Constexpr 构造函数导致链接错误

Constexpr constructor defined in .cpp file causes linking error

我在一个 Visual Studio 解决方案中的两个项目之间链接 constexpr 构造函数时遇到问题。

我的 Visual Studio 2019 解决方案中有两个项目:

第一个是.lib工程,另一个是基本的GTest工程。两者都使用 C++17.

我在 NES_Core 中的 executor.h 中声明了以下 class:

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_;
    };
}

以及executor.cpp中的定义:

namespace nes::cpu::opcodes::immediate {
    constexpr Executor::Executor(registers::Registers& registers) noexcept :
        registers_(registers)
    {
    }
}

稍后我尝试在 NES_Core_Tests 项目的 OpcodesImmediateExecutorTests.cpp 中创建 Executor 对象:

#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: unresolved external symbol "public: __thiscall nes::cpu::opcodes::immediate::Executor::Executor(struct nes::cpu::registers::Registers &)" (??0Executor@immediate@opcodes@cpu@nes@@QAE@AAURegisters@registers@34@@Z) referenced in function "public: __thiscall OPCodes_::OPCodes_(void)" (??0OPCodes_@@QAE@XZ)

此外,当我从 .h 和 .cpp 中删除 constexpr 关键字时,链接执行得很好。你们知道为什么会发生这种情况吗?

该函数隐含为内联函数。它应该在调用它的任何编译单元中定义。

来自c++17标准(10.1.5的constexpr说明符)

1 The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template. A function or static data member declared with the constexpr specifier is implicitly an inline function or variable...