Skip to content

Including the header in multiple source files results in MSVC linker error #63

@hanslhansl

Description

@hanslhansl

When using the latest MSVC version if I include reflect in 2 different source files (.cpp) I get

LNK2005 ""class reflect::test::expect:: reflect::test::expect" (?expect@test@reflect@@3v@expect@test@reflect@@A) already defined in <file_name>.obj" and
LNK1169 "one or more multiply defined symbols found".

The errors are cause by auto expect = [](bool cond) { if (not cond) { failed(); } }; at line 1086 in file "reflect".

Adding inline to the variable declaration removes the linker errors: inline auto expect = [](bool cond) { if (not cond) { failed(); } };.
I don't think (?) adding inline alters functionality but I have just started using this library, someone with a deeper understanding of it would have to determine that...

The tests were done within a newly created empty project. The command line arguments as displayed in Visual Studio "Project -> Properties -> C/C++ -> Command Line" were "/permissive- /ifcOutput "x64\Release" /GS /GL /W3 /Gy /Zc:wchar_t /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc143.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /std:c++20 /FC /Fa"x64\Release" /EHsc /nologo /Fo"x64\Release" /Fp"x64\Release\Project1.pch" /diagnostics:column"

I also tried compiling with clang-cl version 18.1.6 which worked fine both with and without inline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions