I have a problem with linking constexpr constructor between two projects in one Visual Studio solution.
I have two projects in my Visual Studio 2019 Solution:
- NES_Core
- NES_Core_Tests
The first one is .lib project, the other is basic GTest project. Both use C++17.
I have the following class declared in executor.h in NES_Core:
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_;
};
}
And the definition in executor.cpp:
namespace nes::cpu::opcodes::immediate {
constexpr Executor::Executor(registers::Registers& registers) noexcept :
registers_(registers)
{
}
}
Later on I try to create Executor object in OpcodesImmediateExecutorTests.cpp in NES_Core_Tests project:
#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_;
};
Unfortunately, it fails on linking:
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)
What is more, when I remove constexpr keyword from .h and .cpp linking is performed just fine. Do you guys have any ideas why this could happen?